feat: add cache key

This commit is contained in:
xingyu4j
2022-12-06 16:12:54 +08:00
parent c8c6e349ae
commit 3e60ff2970
18 changed files with 100 additions and 81 deletions

View File

@ -2,7 +2,7 @@ import { defineStore } from 'pinia'
import { store } from '../index'
import { setCssVar, humpToUnderline } from '@/utils'
import { ElMessage } from 'element-plus'
import { useCache } from '@/hooks/web/useCache'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { ElementPlusSize } from '@/types/elementPlus'
import { LayoutType } from '@/types/layout'
import { ThemeTypes } from '@/types/theme'
@ -61,10 +61,10 @@ export const useAppStore = defineStore('app', {
greyMode: false, // 是否开始灰色模式,用于特殊悼念日
fixedMenu: wsCache.get('fixedMenu') || false, // 是否固定菜单
layout: wsCache.get('layout') || 'classic', // layout布局
isDark: wsCache.get('isDark') || false, // 是否是暗黑模式
layout: wsCache.get(CACHE_KEY.LAYOUT) || 'classic', // layout布局
isDark: wsCache.get(CACHE_KEY.IS_DARK) || false, // 是否是暗黑模式
currentSize: wsCache.get('default') || 'default', // 组件尺寸
theme: wsCache.get('theme') || {
theme: wsCache.get(CACHE_KEY.THEME) || {
// 主题色
elColorPrimary: '#409eff',
// 左侧菜单边框颜色
@ -223,7 +223,7 @@ export const useAppStore = defineStore('app', {
return
}
this.layout = layout
wsCache.set('layout', this.layout)
wsCache.set(CACHE_KEY.THEME, this.layout)
},
setTitle(title: string) {
this.title = title
@ -237,7 +237,7 @@ export const useAppStore = defineStore('app', {
document.documentElement.classList.add('light')
document.documentElement.classList.remove('dark')
}
wsCache.set('isDark', this.isDark)
wsCache.set(CACHE_KEY.IS_DARK, this.isDark)
},
setCurrentSize(currentSize: ElementPlusSize) {
this.currentSize = currentSize
@ -248,7 +248,7 @@ export const useAppStore = defineStore('app', {
},
setTheme(theme: ThemeTypes) {
this.theme = Object.assign(this.theme, theme)
wsCache.set('theme', this.theme)
wsCache.set(CACHE_KEY.THEME, this.theme)
},
setCssVarTheme() {
for (const key in this.theme) {

View File

@ -1,7 +1,7 @@
import { defineStore } from 'pinia'
import { store } from '../index'
import { DictDataVO } from '@/api/system/dict/types'
import { useCache } from '@/hooks/web/useCache'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache('sessionStorage')
export interface DictValueType {
@ -24,7 +24,7 @@ export const useDictStore = defineStore('dict', {
}),
getters: {
getDictMap(): Recordable {
const dictMap = wsCache.get('dictCache')
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
return dictMap ? dictMap : this.dictMap
},
getHasDictData(): boolean {
@ -54,7 +54,7 @@ export const useDictStore = defineStore('dict', {
})
})
this.dictMap = dictDataMap
wsCache.set('dictCache', dictDataMap, { exp: 60 }) // 60 秒 过期
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
}
}
})

View File

@ -2,7 +2,7 @@ import { defineStore } from 'pinia'
import { store } from '../index'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import en from 'element-plus/es/locale/lang/en'
import { useCache } from '@/hooks/web/useCache'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { LocaleDropdownType } from '@/types/localeDropdown'
const { wsCache } = useCache()
@ -20,8 +20,8 @@ export const useLocaleStore = defineStore('locales', {
state: (): LocaleState => {
return {
currentLocale: {
lang: wsCache.get('lang') || 'zh-CN',
elLocale: elLocaleMap[wsCache.get('lang') || 'zh-CN']
lang: wsCache.get(CACHE_KEY.LANG) || 'zh-CN',
elLocale: elLocaleMap[wsCache.get(CACHE_KEY.LANG) || 'zh-CN']
},
// 多语言
localeMap: [
@ -49,7 +49,7 @@ export const useLocaleStore = defineStore('locales', {
// this.locale = Object.assign(this.locale, localeMap)
this.currentLocale.lang = localeMap?.lang
this.currentLocale.elLocale = elLocaleMap[localeMap?.lang]
wsCache.set('lang', localeMap?.lang)
wsCache.set(CACHE_KEY.LANG, localeMap?.lang)
}
}
})

View File

@ -4,7 +4,7 @@ import { cloneDeep } from 'lodash-es'
import remainingRouter from '@/router/modules/remaining'
import { generateRoute, flatMultiLevelRoutes } from '@/utils/routerHelper'
import { getAsyncRoutesApi } from '@/api/login'
import { useCache } from '@/hooks/web/useCache'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
@ -35,11 +35,11 @@ export const usePermissionStore = defineStore('permission', {
async generateRoutes(): Promise<unknown> {
return new Promise<void>(async (resolve) => {
let res: AppCustomRouteRecordRaw[]
if (wsCache.get('roleRouters')) {
res = wsCache.get('roleRouters') as AppCustomRouteRecordRaw[]
if (wsCache.get(CACHE_KEY.ROLE_ROUTERS)) {
res = wsCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[]
} else {
res = await getAsyncRoutesApi()
wsCache.set('roleRouters', res)
wsCache.set(CACHE_KEY.ROLE_ROUTERS, res)
}
const routerMap: AppRouteRecordRaw[] = generateRoute(res as AppCustomRouteRecordRaw[])
// 动态路由404一定要放到最后面

View File

@ -1,7 +1,7 @@
import { store } from '../index'
import { defineStore } from 'pinia'
import { getAccessToken, removeToken } from '@/utils/auth'
import { useCache } from '@/hooks/web/useCache'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
@ -46,7 +46,7 @@ export const useUserStore = defineStore('admin-user', {
this.permissions = userInfo.permissions
this.roles = userInfo.roles
this.user = userInfo.user
wsCache.set('user', userInfo)
wsCache.set(CACHE_KEY.USER, userInfo)
},
loginOut() {
removeToken()