创建模块:
(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; } };