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