persistent.ts 2.7 KB
Newer Older
V
Vben 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import { createPersistentStorage } from '/@/utils/cache';
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,
} from '/@/enums/cacheEnum';
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
import { toRaw } from 'vue';

interface BasicStore {
  [TOKEN_KEY]: string | number | null | undefined;
  [USER_INFO_KEY]: Recordable;
  [ROLES_KEY]: Recordable;
  [LOCK_INFO_KEY]: Recordable;
  [PROJ_CFG_KEY]: Recordable;
V
vben 已提交
21 22
}

V
Vben 已提交
23
type LocalStore = BasicStore;
V
vben 已提交
24

V
Vben 已提交
25
type SessionStore = BasicStore;
陈文彬 已提交
26

V
Vben 已提交
27 28 29
export type BasicKeys = keyof BasicStore;
type LocalKeys = keyof LocalStore;
type SessionKeys = keyof SessionStore;
V
vben 已提交
30

V
Vben 已提交
31 32
const ls = createPersistentStorage(localStorage);
const ss = createPersistentStorage(sessionStorage);
V
vben 已提交
33

V
Vben 已提交
34 35
const localMemory = new Memory(DEFAULT_CACHE_TIME);
const sessionMemory = new Memory(DEFAULT_CACHE_TIME);
陈文彬 已提交
36

V
Vben 已提交
37 38 39 40 41
function initMemory() {
  const localCache = ls.get(APP_LOCAL_CACHE_KEY);
  const sessionCache = ls.get(APP_SESSION_CACHE_KEY);
  localCache && localMemory.resetCache(localCache);
  sessionCache && sessionMemory.resetCache(sessionCache);
陈文彬 已提交
42
}
V
Vben 已提交
43 44 45 46
initMemory();
export class Persistent {
  static getLocal<T>(key: LocalKeys) {
    return localMemory.get(key)?.value as Nullable<T>;
陈文彬 已提交
47
  }
V
vben 已提交
48

V
Vben 已提交
49 50 51 52
  static setLocal(key: LocalKeys, value: LocalStore[LocalKeys], immediate = false): void {
    localMemory.set(key, toRaw(value));
    immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache);
  }
V
vben 已提交
53

V
Vben 已提交
54 55 56
  static removeLocal(key: LocalKeys): void {
    localMemory.remove(key);
  }
V
vben 已提交
57

V
Vben 已提交
58 59
  static clearLocal(): void {
    localMemory.clear();
60
  }
陈文彬 已提交
61

V
Vben 已提交
62 63
  static getSession<T>(key: SessionKeys) {
    return sessionMemory.get(key)?.value as Nullable<T>;
陈文彬 已提交
64 65
  }

V
Vben 已提交
66 67 68
  static setSession(key: SessionKeys, value: SessionStore[SessionKeys], immediate = false): void {
    sessionMemory.set(key, toRaw(value));
    immediate && ss.set(APP_SESSION_CACHE_KEY, localMemory);
陈文彬 已提交
69 70
  }

V
Vben 已提交
71 72 73 74 75 76
  static removeSession(key: SessionKeys): void {
    sessionMemory.remove(key);
  }
  static clearSession(): void {
    sessionMemory.clear();
  }
陈文彬 已提交
77

V
Vben 已提交
78 79 80 81
  static clearAll() {
    sessionMemory.clear();
    localMemory.clear();
  }
陈文彬 已提交
82 83
}

V
Vben 已提交
84 85 86 87
window.addEventListener('beforeunload', function () {
  ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache);
  ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache);
});
V
vben 已提交
88

V
Vben 已提交
89 90
function storageChange(e: any) {
  const { key, newValue, oldValue } = e;
陈文彬 已提交
91

V
Vben 已提交
92 93 94 95
  if (!key) {
    Persistent.clearAll();
    return;
  }
陈文彬 已提交
96

V
Vben 已提交
97 98 99
  if (!!newValue && !!oldValue) {
    if (APP_LOCAL_CACHE_KEY === key) {
      Persistent.clearLocal();
陈文彬 已提交
100
    }
V
Vben 已提交
101 102
    if (APP_SESSION_CACHE_KEY === key) {
      Persistent.clearSession();
陈文彬 已提交
103 104
    }
  }
V
Vben 已提交
105 106 107
}

window.addEventListener('storage', storageChange);
V
vben 已提交
108

V
Vben 已提交
109
export default {};