mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-01 02:38:43 +08:00 
			
		
		
		
	perf: 优化字典store
This commit is contained in:
		| @@ -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 秒 过期 | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| }) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 xingyu
					xingyu