diff --git a/apps/web-antd/src/router/guard.ts b/apps/web-antd/src/router/guard.ts index 1993426a..be1f1fd9 100644 --- a/apps/web-antd/src/router/guard.ts +++ b/apps/web-antd/src/router/guard.ts @@ -11,6 +11,7 @@ import { useAuthStore, useDictStore } from '#/store'; import { generateAccess } from './access'; import { message } from 'ant-design-vue'; import { $t } from '@vben/locales'; +import { getSimpleDictDataList } from '#/api/system/dict/dict.data'; /** * 通用守卫配置 @@ -94,7 +95,7 @@ function setupAccessGuard(router: Router) { } // 加载字典数据(不阻塞加载) - dictStore.setDictMap(); + dictStore.setDictCacheByApi(getSimpleDictDataList); // 生成路由表 // 当前登录用户拥有的角色标识列表 diff --git a/apps/web-antd/src/store/dict.ts b/apps/web-antd/src/store/dict.ts index ca2825d0..4cc4c886 100644 --- a/apps/web-antd/src/store/dict.ts +++ b/apps/web-antd/src/store/dict.ts @@ -1,78 +1,68 @@ -import { StorageManager } from '@vben/utils'; - import { acceptHMRUpdate, defineStore } from 'pinia'; -import { getSimpleDictDataList } from '#/api/system/dict/dict.data'; - -const DICT_STORAGE_KEY = 'DICT_STORAGE__'; - -interface DictValueType { - value: any; - label: string; +export interface DictItem { colorType?: string; cssClass?: string; + label: string; + value: string; } -// interface DictTypeType { -// dictType: string; -// dictValue: DictValueType[]; -// } +export type Dict = Record; interface DictState { - dictMap: Map; - isSetDict: boolean; + dictCache: Dict; } -const storage = new StorageManager({ - prefix: import.meta.env.VITE_APP_NAMESPACE, - storageType: 'sessionStorage', -}); - export const useDictStore = defineStore('dict', { actions: { - async setDictMap() { - try { - const dataRes = await getSimpleDictDataList(); - - const dictDataMap = new Map(); - - dataRes.forEach((item: any) => { - let dictTypeArray = dictDataMap.get(item.dictType); - if (!dictTypeArray) { - dictTypeArray = []; - } - dictTypeArray.push({ - value: item.value, - label: item.label, - colorType: item.colorType, - cssClass: item.cssClass, - }); - dictDataMap.set(item.dictType, dictTypeArray); - }); - - this.dictMap = dictDataMap; - this.isSetDict = true; - - // 将字典数据存储到 sessionStorage 中 - storage.setItem(DICT_STORAGE_KEY, dictDataMap, 60); - } catch (error) { - console.error('Failed to set dictionary values:', error); + getDictData(dictType: string, value: any) { + const dict = this.dictCache[dictType]; + if (!dict) { + return undefined; } + return ( + dict.find((d) => d.value === value || d.value === value.toString()) ?? + undefined + ); + }, + getDictOptions(dictType: string) { + const dictOptions = this.dictCache[dictType]; + if (!dictOptions) { + return []; + } + return dictOptions; + }, + setDictCache(dicts: Dict) { + this.dictCache = dicts; + }, + setDictCacheByApi( + api: (params: Record) => Promise[]>, + params: Record = {}, + labelField: string = 'label', + valueField: string = 'value', + ) { + api(params).then((dicts) => { + const dictCacheData: Dict = {}; + dicts.forEach((dict) => { + dictCacheData[dict.dictType] = dicts + .filter((d) => d.dictType === dict.dictType) + .map((d) => ({ + colorType: d.colorType, + cssClass: d.cssClass, + label: d[labelField], + value: d[valueField], + })); + }); + this.setDictCache(dictCacheData); + }); }, }, - getters: { - getDictMap: (state) => state.dictMap, - getDictData: (state) => (dictType: string) => { - return state.dictMap.get(dictType); - }, - getDictOptions: (state) => (dictType: string) => { - return state.dictMap.get(dictType); - }, + persist: { + // 持久化 + pick: ['dictCache'], }, - persist: [{ pick: ['dictMap', 'isSetDict'] }], state: (): DictState => ({ - dictMap: new Map(), - isSetDict: false, + dictCache: {}, }), });