美文网首页
js常用的函数(封装)

js常用的函数(封装)

作者: 董董董董董董董董董大笨蛋 | 来源:发表于2018-08-02 09:06 被阅读0次
    • //获取当前日期时间
    let current = function(str) {
            var _d = new Date();
            var _s = _d.getFullYear() + '-' + (_d.getMonth() + 1) + '-' + _d.getDate();
            if (str) {
                _s += ' ' + _d.getHours() + ':' + _d.getMinutes() + ':' + _d.getSeconds();
            }
            return _s;
        };
    
    
    • //获取当前设备的逻辑宽度和高度
        let viewport = {
            width: $(window).width(),
            height: $(window).height()
        };
    
    • //获取当前页面的名称
        let getPageName = function() {
            var href = window.location.href;
            var index = href.indexOf('?');
            if (index > 0) {
                href = href.substr(0, index);
            }
            index = href.lastIndexOf('\/');
            if (index > 0) {
                href = href.substr(index + 1);
            }
            return href;
        };
    

    //设置本地缓存
    //修改人:zhou.fx
    e.setLocalStorage = function(key, value, isJson) {
    if (window.localStorage) {
    if (isJson) {
    value = JSON.stringify(value);
    }
    window.localStorage[key] = value;
    } else {
    e.log("当前浏览器不支持localStorage");
    }
    };

    //获取本地缓存
    //修改人zhou.fx
    e.getLocalStorage = function(key, isJson) {
        if (window.localStorage) {
            var value = window.localStorage[key] || "";
            if (isJson && value) {
                value = JSON.parse(value);
            }
            return value;
        } else {
            e.log("当前浏览器不支持localStorage");
        }
    };
    //清除本地缓存removelocalStorage
    e.removelocalStorage = function(key) {
        if (window.localStorage) {
            window.localStorage.removeItem(key);
        }
    };
    
    //设置运行时缓存setSessionStorage
    e.setSessionStorage = function(key, content) {
        if (window.sessionStorage) {
            window.sessionStorage[key] = content;
        } else {
            e.log('您的浏览器不支持setSessionStorage方法');
        }
    };
    
    //获取运行时缓存getSessionStorage
    e.getSessionStorage = function(key) {
        if (window.sessionStorage) {
            return window.sessionStorage[key] || '';
        } else {
            e.log('您的浏览器不支持getSessionStorage方法');
        }
    };
    
    //清除运行时缓存removeSessionStorage
    e.removeSessionStorage = function(key) {
        if (window.sessionStorage) {
            window.sessionStorage.removeItem(key);
        }
    };
    
    //获取某个cookie
    e.getCookie = function(cookie_name, decode) {
        //2016-11-22 suncao 修改ACCESS_TOKEN从localStorage中获取
        if ('ACCESS_TOKEN' == cookie_name) {
            return e.getLocalStorage('ACCESS_TOKEN');
        }
        decode = decode || true;
        var allcookies = document.cookie;
        var cookie_pos = allcookies.indexOf(cookie_name);
        var value = '';
        if (cookie_pos > -1) {
            cookie_pos += cookie_name.length + 1;
            var cookie_end = allcookies.indexOf(";", cookie_pos);
            if (cookie_end == -1) {
                cookie_end = allcookies.length;
            }
            value = allcookies.substring(cookie_pos, cookie_end);
            if (decode) {
                try {
                    value = decodeURIComponent(value);
                } catch (e) {
                    console.log(e);
                    return "";
                }
            }
        }
        return value;
    };
    
    //设置某个cookie
    e.setCookie = function(cookie_name, cookie_value, domain, isencode, expTime) {
        var exp = new Date();
        var expires = "";
        if (expTime) {
            exp.setTime(exp.getTime() + expTime);
            expires = ";expires=" + exp.toGMTString();
        }
        if (!domain) {
            domain = document.domain;
            if (/^[a-z]/i.test(document.domain)) {
                domain = document.domain.substring(document.domain.indexOf("."));
            }
        }
    
        isencode = typeof isencode == 'undefined' ? true : isencode;
        if (isencode) {
            cookie_value = encodeURIComponent(cookie_value);
        }
        // 2016-11-22 suncao 修改ACCESS_TOKEN 存储为localStorage形式,避免应用退出用户丢失,重新登录问题。
        if ('ACCESS_TOKEN' == cookie_name) {
            e.setLocalStorage('ACCESS_TOKEN', cookie_value);
            document.cookie = cookie_name + "=" + cookie_value + "; path=/; domain=" + domain + ";" + expires;
        } else {
            document.cookie = cookie_name + "=" + cookie_value + "; path=/; domain=" + domain + ";" + expires;
        }
    };
    

    相关文章

      网友评论

          本文标题:js常用的函数(封装)

          本文链接:https://www.haomeiwen.com/subject/ljfzcxtx.html