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