存储工具
# js localStorage工具
/*******************************************************
* 设置sessionStorage缓存
* @param {String} key 缓存对象key
* @param {String/Object} value 缓存对象value
* @return {Boolean} 是否保存成功 true/false
*******************************************************/
export const setSStorage = function (key, value) {
if (arguments.length !== 2) return false;
let v = value;
if (typeof v === 'object') {
v = JSON.stringify(v);
v = 'ri-obj-' + v;
} else {
v = 'ri-str-' + v;
}
let ss = sessionStorage;
if (ss) {
ss.setItem(key, v);
} else { return false; }
return true;
};
/*********************************************
* 获取sessionStorage缓存
* @param {String} key 缓存对象key
* @return {String/Object} 缓存对象value
*********************************************/
export const getSStorage = function (key) {
let ss = sessionStorage;
if (ss) {
let v = ss.getItem(key);
if (!v) { return false; }
if (v.indexOf('ri-obj-') === 0) {
v = v.slice(7);
return JSON.parse(v);
} else if (v.indexOf('ri-str-') === 0) {
return v.slice(7);
} else { return false; }
} else { return false; }
};
/*******************************************
* 删除sessionStorage缓存
* @param {String} key 缓存对象key
* @return {Boolean} 是否删除成功 true/false
*******************************************/
export const removeSStorage = function (key) {
let ss = sessionStorage;
if (ss && key) {
ss.removeItem(key);
return true;
} else { return false; }
};
/***************************************
* 清理sessionStorage缓存
* @return {Boolean} 是否清理成功 true/false
***************************************/
export const clearSStorage = function () {
let ss = sessionStorage;
if (ss) {
ss.clear();
return true;
} else { return false; }
}
/*******************************************************
* 设置localStorage缓存
* @param {String} key 缓存对象key
* @param {String/Object} value 缓存对象value
* @return {Boolean} 是否保存成功 true/false
*******************************************************/
export const setLStorage = function (key, value) {
if (arguments.length !== 2) return false;
let v = value;
if (typeof v === 'object') {
v = JSON.stringify(v);
v = 'ri-obj-' + v;
} else {
v = 'ri-str-' + v;
}
let ls = localStorage;
if (ls) {
ls.setItem(key, v);
} else { return false; }
return true;
};
/*********************************************
* 获取localStorage缓存
* @param {String} key 缓存对象key
* @return {String/Object} 缓存对象value
*********************************************/
export const getLStorage = function (key) {
let ls = localStorage;
if (ls) {
let v = ls.getItem(key);
if (!v) { return false; }
if (v.indexOf('ri-obj-') === 0) {
v = v.slice(7);
return JSON.parse(v);
} else if (v.indexOf('ri-str-') === 0) {
return v.slice(7);
} else { return false; }
} else { return false; }
};
/*******************************************
* 删除localStorage缓存
* @param {String} key 缓存对象key
* @return {Boolean} 是否删除成功 true/false
*******************************************/
export const removeLStorage = function (key) {
let ls = localStorage;
if (ls && key) {
ls.removeItem(key);
return true;
} else { return false; }
};
/***************************************
* 清理localStorage缓存
* @return {Boolean} 是否清理成功 true/false
***************************************/
export const clearLStorage = function () {
let ls = localStorage;
if (ls) {
ls.clear();
return true;
} else { return false; }
}
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
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
更新时间: 3/26/2021, 9:15:55 AM