整理---定义全局产品cookie存取

window.localData = {};
window.historyData = {};
jQuery(function () {
    localData = (function () {
        var hname = "localData" + (new Date().getTime());
        return { //other cookie; //storage() || userData() || 
            time: 10,
            date: function (day) {
                var date = new Date();
                date.setTime(date.getTime() + (parseInt(day) || parseInt(this.time) || 10) * 24 * 3600 * 1000);
                return date.toGMTString();
            },
            set: function (key, val) {
                if (!!key) {
                    document.cookie = key + "=" + encodeURIComponent(val) + "; path=/; ";
                }
                return this;
            },
            get: function (key) {
                if (!!key) {
                    var arrCookie = (document.cookie || "").split("; ");
                    for (var i = 0; i < arrCookie.length; i++) {
                        var arr = arrCookie[i].split("=");
                        if (arr[0] == key)
                            return decodeURIComponent(arr[1]);
                    }
                    return null;
                }
            },
            remove: function (key) {
                if (!!key) {
                    document.cookie = key + "=" + encodeURIComponent("") + "; path=/; expires=" + this.date(-10);
                }
                return this;
            }
        };
    })();


    historyData = {
        local: localData,
        type: Object.prototype.toString,
        isArray: Array.isArray || function (obj) {
            return this.type.call(obj) === "[object Array]";
        },
        isObj: function (data) {
            return typeof data === "string" || typeof data === "number";
        },
        join: function (json, depth, flag) {
            var str = "",
                key = "",
                val = "";
            for (key in json) {
                str += flag === 1 ? this.jsonToStr(key, depth, true) + ":" : "";
                str += this.jsonToStr(json[key], depth, true) + ",";
            }
            return str.lastIndexOf(",") === str.length - 1 ? str.substring(0, str.length - 1) : str;
        },
        jsonToStr: function (json, depth, flag) {
            var str = "",
                index = 1,
                depth = depth || 10;
            if (index++ > depth) return "";
            if (typeof json === "string" || typeof json === "number") {
                if (!flag) {
                    return (str + (json || "")).replace(/([\\\"])/g, "\\$1");
                } else {
                    return '\"' + (str + (json || "")).replace(/([\\\"])/g, "\\$1") + '\"';
                }
            }
            if (this.isArray(json)) {
                return str + "[" + this.join(json, depth, 0) + "]";
            } else if (typeof json === "object") {
                return str + "{" + this.join(json, depth, 1) + "}";
            }
        },
        set: function (key, val, depth) {
            this.local.set(key, this.jsonToStr(val, depth));
        },
        get: function (key) {
            var val = this.local.get(key);
            if (!val) return null;
            if ((val.indexOf("{") === 0 && val.lastIndexOf("}") === val.length - 1) || (val.indexOf("[") === 0 && val.lastIndexOf("]") === val.length - 1))
                try {
                    val = eval('(' + val + ')');
                } catch (e) {

                }
            return val;
        },
        removeAll: function (key) {
            this.local.remove(key);
        }
    };

    window.localData = localData;
    window.historyData = historyData;

});

 

posted @ 2019-06-27 15:10  一块一  阅读(305)  评论(0)    收藏  举报