perf: 优化字典store
parent
2dd1e01183
commit
efcbd1ab68
|
@ -26,10 +26,10 @@ import '@/styles/index.scss'
|
||||||
import '@/plugins/animate.css'
|
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'
|
import { createApp } from 'vue'
|
||||||
|
|
||||||
|
@ -53,6 +53,8 @@ const setupAll = async () => {
|
||||||
|
|
||||||
setupAuth(app)
|
setupAuth(app)
|
||||||
|
|
||||||
|
await router.isReady()
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,15 +6,11 @@ import { isRelogin } from '@/config/axios/service'
|
||||||
import { getAccessToken } from '@/utils/auth'
|
import { getAccessToken } from '@/utils/auth'
|
||||||
import { useTitle } from '@/hooks/web/useTitle'
|
import { useTitle } from '@/hooks/web/useTitle'
|
||||||
import { useNProgress } from '@/hooks/web/useNProgress'
|
import { useNProgress } from '@/hooks/web/useNProgress'
|
||||||
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
|
|
||||||
import { usePageLoading } from '@/hooks/web/usePageLoading'
|
import { usePageLoading } from '@/hooks/web/usePageLoading'
|
||||||
import { useDictStoreWithOut } from '@/store/modules/dict'
|
import { useDictStoreWithOut } from '@/store/modules/dict'
|
||||||
import { useUserStoreWithOut } from '@/store/modules/user'
|
import { useUserStoreWithOut } from '@/store/modules/user'
|
||||||
import { usePermissionStoreWithOut } from '@/store/modules/permission'
|
import { usePermissionStoreWithOut } from '@/store/modules/permission'
|
||||||
import { getInfoApi } from '@/api/login'
|
import { getInfoApi } from '@/api/login'
|
||||||
import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'
|
|
||||||
|
|
||||||
const { wsCache } = useCache()
|
|
||||||
|
|
||||||
const { start, done } = useNProgress()
|
const { start, done } = useNProgress()
|
||||||
|
|
||||||
|
@ -50,12 +46,11 @@ router.beforeEach(async (to, from, next) => {
|
||||||
const dictStore = useDictStoreWithOut()
|
const dictStore = useDictStoreWithOut()
|
||||||
const userStore = useUserStoreWithOut()
|
const userStore = useUserStoreWithOut()
|
||||||
const permissionStore = usePermissionStoreWithOut()
|
const permissionStore = usePermissionStoreWithOut()
|
||||||
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
|
if (!dictStore.getIsSetDict) {
|
||||||
if (!dictMap) {
|
dictStore.setDictMap()
|
||||||
const res = await listSimpleDictDataApi()
|
|
||||||
dictStore.setDictMap(res)
|
|
||||||
}
|
}
|
||||||
if (!userStore.getIsSetUser) {
|
if (!userStore.getIsSetUser) {
|
||||||
|
console.info(1)
|
||||||
isRelogin.show = true
|
isRelogin.show = true
|
||||||
const res = await getInfoApi()
|
const res = await getInfoApi()
|
||||||
await userStore.setUserInfoAction(res)
|
await userStore.setUserInfoAction(res)
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { store } from '../index'
|
||||||
import { DictDataVO } from '@/api/system/dict/types'
|
import { DictDataVO } from '@/api/system/dict/types'
|
||||||
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
|
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
|
||||||
const { wsCache } = useCache('sessionStorage')
|
const { wsCache } = useCache('sessionStorage')
|
||||||
|
import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'
|
||||||
|
|
||||||
export interface DictValueType {
|
export interface DictValueType {
|
||||||
value: any
|
value: any
|
||||||
|
@ -16,30 +17,37 @@ export interface DictTypeType {
|
||||||
}
|
}
|
||||||
export interface DictState {
|
export interface DictState {
|
||||||
dictMap: Map<string, any>
|
dictMap: Map<string, any>
|
||||||
|
isSetDict: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useDictStore = defineStore('dict', {
|
export const useDictStore = defineStore('dict', {
|
||||||
state: (): DictState => ({
|
state: (): DictState => ({
|
||||||
dictMap: new Map<string, any>()
|
dictMap: new Map<string, any>(),
|
||||||
|
isSetDict: false
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
getDictMap(): Recordable {
|
getDictMap(): Recordable {
|
||||||
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
|
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
|
||||||
return dictMap ? dictMap : this.dictMap
|
if (dictMap) {
|
||||||
},
|
this.dictMap = dictMap
|
||||||
getHasDictData(): boolean {
|
|
||||||
if (this.dictMap.size > 0) {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return this.dictMap
|
||||||
|
},
|
||||||
|
getIsSetDict(): boolean {
|
||||||
|
return this.isSetDict
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
setDictMap(dictMap: Recordable) {
|
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>()
|
const dictDataMap = new Map<string, any>()
|
||||||
dictMap.forEach((dictData: DictDataVO) => {
|
res.forEach((dictData: DictDataVO) => {
|
||||||
// 获得 dictType 层级
|
// 获得 dictType 层级
|
||||||
const enumValueObj = dictDataMap[dictData.dictType]
|
const enumValueObj = dictDataMap[dictData.dictType]
|
||||||
if (!enumValueObj) {
|
if (!enumValueObj) {
|
||||||
|
@ -54,9 +62,11 @@ export const useDictStore = defineStore('dict', {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
this.dictMap = dictDataMap
|
this.dictMap = dictDataMap
|
||||||
|
this.isSetDict = true
|
||||||
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
|
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export const useDictStoreWithOut = () => {
|
export const useDictStoreWithOut = () => {
|
||||||
|
|
Loading…
Reference in New Issue