From efa27360a323bb261f391344d7cfe0d3d476f661 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 26 Mar 2025 12:46:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AD=97=E5=85=B8=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=94=BE=E5=85=A5auth=20store=20=E4=B8=AD=E3=80=90b7465498?= =?UTF-8?q?=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/router/guard.ts | 3 +- apps/web-antd/src/store/dict.ts | 106 ++++++++++++++---------------- 2 files changed, 50 insertions(+), 59 deletions(-) diff --git a/apps/web-antd/src/router/guard.ts b/apps/web-antd/src/router/guard.ts index 1993426a2..be1f1fd9a 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 ca2825d08..4cc4c8865 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: {}, }), });