perf: 优化字典store

This commit is contained in:
xingyu 2023-01-05 14:52:14 +08:00
parent 2dd1e01183
commit efcbd1ab68
3 changed files with 43 additions and 36 deletions

View File

@ -26,10 +26,10 @@ import '@/styles/index.scss'
import '@/plugins/animate.css'
// 路由
import { setupRouter } from './router'
import router, { setupRouter } from '@/router'
// 权限
import { setupAuth } from './directives'
import { setupAuth } from '@/directives'
import { createApp } from 'vue'
@ -53,6 +53,8 @@ const setupAll = async () => {
setupAuth(app)
await router.isReady()
app.mount('#app')
}

View File

@ -6,15 +6,11 @@ import { isRelogin } from '@/config/axios/service'
import { getAccessToken } from '@/utils/auth'
import { useTitle } from '@/hooks/web/useTitle'
import { useNProgress } from '@/hooks/web/useNProgress'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { usePageLoading } from '@/hooks/web/usePageLoading'
import { useDictStoreWithOut } from '@/store/modules/dict'
import { useUserStoreWithOut } from '@/store/modules/user'
import { usePermissionStoreWithOut } from '@/store/modules/permission'
import { getInfoApi } from '@/api/login'
import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'
const { wsCache } = useCache()
const { start, done } = useNProgress()
@ -50,12 +46,11 @@ router.beforeEach(async (to, from, next) => {
const dictStore = useDictStoreWithOut()
const userStore = useUserStoreWithOut()
const permissionStore = usePermissionStoreWithOut()
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
if (!dictMap) {
const res = await listSimpleDictDataApi()
dictStore.setDictMap(res)
if (!dictStore.getIsSetDict) {
dictStore.setDictMap()
}
if (!userStore.getIsSetUser) {
console.info(1)
isRelogin.show = true
const res = await getInfoApi()
await userStore.setUserInfoAction(res)

View File

@ -3,6 +3,7 @@ import { store } from '../index'
import { DictDataVO } from '@/api/system/dict/types'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache('sessionStorage')
import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'
export interface DictValueType {
value: any
@ -16,45 +17,54 @@ export interface DictTypeType {
}
export interface DictState {
dictMap: Map<string, any>
isSetDict: boolean
}
export const useDictStore = defineStore('dict', {
state: (): DictState => ({
dictMap: new Map<string, any>()
dictMap: new Map<string, any>(),
isSetDict: false
}),
getters: {
getDictMap(): Recordable {
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
return dictMap ? dictMap : this.dictMap
},
getHasDictData(): boolean {
if (this.dictMap.size > 0) {
return true
} else {
return false
if (dictMap) {
this.dictMap = dictMap
}
return this.dictMap
},
getIsSetDict(): boolean {
return this.isSetDict
}
},
actions: {
setDictMap(dictMap: Recordable) {
// 设置数据
const dictDataMap = new Map<string, any>()
dictMap.forEach((dictData: DictDataVO) => {
// 获得 dictType 层级
const enumValueObj = dictDataMap[dictData.dictType]
if (!enumValueObj) {
dictDataMap[dictData.dictType] = []
}
// 处理 dictValue 层级
dictDataMap[dictData.dictType].push({
value: dictData.value,
label: dictData.label,
colorType: dictData.colorType,
cssClass: dictData.cssClass
async setDictMap() {
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
if (dictMap) {
this.dictMap = dictMap
this.isSetDict = true
} else {
const res = await listSimpleDictDataApi()
// 设置数据
const dictDataMap = new Map<string, any>()
res.forEach((dictData: DictDataVO) => {
// 获得 dictType 层级
const enumValueObj = dictDataMap[dictData.dictType]
if (!enumValueObj) {
dictDataMap[dictData.dictType] = []
}
// 处理 dictValue 层级
dictDataMap[dictData.dictType].push({
value: dictData.value,
label: dictData.label,
colorType: dictData.colorType,
cssClass: dictData.cssClass
})
})
})
this.dictMap = dictDataMap
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
this.dictMap = dictDataMap
this.isSetDict = true
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
}
}
}
})