|
|
|
|
|
|
|
BI.Cache = {
|
|
|
|
_prefix: "bi",
|
|
|
|
setUsername: function (username) {
|
|
|
|
localStorage.setItem(BI.Cache._prefix + ".username", (username + "" || "").toUpperCase());
|
|
|
|
},
|
|
|
|
getUsername: function () {
|
|
|
|
return localStorage.getItem(BI.Cache._prefix + ".username") || "";
|
|
|
|
},
|
|
|
|
_getKeyPrefix: function () {
|
|
|
|
return BI.Cache.getUsername() + "." + BI.Cache._prefix + ".";
|
|
|
|
},
|
|
|
|
_generateKey: function (key) {
|
|
|
|
return BI.Cache._getKeyPrefix() + (key || "");
|
|
|
|
},
|
|
|
|
getItem: function (key) {
|
|
|
|
return localStorage.getItem(BI.Cache._generateKey(key));
|
|
|
|
},
|
|
|
|
setItem: function (key, value) {
|
|
|
|
localStorage.setItem(BI.Cache._generateKey(key), value);
|
|
|
|
},
|
|
|
|
removeItem: function (key) {
|
|
|
|
localStorage.removeItem(BI.Cache._generateKey(key));
|
|
|
|
},
|
|
|
|
clear: function () {
|
|
|
|
for (var i = localStorage.length; i >= 0; i--) {
|
|
|
|
var key = localStorage.key(i);
|
|
|
|
if (key) {
|
|
|
|
if (key.indexOf(BI.Cache._getKeyPrefix()) === 0) {
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
keys: function () {
|
|
|
|
var result = [];
|
|
|
|
for (var i = localStorage.length; i >= 0; i--) {
|
|
|
|
var key = localStorage.key(i);
|
|
|
|
if (key) {
|
|
|
|
var prefix = BI.Cache._getKeyPrefix();
|
|
|
|
if (key.indexOf(prefix) === 0) {
|
|
|
|
result[result.length] = key.substring(prefix.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
addCookie: function (name, value, path, expiresHours) {
|
|
|
|
var cookieString = name + "=" + encodeURI(value);
|
|
|
|
// 判断是否设置过期时间
|
|
|
|
if (expiresHours && expiresHours > 0) {
|
|
|
|
var date = new Date();
|
|
|
|
// expires是标准GMT格式时间,应该使用时间戳作为起始时间
|
|
|
|
date.setTime(date.getTime() + expiresHours * 3600 * 1000);
|
|
|
|
cookieString = cookieString + "; expires=" + date.toUTCString();
|
|
|
|
}
|
|
|
|
if (path) {
|
|
|
|
cookieString = cookieString + "; path=" + path;
|
|
|
|
}
|
|
|
|
document.cookie = cookieString;
|
|
|
|
},
|
|
|
|
getCookie: function (name) {
|
|
|
|
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
|
|
|
|
if (arr = document.cookie.match(reg)) {return decodeURI(arr[2]);}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
deleteCookie: function (name, path) {
|
|
|
|
var date = new Date();
|
|
|
|
date.setTime(date.getTime() - 10000);
|
|
|
|
var cookieString = name + "=v; expires=" + date.toUTCString();
|
|
|
|
if (path) {
|
|
|
|
cookieString = cookieString + "; path=" + path;
|
|
|
|
}
|
|
|
|
document.cookie = cookieString;
|
|
|
|
}
|
|
|
|
};
|