const BaseStorage = function (preId) {
this.preId = preId || 'WEIBO-';
this.time = -1;
this.expression = '|-|'
}
BaseStorage.prototype = {
status: {
SUCCESS: 0,
FAILURE: 1,
OVERFLOW: 2,
TIMEOUT: 3
},
storage: localStorage || window.localStorage,
setCookie: function(key, value, t){
var oDate = new Date();
oDate.setDate(oDate.getDate()+t);
if(t != -1){
document.cookie=key+"="+encodeURIComponent(value)+";expires="+oDate.toUTCString();
}
else{
document.cookie=key+"="+encodeURIComponent(value);
}
if(this.getCookie(key)){
return true;
}else{
return false;
}
},
getCookie: function (key){
var str = document.cookie.replace(/;\s*/,';');
var cookieArr = str.split(';');
var cookieObj = {};
var len = cookieArr.length;
for(var i = 0; i < len; i++){
var item = cookieArr[i];
var k = item.split('=')[0];
var v = item.split('=')[1];
cookieObj[k] = v;
}
if(cookieObj[key]){
return decodeURIComponent(cookieObj[key]);
}else{
return false;
}
},
removeCookie: function (key){
document.cookie = key+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
if(!this.getCookie(key)){
return true;
}else{
return false;
}
},
getKey: function(key){
return this.preId + key;
},
set: function (type, key, val, time, cb) {
var status = this.status.SUCCESS;
key = this.getKey(key);
if(type == 'local'){
this.storage = localStorage || window.localStorage
}
else if(type == 'session'){
this.storage = sessionStorage || window.sessionStorage
}
else{
this.storage = document.cookie;
}
try{
if(time != -1){
this.time = new Date().getTime() + time * 24 * 60 * 1000 ;
}
else{
this.time = -1
}
}
catch(e){
this.time = new Date().getTime() + 1000 * 24 * 60 * 60 * 31;
}
try {
if(window.localStorage || window.sessionStorage){
if(type == 'local' || type == 'session'){
if(typeof val != Object){
val = this.time != -1 ? val + '|-|'+this.time : val;
this.storage.setItem(key, val)
}
else{
val = this.time != -1 ? JSON.stringify(val) + '|-|'+this.time : val;
this.storage.setItem(key, val)
}
}
else{
this.setCookie(key, val, this.time)
}
}
else{
this.setCookie(key, val, this.time)
}
}
catch(e){
status = this.status.OVERFLOW;
}
cb && cb.call(this, status, key, val);
},
get: function (type, key, cb) {
var status = this.status.SUCCESS;
key = this.getKey(key);
var value = '';
var timeExpire = 0;
if(type == 'local'){
this.storage = localStorage || window.localStorage
}
else if(type == 'session'){
this.storage = sessionStorage || window.sessionStorage
}
else{
this.storage = document.cookie;
}
try{
if(window.localStorage || window.sessionStorage){
var index = this.storage.getItem(key).indexOf(this.expression);
timeExpire = this.time != -1 ? Number(this.storage.getItem(key).slice(index + this.expression.length)) : -1
if(type == 'local' || type == 'session'){
value = this.storage.getItem(key);
}
else{
value = this.getCookie(key)
}
}
else{
value = this.getCookie(key)
}
}
catch(e){
value = null;
cb && cb.call(this, status, key, value);
return value;
}
if(timeExpire == -1 || new Date().getTime() <= timeExpire){
cb && cb.call(this, status, value);
}
else{
value = null;
status = this.status.TIMEOUT;
this.remove(type, key)
}
},
remove: function (type, key, cb) {
var status = this.status.FAILURE;
key = this.getKey(key);
if(type == 'local'){
this.storage = localStorage || window.localStorage
}
else if(type == 'session'){
this.storage = sessionStorage || window.sessionStorage
}
else{
this.storage = document.cookie;
}
try{
if(window.localStorage || window.sessionStorage){
if(type == 'local' || type == 'session'){
this.storage.removeItem(key);
}
else{
this.removeCookie(key);
}
}
else{
this.removeCookie(key);
}
cb && cb.call(this, status, null);
}catch(e){
cb && cb.call(this, this.status.FAILURE);
}
}
}