Vben
authored
|
1
2
|
import type { LockInfo, UserInfo } from '/#/store';
import type { ProjectConfig } from '/#/config';
|
Vben
authored
|
3
|
import type { RouteLocationNormalized } from 'vue-router';
|
Vben
authored
|
4
|
|
Vben
authored
|
5
|
import { createLocalStorage, createSessionStorage } from '/@/utils/cache';
|
Vben
authored
|
6
7
8
9
10
11
12
13
14
|
import { Memory } from './memory';
import {
TOKEN_KEY,
USER_INFO_KEY,
ROLES_KEY,
LOCK_INFO_KEY,
PROJ_CFG_KEY,
APP_LOCAL_CACHE_KEY,
APP_SESSION_CACHE_KEY,
|
Vben
authored
|
15
|
MULTIPLE_TABS_KEY,
|
sanmu
authored
|
16
|
ORDER_COLUMN,
|
Vben
authored
|
17
18
19
|
} from '/@/enums/cacheEnum';
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
import { toRaw } from 'vue';
|
|
20
|
import { pick, omit } from 'lodash-es';
|
Vben
authored
|
21
22
23
|
interface BasicStore {
[TOKEN_KEY]: string | number | null | undefined;
|
Vben
authored
|
24
25
26
27
|
[USER_INFO_KEY]: UserInfo;
[ROLES_KEY]: string[];
[LOCK_INFO_KEY]: LockInfo;
[PROJ_CFG_KEY]: ProjectConfig;
|
Vben
authored
|
28
|
[MULTIPLE_TABS_KEY]: RouteLocationNormalized[];
|
sanmu
authored
|
29
|
[ORDER_COLUMN]: any;
|
vben
authored
|
30
31
|
}
|
Vben
authored
|
32
|
type LocalStore = BasicStore;
|
vben
authored
|
33
|
|
Vben
authored
|
34
|
type SessionStore = BasicStore;
|
|
35
|
|
Vben
authored
|
36
37
38
|
export type BasicKeys = keyof BasicStore;
type LocalKeys = keyof LocalStore;
type SessionKeys = keyof SessionStore;
|
vben
authored
|
39
|
|
Vben
authored
|
40
41
|
const ls = createLocalStorage();
const ss = createSessionStorage();
|
vben
authored
|
42
|
|
Vben
authored
|
43
44
|
const localMemory = new Memory(DEFAULT_CACHE_TIME);
const sessionMemory = new Memory(DEFAULT_CACHE_TIME);
|
|
45
|
|
Vben
authored
|
46
|
function initPersistentMemory() {
|
Vben
authored
|
47
|
const localCache = ls.get(APP_LOCAL_CACHE_KEY);
|
Vben
authored
|
48
|
const sessionCache = ss.get(APP_SESSION_CACHE_KEY);
|
Vben
authored
|
49
50
|
localCache && localMemory.resetCache(localCache);
sessionCache && sessionMemory.resetCache(sessionCache);
|
|
51
|
}
|
Vben
authored
|
52
|
|
Vben
authored
|
53
54
55
|
export class Persistent {
static getLocal<T>(key: LocalKeys) {
return localMemory.get(key)?.value as Nullable<T>;
|
|
56
|
}
|
vben
authored
|
57
|
|
Vben
authored
|
58
59
60
61
|
static setLocal(key: LocalKeys, value: LocalStore[LocalKeys], immediate = false): void {
localMemory.set(key, toRaw(value));
immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache);
}
|
vben
authored
|
62
|
|
|
63
|
static removeLocal(key: LocalKeys, immediate = false): void {
|
Vben
authored
|
64
|
localMemory.remove(key);
|
|
65
|
immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache);
|
Vben
authored
|
66
|
}
|
vben
authored
|
67
|
|
|
68
|
static clearLocal(immediate = false): void {
|
Vben
authored
|
69
|
localMemory.clear();
|
|
70
|
immediate && ls.clear();
|
vben
authored
|
71
|
}
|
|
72
|
|
Vben
authored
|
73
74
|
static getSession<T>(key: SessionKeys) {
return sessionMemory.get(key)?.value as Nullable<T>;
|
|
75
76
|
}
|
Vben
authored
|
77
78
|
static setSession(key: SessionKeys, value: SessionStore[SessionKeys], immediate = false): void {
sessionMemory.set(key, toRaw(value));
|
Vben
authored
|
79
|
immediate && ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache);
|
|
80
81
|
}
|
|
82
|
static removeSession(key: SessionKeys, immediate = false): void {
|
Vben
authored
|
83
|
sessionMemory.remove(key);
|
|
84
|
immediate && ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache);
|
Vben
authored
|
85
|
}
|
|
86
|
static clearSession(immediate = false): void {
|
Vben
authored
|
87
|
sessionMemory.clear();
|
|
88
|
immediate && ss.clear();
|
Vben
authored
|
89
|
}
|
|
90
|
|
|
91
|
static clearAll(immediate = false) {
|
Vben
authored
|
92
93
|
sessionMemory.clear();
localMemory.clear();
|
|
94
95
96
97
|
if (immediate) {
ls.clear();
ss.clear();
}
|
Vben
authored
|
98
|
}
|
|
99
100
|
}
|
Vben
authored
|
101
|
window.addEventListener('beforeunload', function () {
|
|
102
|
// TOKEN_KEY 在登录或注销时已经写入到storage了,此处为了解决同时打开多个窗口时token不同步的问题
|
|
103
|
// LOCK_INFO_KEY 在锁屏和解锁时写入,此处也不应修改
|
|
104
|
ls.set(APP_LOCAL_CACHE_KEY, {
|
|
105
106
|
...omit(localMemory.getCache, LOCK_INFO_KEY),
...pick(ls.get(APP_LOCAL_CACHE_KEY), [TOKEN_KEY, USER_INFO_KEY, LOCK_INFO_KEY]),
|
|
107
108
|
});
ss.set(APP_SESSION_CACHE_KEY, {
|
|
109
110
|
...omit(sessionMemory.getCache, LOCK_INFO_KEY),
...pick(ss.get(APP_SESSION_CACHE_KEY), [TOKEN_KEY, USER_INFO_KEY, LOCK_INFO_KEY]),
|
|
111
|
});
|
Vben
authored
|
112
|
});
|
vben
authored
|
113
|
|
Vben
authored
|
114
115
|
function storageChange(e: any) {
const { key, newValue, oldValue } = e;
|
|
116
|
|
Vben
authored
|
117
118
119
120
|
if (!key) {
Persistent.clearAll();
return;
}
|
|
121
|
|
Vben
authored
|
122
123
124
|
if (!!newValue && !!oldValue) {
if (APP_LOCAL_CACHE_KEY === key) {
Persistent.clearLocal();
|
|
125
|
}
|
Vben
authored
|
126
127
|
if (APP_SESSION_CACHE_KEY === key) {
Persistent.clearSession();
|
|
128
129
|
}
}
|
Vben
authored
|
130
131
132
|
}
window.addEventListener('storage', storageChange);
|
vben
authored
|
133
|
|
Vben
authored
|
134
|
initPersistentMemory();
|