forked from fanruan/fineui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.0 KiB
98 lines
3.0 KiB
8 years ago
|
/*
|
||
|
* 前端缓存
|
||
|
*/
|
||
|
window.localStorage || (window.localStorage = {
|
||
|
items: {},
|
||
|
setItem: function (k, v) {
|
||
|
BI.Cache.addCookie(k, v);
|
||
|
},
|
||
|
getItem: function (k) {
|
||
|
return BI.Cache.getCookie(k);
|
||
|
},
|
||
|
removeItem: function (k) {
|
||
|
BI.Cache.deleteCookie(k);
|
||
|
},
|
||
|
key: function () {
|
||
|
|
||
|
},
|
||
|
clear: function () {
|
||
|
this.items = {};
|
||
|
}
|
||
|
});
|
||
|
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 + "=" + escape(value);
|
||
|
// 判断是否设置过期时间
|
||
|
if (expiresHours && expiresHours > 0) {
|
||
|
var date = new Date();
|
||
|
date.setTime(date.getTime() + expiresHours * 3600 * 1000);
|
||
|
cookieString = cookieString + "; expires=" + date.toGMTString();
|
||
|
}
|
||
|
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 unescape(arr[2]);
|
||
|
else
|
||
|
return null;
|
||
|
},
|
||
|
deleteCookie: function (name, path) {
|
||
|
var date = new Date();
|
||
|
date.setTime(date.getTime() - 10000);
|
||
|
var cookieString = name + "=v; expires=" + date.toGMTString();
|
||
|
if (path) {
|
||
|
cookieString = cookieString + "; path=" + path;
|
||
|
}
|
||
|
document.cookie = cookieString;
|
||
|
}
|
||
|
};
|