Blame view

src/utils/cache/persistent.ts 3.78 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,
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;
24
25
26
27
  [USER_INFO_KEY]: UserInfo;
  [ROLES_KEY]: string[];
  [LOCK_INFO_KEY]: LockInfo;
  [PROJ_CFG_KEY]: ProjectConfig;
28
  [MULTIPLE_TABS_KEY]: RouteLocationNormalized[];
sanmu authored
29
  [ORDER_COLUMN]: any;
vben authored
30
31
}
Vben authored
32
type LocalStore = BasicStore;
33
Vben authored
34
type SessionStore = BasicStore;
陈文彬 authored
35
Vben authored
36
37
38
export type BasicKeys = keyof BasicStore;
type LocalKeys = keyof LocalStore;
type SessionKeys = keyof SessionStore;
39
40
41
const ls = createLocalStorage();
const ss = createSessionStorage();
42
Vben authored
43
44
const localMemory = new Memory(DEFAULT_CACHE_TIME);
const sessionMemory = new Memory(DEFAULT_CACHE_TIME);
陈文彬 authored
45
46
function initPersistentMemory() {
Vben authored
47
  const localCache = ls.get(APP_LOCAL_CACHE_KEY);
48
  const sessionCache = ss.get(APP_SESSION_CACHE_KEY);
Vben authored
49
50
  localCache && localMemory.resetCache(localCache);
  sessionCache && sessionMemory.resetCache(sessionCache);
陈文彬 authored
51
}
52
Vben authored
53
54
55
export class Persistent {
  static getLocal<T>(key: LocalKeys) {
    return localMemory.get(key)?.value as Nullable<T>;
陈文彬 authored
56
  }
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);
  }
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
  }
67
68
  static clearLocal(immediate = false): void {
Vben authored
69
    localMemory.clear();
70
    immediate && ls.clear();
71
  }
陈文彬 authored
72
Vben authored
73
74
  static getSession<T>(key: SessionKeys) {
    return sessionMemory.get(key)?.value as Nullable<T>;
陈文彬 authored
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);
陈文彬 authored
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
  }
陈文彬 authored
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
  }
陈文彬 authored
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
});
113
Vben authored
114
115
function storageChange(e: any) {
  const { key, newValue, oldValue } = e;
陈文彬 authored
116
Vben authored
117
118
119
120
  if (!key) {
    Persistent.clearAll();
    return;
  }
陈文彬 authored
121
Vben authored
122
123
124
  if (!!newValue && !!oldValue) {
    if (APP_LOCAL_CACHE_KEY === key) {
      Persistent.clearLocal();
陈文彬 authored
125
    }
Vben authored
126
127
    if (APP_SESSION_CACHE_KEY === key) {
      Persistent.clearSession();
陈文彬 authored
128
129
    }
  }
Vben authored
130
131
132
}

window.addEventListener('storage', storageChange);
133
134
initPersistentMemory();