Storage 储存操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class BaseLocalStorage {
constructor(preId, timeSign) {
this.preId = preId // 定义本地存储数据库前缀
this.timeSign = timeSign || '|-|' // 定义时间戳与存储数据之间的拼接符
// 操作状态
this.status = {
SUCCESS: 0, // 成功
FAILURE: 1, // 失败
OVERFLOW: 2, // 溢出
TIMEOUT: 3 // 过期
}
this.storage = localStorage || window.localStorage // 保存本地存储链接
}

/**
* 获取本地存储数据库数据真实字段
* @param {*} key
* @returns
* @memberof BaseLocalStorage
*/
getKey (key) {
return this.preId + key
}

/**
* 添加(修改)数据
* @param {*} key 数据字段标识
* @param {*} value 数据值
* @param {*} callback 回调函数
* @param {*} time 过期时间
* @memberof BaseLocalStorage
*/
set (key, value, callback, time) {
let status = this.status.SUCCESS // 操作状态
key = this.getKey(key)
try {
time = new Date(time).getTime() || time.getTime() // 根据时间参数获取时间戳
} catch (e) {
time = new Date().getTime() + 1000 * 60 * 60 * 24 // 未传入时间参数或者时间参数有误,获取默认时间:一个月
}
try {
this.storage.setItem(key, time + this.timeSign + (Object.prototype.toString.call(value).slice(8, -1) === 'Object' ? JSON.stringify(value) : value))
} catch (e) {
status = this.status.OVERFLOW
}
callback && callback.call(this, status, key, value)
}

/**
* 获取数据
* @param {*} key 数据字段标识
* @param {*} callback 回调函数
* @returns
* @memberof BaseLocalStorage
*/
get (key, callback) {
let status = this.status.SUCCESS // 操作状态
key = this.getKey(key)
let value = null // 默认值
let timeSignLen = this.timeSign.length // 时间戳与存储数据之间的拼接符长度
let that = this //缓存当前对象
let index // 时间戳与存储数据之间的拼接符起始位置
let time // 时间戳
let result //最终获取的数据
try {
value = that.storage.getItem(key) //获取字段对应的数据字符串
} catch (e) {
result = {
status: that.status.FAILURE,
value: null
}
callback && callback.call(this, result, status, result.value)
return result
}
if (value) {
index = value.indexOf(that.timeSign) // 获取时间戳与数据之间的拼接符起始位置
time = +value.slice(0, index) //获取时间戳
if (new Date(time).getTime() > new Date().getTime() || time === 0) {
value = value.slice(index + timeSignLen) //获取数据结果(拼接符后面的字符串
} else {
// 过期则结果为null,标记操作状态为过期,并且删除该字段
value = null
status = that.status.TIMEOUT
that.remove(key)
}
} else {
status = that.status.FAILURE
}
// 设置结果
try { value = JSON.parse(value) } catch (e) { }
result = {
status,
value
}
callback && callback.call(this, result.status, result.value)
return result
}

/**
* 删除数据
* @param {*} key 数据字段标识
* @param {*} callback 回调函数
* @memberof BaseLocalStorage
*/
remove (key, callback) {
let status = this.status.FAILURE
key = this.getKey(key)
let value = null
try {
value = this.storage.getItem(key) // 获取字段对应的数据
} catch (e) { }
if (value) {
try {
this.storage.removeItem(key)
status = this.status.SUCCESS
} catch (e) { }
}
// 执行回调,注意传入回调函数中的数据值:如果操作状态成功则返回真实的数据结果,否则返回空
callback && callback.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSign) + this.timeSign.length))
}
}

// #region 测试用例
const Leo = new BaseLocalStorage('Leo_')

Leo.set('test', 'XueBin', function (...params) { console.log('[set] -> test', params) })
Leo.get('test', function (...params) { console.log('[get] -> test', params) })
Leo.remove('test', function (...params) { console.log('[remove] -> test', params) })
Leo.remove('test', function (...params) { console.log('[remove] -> test', params) })
Leo.get('test', function (...params) { console.log('[get] -> test', params) })

Leo.set('testObj', { "SUCCESS": 0, "FAILURE": 1, "OVERFLOW": 2, "TIMEOUT": 3 }, function (...params) { console.log('[set] -> testObj', params) })
Leo.get('testObj', function (...params) { console.log('[get] -> testObj', params) })
Leo.remove('testObj', function (...params) { console.log('[remove] -> testObj', params) })
Leo.remove('testObj', function (...params) { console.log('[remove] -> testObj', params) })
Leo.get('testObj', function (...params) { console.log('[get] -> testObj', params) })
// #endregion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class StorageFn {
constructor () {
this.ls = window.localStorage;
this.ss = window.sessionStorage;
}

/*-----------------cookie---------------------*/
/*设置cookie*/
setCookie (name, value, day) {
var setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
for (var i in setting) {
var oDate = new Date();
oDate.setDate(oDate.getDate() + day);
document.cookie = i + '=' + setting[i] + ';expires=' + oDate;
}
}else{
var oDate = new Date();
oDate.setDate(oDate.getDate() + day);
document.cookie = name + '=' + value + ';expires=' + oDate;
}

}

/*获取cookie*/
getCookie (name) {
var arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('=');
if (arr2[0] == name) {
return arr2[1];
}
}
return '';
}

/*删除cookie*/
removeCookie (name) {
this.setCookie(name, 1, -1);
}


/*-----------------localStorage---------------------*/
/*设置localStorage*/
setLocal(key, val) {
var setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
for(var i in setting){
this.ls.setItem(i, JSON.stringify(setting[i]))
}
}else{
this.ls.setItem(key, JSON.stringify(val))
}

}

/*获取localStorage*/
getLocal(key) {
if (key) return JSON.parse(this.ls.getItem(key))
return null;

}

/*移除localStorage*/
removeLocal(key) {
this.ls.removeItem(key)
}

/*移除所有localStorage*/
clearLocal() {
this.ls.clear()
}


/*-----------------sessionStorage---------------------*/
/*设置sessionStorage*/
setSession(key, val) {
var setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
for(var i in setting){
this.ss.setItem(i, JSON.stringify(setting[i]))
}
}else{
this.ss.setItem(key, JSON.stringify(val))
}

}

/*获取sessionStorage*/
getSession(key) {
if (key) return JSON.parse(this.ss.getItem(key))
return null;

}

/*移除sessionStorage*/
removeSession(key) {
this.ss.removeItem(key)
}

/*移除所有sessionStorage*/
clearSession() {
this.ss.clear()
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!