zoukankan      html  css  js  c++  java
  • 存储:cookie,localstorage

    创建模块:

    (function () {
        /**
         * FCZX.foo.bar
         */
        let FCZX = {};
        FCZX.globalNamespace = function (ns) {
            var nsParts = ns.split(".");
            var root = window;
            for (var i = 0; i < nsParts.length; i++) {
                if (typeof root[nsParts[i]] == "undefined") {
                    root[nsParts[i]] = {};
                }
                root = root[nsParts[i]];
            }
            return root;
        }
    
        window.FCZX = FCZX;
    })();

    简单加密:

    (function () {
        FCZX.globalNamespace('FCZX.Encript');
    
        FCZX.Encript.encode = function (value) {
            if (value) {
                return btoa(encodeURIComponent(JSON.stringify(value)))
            }
        }
    
        FCZX.Encript.decode = function (value) {
            if (value) {
                return JSON.parse(decodeURIComponent(atob(value)))
            }
        }
    })();

    localstorage:

    (function (win) {
        FCZX.globalNamespace('FCZX.Store');
    
        FCZX.Store.setItem = function (key, value) {
            win.localStorage.setItem(key, FCZX.Encript.encode(value))
        }
    
        FCZX.Store.getItem = function (key) {
            let value = win.localStorage.getItem(key)
            if (value) {
                return FCZX.Encript.decode(value)
            }
        }
    
        FCZX.Store.clear = function (key) {
            key ? win.localStorage.removeItem(key) : win.localStorage.clear();
        }
    })(window);

    cookie:

    setCookie = function (key, value, expires, domain, path = "/") {
        let today = new Date();
        today.setTime(today.getTime());
        if (expires) {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        let expiresDate = new Date(today.getTime() + (expires));
    
        document.cookie = key + "=" + escape(value) +
            ((expires) ? ";expires=" + expiresDate.toGMTString() : "") +
            ((path) ? ";path=" + path : "") +
            ((domain) ? ";domain=" + domain : "");
    }
    
    getCookie = function (key) {
        var allCookies = document.cookie.split(';');
        var cookieKey = '';
        var cookieValue = '';
        var tempCookie = '';
        var isFound = false;
    
        for (const item of allCookies) {
            tempCookie = item.split('=');
            cookieKey = tempCookie[0].replace(/^s+|s+$/g, '');
            if (cookieKey == key) {
                isFound = true;
                if (tempCookie.length > 1) {
                    cookieValue = unescape(tempCookie[1].replace(/^s+|s+$/g, ''));
                }
                return cookieValue
            }
            cookieKey = '';
            tempCookie = null;
        }
        if (!isFound) {
            return null
        }
    }
    
    deleteCookie = function (key, domain, path = '/') {
        if (app.getCookie(key)) {
            document.cookie = key + "=" +
                ((path) ? ";path=" + path : "") +
                ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
    }
    getToken = function () {
        var arr, reg = new RegExp("(^| )_x_token=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) {
            return unescape(arr[2]);
        } else {
            return undefined;
        }
    };
  • 相关阅读:
    关于10月20日#8的六道题的心得与感悟
    关于10月19日#7的六道题的心得与感悟
    关于10月17日#6的五道数据结构题的心得与感悟
    关于10月16日模拟赛的心得与感悟
    关于10月15日#5的四道图论题的心得与感悟
    关于10月14日#4的四道图论题的心得与感悟
    关于跨进程通信AIDL的一些总结
    对App应用架构搭建的一些思考
    Java 实例化接口或抽象类
    使用ViewPager实现卡片叠加效果
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13577299.html
Copyright © 2011-2022 走看看