Blame view

src/utils/cache/persistent.ts 3.74 KB
Vben authored
1
2
import type { LockInfo, UserInfo } from '/#/store';
import type { ProjectConfig } from '/#/config';
3
import type { RouteLocationNormalized } from 'vue-router';
4
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,
15
  MULTIPLE_TABS_KEY,
Vben authored
16
17
18
} from '/@/enums/cacheEnum';
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
import { toRaw } from 'vue';
19
import { pick, omit } from 'lodash-es';
Vben authored
20
21
22

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

window.addEventListener('storage', storageChange);
131
132
initPersistentMemory();