store.js
1004 Bytes
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
import {
gbs
} from 'config/settings.js';
class Store {
constructor() {
this.store = window.localStorage;
this.prefix = gbs.db_prefix;
}
set(key, value, fn) {
try {
value = JSON.stringify(value);
} catch (e) {
value = value;
}
this.store.setItem(this.prefix + key, value);
fn && fn();
}
get(key, fn) {
if (!key) {
throw new Error('没有找到key。');
return;
}
if (typeof key === 'object') {
throw new Error('key不能是一个对象。');
return;
}
var value = this.store.getItem(this.prefix + key);
if (value !== null) {
try {
value = JSON.parse(value);
} catch (e) {
value = value;
}
}
return value;
}
remove(key) {
this.store.removeItem(this.prefix + key);
}
}
module.exports = new Store();