/* eslint-disable */ import { Memory } from './memory' import { createLocalStorage, createSessionStorage } from '@/utils/cache' import { TOKEN_KEY, APP_LOCAL_CACHE_KEY, APP_SESSION_CACHE_KEY, CASCADER_HISTORY_KEY, PERMISSION_SELECT_KEY, MAIN_MENU_KEY, TABLE_COLUMNS_MAP, WORKBENCH_CYCLICAL_KEY, BUSINESS_GROUP_KEY } from './cacheEnum' import { DEFAULT_CACHE_TIME } from '@/settings/encryptionSetting' import { pick, omit } from 'lodash-es' interface BasicStore { [TOKEN_KEY]: string | null | undefined [CASCADER_HISTORY_KEY]: { key: string; value: string[]; label: string }[] | null | underfined [PERMISSION_SELECT_KEY]: { [key in MAIN_MENU_KEY]: string | string[] } | null | underfined [TABLE_COLUMNS_MAP]: { [key in MAIN_MENU_KEY]: Record } | null | underfined [WORKBENCH_CYCLICAL_KEY]: string [BUSINESS_GROUP_KEY]: string } type LocalStore = BasicStore type SessionStore = BasicStore export type BasicKeys = keyof BasicStore type LocalKeys = keyof LocalStore type SessionKeys = keyof SessionStore const ls = createLocalStorage() const ss = createSessionStorage() const localMemory = new Memory(DEFAULT_CACHE_TIME) const sessionMemory = new Memory(DEFAULT_CACHE_TIME) function initPersistentMemory() { const localCache = ls.get(APP_LOCAL_CACHE_KEY) const sessionCache = ss.get(APP_SESSION_CACHE_KEY) localCache && localMemory.resetCache(localCache) sessionCache && sessionMemory.resetCache(sessionCache) } export class Persistent { static getLocal(key: LocalKeys) { return localMemory.get(key)?.value as Nullable } static setLocal(key: LocalKeys, value: LocalStore[LocalKeys], immediate = false): void { localMemory.set(key, value) immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache) } static removeLocal(key: LocalKeys, immediate = false): void { localMemory.remove(key) immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache) } static clearLocal(immediate = false): void { localMemory.clear() immediate && ls.clear() } static getSession(key: SessionKeys) { return sessionMemory.get(key)?.value as Nullable } static setSession(key: SessionKeys, value: SessionStore[SessionKeys], immediate = false): void { sessionMemory.set(key, toRaw(value)) immediate && ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache) } static removeSession(key: SessionKeys, immediate = false): void { sessionMemory.remove(key) immediate && ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache) } static clearSession(immediate = false): void { sessionMemory.clear() immediate && ss.clear() } static clearAll(immediate = false) { sessionMemory.clear() localMemory.clear() if (immediate) { ls.clear() ss.clear() } } } window.addEventListener('beforeunload', function () { // TOKEN_KEY 在登录或注销时已经写入到storage了,此处为了解决同时打开多个窗口时token不同步的问题 // LOCK_INFO_KEY 在锁屏和解锁时写入,此处也不应修改 ls.set(APP_LOCAL_CACHE_KEY, { ...omit(localMemory.getCache), ...pick(ls.get(APP_LOCAL_CACHE_KEY), [TOKEN_KEY]) }) ss.set(APP_SESSION_CACHE_KEY, { ...omit(sessionMemory.getCache), ...pick(ss.get(APP_SESSION_CACHE_KEY), [TOKEN_KEY]) }) }) function storageChange(e: any) { const { key, newValue, oldValue } = e if (!key) { Persistent.clearAll() return } if (!!newValue && !!oldValue) { if (APP_LOCAL_CACHE_KEY === key) { Persistent.clearLocal() } if (APP_SESSION_CACHE_KEY === key) { Persistent.clearSession() } } } window.addEventListener('storage', storageChange) initPersistentMemory()