Commit efcbd1ab68a49502d27cb8fc33bf0b1897f9c724

Authored by xingyu
1 parent 2dd1e011

perf: 优化字典store

yudao-ui-admin-vue3/src/main.ts
@@ -26,10 +26,10 @@ import '@/styles/index.scss' @@ -26,10 +26,10 @@ import '@/styles/index.scss'
26 import '@/plugins/animate.css' 26 import '@/plugins/animate.css'
27 27
28 // 路由 28 // 路由
29 -import { setupRouter } from './router' 29 +import router, { setupRouter } from '@/router'
30 30
31 // 权限 31 // 权限
32 -import { setupAuth } from './directives' 32 +import { setupAuth } from '@/directives'
33 33
34 import { createApp } from 'vue' 34 import { createApp } from 'vue'
35 35
@@ -53,6 +53,8 @@ const setupAll = async () => { @@ -53,6 +53,8 @@ const setupAll = async () => {
53 53
54 setupAuth(app) 54 setupAuth(app)
55 55
  56 + await router.isReady()
  57 +
56 app.mount('#app') 58 app.mount('#app')
57 } 59 }
58 60
yudao-ui-admin-vue3/src/router/index.ts
@@ -6,15 +6,11 @@ import { isRelogin } from '@/config/axios/service' @@ -6,15 +6,11 @@ import { isRelogin } from '@/config/axios/service'
6 import { getAccessToken } from '@/utils/auth' 6 import { getAccessToken } from '@/utils/auth'
7 import { useTitle } from '@/hooks/web/useTitle' 7 import { useTitle } from '@/hooks/web/useTitle'
8 import { useNProgress } from '@/hooks/web/useNProgress' 8 import { useNProgress } from '@/hooks/web/useNProgress'
9 -import { CACHE_KEY, useCache } from '@/hooks/web/useCache'  
10 import { usePageLoading } from '@/hooks/web/usePageLoading' 9 import { usePageLoading } from '@/hooks/web/usePageLoading'
11 import { useDictStoreWithOut } from '@/store/modules/dict' 10 import { useDictStoreWithOut } from '@/store/modules/dict'
12 import { useUserStoreWithOut } from '@/store/modules/user' 11 import { useUserStoreWithOut } from '@/store/modules/user'
13 import { usePermissionStoreWithOut } from '@/store/modules/permission' 12 import { usePermissionStoreWithOut } from '@/store/modules/permission'
14 import { getInfoApi } from '@/api/login' 13 import { getInfoApi } from '@/api/login'
15 -import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'  
16 -  
17 -const { wsCache } = useCache()  
18 14
19 const { start, done } = useNProgress() 15 const { start, done } = useNProgress()
20 16
@@ -50,12 +46,11 @@ router.beforeEach(async (to, from, next) => { @@ -50,12 +46,11 @@ router.beforeEach(async (to, from, next) => {
50 const dictStore = useDictStoreWithOut() 46 const dictStore = useDictStoreWithOut()
51 const userStore = useUserStoreWithOut() 47 const userStore = useUserStoreWithOut()
52 const permissionStore = usePermissionStoreWithOut() 48 const permissionStore = usePermissionStoreWithOut()
53 - const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)  
54 - if (!dictMap) {  
55 - const res = await listSimpleDictDataApi()  
56 - dictStore.setDictMap(res) 49 + if (!dictStore.getIsSetDict) {
  50 + dictStore.setDictMap()
57 } 51 }
58 if (!userStore.getIsSetUser) { 52 if (!userStore.getIsSetUser) {
  53 + console.info(1)
59 isRelogin.show = true 54 isRelogin.show = true
60 const res = await getInfoApi() 55 const res = await getInfoApi()
61 await userStore.setUserInfoAction(res) 56 await userStore.setUserInfoAction(res)
yudao-ui-admin-vue3/src/store/modules/dict.ts
@@ -3,6 +3,7 @@ import { store } from '../index' @@ -3,6 +3,7 @@ import { store } from '../index'
3 import { DictDataVO } from '@/api/system/dict/types' 3 import { DictDataVO } from '@/api/system/dict/types'
4 import { CACHE_KEY, useCache } from '@/hooks/web/useCache' 4 import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
5 const { wsCache } = useCache('sessionStorage') 5 const { wsCache } = useCache('sessionStorage')
  6 +import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'
6 7
7 export interface DictValueType { 8 export interface DictValueType {
8 value: any 9 value: any
@@ -16,45 +17,54 @@ export interface DictTypeType { @@ -16,45 +17,54 @@ export interface DictTypeType {
16 } 17 }
17 export interface DictState { 18 export interface DictState {
18 dictMap: Map<string, any> 19 dictMap: Map<string, any>
  20 + isSetDict: boolean
19 } 21 }
20 22
21 export const useDictStore = defineStore('dict', { 23 export const useDictStore = defineStore('dict', {
22 state: (): DictState => ({ 24 state: (): DictState => ({
23 - dictMap: new Map<string, any>() 25 + dictMap: new Map<string, any>(),
  26 + isSetDict: false
24 }), 27 }),
25 getters: { 28 getters: {
26 getDictMap(): Recordable { 29 getDictMap(): Recordable {
27 const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE) 30 const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
28 - return dictMap ? dictMap : this.dictMap  
29 - },  
30 - getHasDictData(): boolean {  
31 - if (this.dictMap.size > 0) {  
32 - return true  
33 - } else {  
34 - return false 31 + if (dictMap) {
  32 + this.dictMap = dictMap
35 } 33 }
  34 + return this.dictMap
  35 + },
  36 + getIsSetDict(): boolean {
  37 + return this.isSetDict
36 } 38 }
37 }, 39 },
38 actions: { 40 actions: {
39 - setDictMap(dictMap: Recordable) {  
40 - // 设置数据  
41 - const dictDataMap = new Map<string, any>()  
42 - dictMap.forEach((dictData: DictDataVO) => {  
43 - // 获得 dictType 层级  
44 - const enumValueObj = dictDataMap[dictData.dictType]  
45 - if (!enumValueObj) {  
46 - dictDataMap[dictData.dictType] = []  
47 - }  
48 - // 处理 dictValue 层级  
49 - dictDataMap[dictData.dictType].push({  
50 - value: dictData.value,  
51 - label: dictData.label,  
52 - colorType: dictData.colorType,  
53 - cssClass: dictData.cssClass 41 + async setDictMap() {
  42 + const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
  43 + if (dictMap) {
  44 + this.dictMap = dictMap
  45 + this.isSetDict = true
  46 + } else {
  47 + const res = await listSimpleDictDataApi()
  48 + // 设置数据
  49 + const dictDataMap = new Map<string, any>()
  50 + res.forEach((dictData: DictDataVO) => {
  51 + // 获得 dictType 层级
  52 + const enumValueObj = dictDataMap[dictData.dictType]
  53 + if (!enumValueObj) {
  54 + dictDataMap[dictData.dictType] = []
  55 + }
  56 + // 处理 dictValue 层级
  57 + dictDataMap[dictData.dictType].push({
  58 + value: dictData.value,
  59 + label: dictData.label,
  60 + colorType: dictData.colorType,
  61 + cssClass: dictData.cssClass
  62 + })
54 }) 63 })
55 - })  
56 - this.dictMap = dictDataMap  
57 - wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期 64 + this.dictMap = dictDataMap
  65 + this.isSetDict = true
  66 + wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
  67 + }
58 } 68 }
59 } 69 }
60 }) 70 })