storage.js
1.84 KB
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
const ls = window.localStorage;
const ss = window.sessionStorage;
export const Cookie = {
get(key) {
let arr = document.cookie.split('; ');
for (let i = 0; i < arr.length; i++) {
let arr2 = arr[i].trim().split('=');
if (arr2[0] == key) {
return arr2[1];
}
}
return '';
},
set(key, value, day) {
let setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object') {
for (let i in setting) {
let oDate = new Date();
oDate.setDate(oDate.getDate() + day);
document.cookie = i + '=' + setting[i] + ';expires=' + oDate;
}
} else {
let oDate = new Date();
oDate.setDate(oDate.getDate() + day);
document.cookie = key + '=' + value + ';expires=' + oDate;
}
},
remove(key) {
this.set(key, 1, -1);
},
};
export const Local = {
get(key) {
if (key) return JSON.parse(ls.getItem(key));
return null;
},
set(key, val) {
const setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object') {
for (const i in setting) {
ls.setItem(i, JSON.stringify(setting[i]));
}
} else {
ls.setItem(key, JSON.stringify(val));
}
},
remove(key) {
ls.removeItem(key);
},
clear() {
ls.clear();
},
};
export const Session = {
get(key) {
if (key) return JSON.parse(ss.getItem(key));
return null;
},
set(key, val) {
const setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object') {
for (const i in setting) {
ss.setItem(i, JSON.stringify(setting[i]));
}
} else {
ss.setItem(key, JSON.stringify(val));
}
},
remove(key) {
ss.removeItem(key);
},
clear() {
ss.clear();
},
};