提交 | 用户 | age
|
215d8b
|
1 |
import type { LockInfo, UserInfo } from '/#/store'; |
V |
2 |
import type { ProjectConfig } from '/#/config'; |
967b28
|
3 |
import type { RouteLocationNormalized } from 'vue-router'; |
15567e
|
4 |
|
f6cef1
|
5 |
import { createLocalStorage, createSessionStorage } from '/@/utils/cache'; |
f57eb9
|
6 |
import { Memory } from './memory'; |
V |
7 |
import { |
|
8 |
TOKEN_KEY, |
|
9 |
USER_INFO_KEY, |
|
10 |
ROLES_KEY, |
|
11 |
LOCK_INFO_KEY, |
|
12 |
PROJ_CFG_KEY, |
|
13 |
APP_LOCAL_CACHE_KEY, |
|
14 |
APP_SESSION_CACHE_KEY, |
967b28
|
15 |
MULTIPLE_TABS_KEY, |
f57eb9
|
16 |
} from '/@/enums/cacheEnum'; |
V |
17 |
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting'; |
|
18 |
import { toRaw } from 'vue'; |
2f6253
|
19 |
|
f57eb9
|
20 |
interface BasicStore { |
V |
21 |
[TOKEN_KEY]: string | number | null | undefined; |
15567e
|
22 |
[USER_INFO_KEY]: UserInfo; |
V |
23 |
[ROLES_KEY]: string[]; |
|
24 |
[LOCK_INFO_KEY]: LockInfo; |
|
25 |
[PROJ_CFG_KEY]: ProjectConfig; |
967b28
|
26 |
[MULTIPLE_TABS_KEY]: RouteLocationNormalized[]; |
c620f8
|
27 |
} |
V |
28 |
|
f57eb9
|
29 |
type LocalStore = BasicStore; |
2f6253
|
30 |
|
f57eb9
|
31 |
type SessionStore = BasicStore; |
V |
32 |
|
|
33 |
export type BasicKeys = keyof BasicStore; |
|
34 |
type LocalKeys = keyof LocalStore; |
|
35 |
type SessionKeys = keyof SessionStore; |
|
36 |
|
f6cef1
|
37 |
const ls = createLocalStorage(); |
V |
38 |
const ss = createSessionStorage(); |
f57eb9
|
39 |
|
V |
40 |
const localMemory = new Memory(DEFAULT_CACHE_TIME); |
|
41 |
const sessionMemory = new Memory(DEFAULT_CACHE_TIME); |
|
42 |
|
f6cef1
|
43 |
function initPersistentMemory() { |
f57eb9
|
44 |
const localCache = ls.get(APP_LOCAL_CACHE_KEY); |
15567e
|
45 |
const sessionCache = ss.get(APP_SESSION_CACHE_KEY); |
f57eb9
|
46 |
localCache && localMemory.resetCache(localCache); |
V |
47 |
sessionCache && sessionMemory.resetCache(sessionCache); |
2f6253
|
48 |
} |
f6cef1
|
49 |
|
f57eb9
|
50 |
export class Persistent { |
V |
51 |
static getLocal<T>(key: LocalKeys) { |
|
52 |
return localMemory.get(key)?.value as Nullable<T>; |
|
53 |
} |
f7aa93
|
54 |
|
f57eb9
|
55 |
static setLocal(key: LocalKeys, value: LocalStore[LocalKeys], immediate = false): void { |
V |
56 |
localMemory.set(key, toRaw(value)); |
|
57 |
immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache); |
|
58 |
} |
2f6253
|
59 |
|
f57eb9
|
60 |
static removeLocal(key: LocalKeys): void { |
V |
61 |
localMemory.remove(key); |
|
62 |
} |
26b610
|
63 |
|
f57eb9
|
64 |
static clearLocal(): void { |
V |
65 |
localMemory.clear(); |
|
66 |
} |
26b610
|
67 |
|
f57eb9
|
68 |
static getSession<T>(key: SessionKeys) { |
V |
69 |
return sessionMemory.get(key)?.value as Nullable<T>; |
|
70 |
} |
|
71 |
|
|
72 |
static setSession(key: SessionKeys, value: SessionStore[SessionKeys], immediate = false): void { |
|
73 |
sessionMemory.set(key, toRaw(value)); |
fedd9c
|
74 |
immediate && ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache); |
f57eb9
|
75 |
} |
V |
76 |
|
|
77 |
static removeSession(key: SessionKeys): void { |
|
78 |
sessionMemory.remove(key); |
|
79 |
} |
|
80 |
static clearSession(): void { |
|
81 |
sessionMemory.clear(); |
|
82 |
} |
|
83 |
|
|
84 |
static clearAll() { |
|
85 |
sessionMemory.clear(); |
|
86 |
localMemory.clear(); |
c8021e
|
87 |
} |
2f6253
|
88 |
} |
陈 |
89 |
|
f57eb9
|
90 |
window.addEventListener('beforeunload', function () { |
V |
91 |
ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache); |
|
92 |
ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache); |
|
93 |
}); |
|
94 |
|
|
95 |
function storageChange(e: any) { |
|
96 |
const { key, newValue, oldValue } = e; |
|
97 |
|
|
98 |
if (!key) { |
|
99 |
Persistent.clearAll(); |
|
100 |
return; |
2f6253
|
101 |
} |
f7aa93
|
102 |
|
f57eb9
|
103 |
if (!!newValue && !!oldValue) { |
V |
104 |
if (APP_LOCAL_CACHE_KEY === key) { |
|
105 |
Persistent.clearLocal(); |
2f6253
|
106 |
} |
f57eb9
|
107 |
if (APP_SESSION_CACHE_KEY === key) { |
V |
108 |
Persistent.clearSession(); |
2f6253
|
109 |
} |
陈 |
110 |
} |
f57eb9
|
111 |
} |
f7aa93
|
112 |
|
f57eb9
|
113 |
window.addEventListener('storage', storageChange); |
V |
114 |
|
f6cef1
|
115 |
initPersistentMemory(); |