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
| export const storage = {
| // 后缀标识
| suffix: "_deadtime",
|
| /**
| * 获取
| * @param {*} key 关键字
| */
| get(key: string): any {
| return uni.getStorageSync(key);
| },
|
| /**
| * 获取全部
| */
| info() {
| const { keys } = uni.getStorageInfoSync();
| const d: any = {};
|
| keys.forEach((e: string) => {
| d[e] = uni.getStorageSync(e);
| });
|
| return d;
| },
|
| /**
| * 设置
| * @param {*} key 关键字
| * @param {*} value 值
| * @param {*} expires 过期时间
| */
| set(key: string, value: any, expires?: number): void {
| uni.setStorageSync(key, value);
|
| if (expires) {
| uni.setStorageSync(`${key}${this.suffix}`, Date.now() + expires * 1000);
| }
| },
|
| /**
| * 是否过期
| * @param {*} key 关键字
| */
| isExpired(key: string): boolean {
| const raw = uni.getStorageSync(`${key}${this.suffix}`);
| if (raw === "" || raw === null || raw === undefined) {
| return false;
| }
| const dead = Number(raw);
| if (!dead) {
| return true;
| }
| return dead - Date.now() <= 0;
| },
|
| /**
| * 删除
| * @param {*} key 关键字
| */
| remove(key: string) {
| return uni.removeStorageSync(key);
| },
|
| /**
| * 清理
| */
| clear() {
| uni.clearStorageSync();
| },
|
| /**
| * 获取一次后删除
| */
| once(key: string) {
| const value = this.get(key);
| this.remove(key);
| return value;
| },
| };
|
|