From 1d1e70ee6690d2e3c00952b87a18c1c2b5c952fe Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 25 Mar 2025 22:51:51 +0800 Subject: [PATCH] =?UTF-8?q?Merge=20branch=20'dev-v5'=20of=20https://gitee.?= =?UTF-8?q?com/yudaocode/yudao-ui-admin-vben=20into=20dev-v5=E3=80=9078806?= =?UTF-8?q?1ad=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/api/request.ts | 1 + apps/web-antd/src/router/guard.ts | 6 +- apps/web-antd/src/store/dict.ts | 107 ++++++++++++++++-------------- 3 files changed, 64 insertions(+), 50 deletions(-) diff --git a/apps/web-antd/src/api/request.ts b/apps/web-antd/src/api/request.ts index 3d1892332..5ce3afc68 100644 --- a/apps/web-antd/src/api/request.ts +++ b/apps/web-antd/src/api/request.ts @@ -50,6 +50,7 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { */ async function doRefreshToken() { const accessStore = useAccessStore(); + debugger const resp = await refreshTokenApi(); const newToken = resp.data; accessStore.setAccessToken(newToken); diff --git a/apps/web-antd/src/router/guard.ts b/apps/web-antd/src/router/guard.ts index 9e464ada6..1993426a2 100644 --- a/apps/web-antd/src/router/guard.ts +++ b/apps/web-antd/src/router/guard.ts @@ -6,7 +6,7 @@ import { useAccessStore, useUserStore } from '@vben/stores'; import { startProgress, stopProgress } from '@vben/utils'; import { accessRoutes, coreRouteNames } from '#/router/routes'; -import { useAuthStore } from '#/store'; +import { useAuthStore, useDictStore } from '#/store'; import { generateAccess } from './access'; import { message } from 'ant-design-vue'; @@ -51,6 +51,7 @@ function setupAccessGuard(router: Router) { const accessStore = useAccessStore(); const userStore = useUserStore(); const authStore = useAuthStore(); + const dictStore = useDictStore(); // 基本路由,这些路由不需要进入权限拦截 if (coreRouteNames.includes(to.name as string)) { @@ -92,6 +93,9 @@ function setupAccessGuard(router: Router) { return true; } + // 加载字典数据(不阻塞加载) + dictStore.setDictMap(); + // 生成路由表 // 当前登录用户拥有的角色标识列表 let userInfo = userStore.userInfo; diff --git a/apps/web-antd/src/store/dict.ts b/apps/web-antd/src/store/dict.ts index 52a44ad8e..ca2825d08 100644 --- a/apps/web-antd/src/store/dict.ts +++ b/apps/web-antd/src/store/dict.ts @@ -1,69 +1,78 @@ -// TODO @芋艿:【可优化】挪到 packages/stores/src/modules +import { StorageManager } from '@vben/utils'; + import { acceptHMRUpdate, defineStore } from 'pinia'; -export interface DictItem { +import { getSimpleDictDataList } from '#/api/system/dict/dict.data'; + +const DICT_STORAGE_KEY = 'DICT_STORAGE__'; + +interface DictValueType { + value: any; + label: string; colorType?: string; cssClass?: string; - label: string; - value: string; } -export type Dict = Record; +// interface DictTypeType { +// dictType: string; +// dictValue: DictValueType[]; +// } interface DictState { - dictCache: Dict; + dictMap: Map; + isSetDict: boolean; } +const storage = new StorageManager({ + prefix: import.meta.env.VITE_APP_NAMESPACE, + storageType: 'sessionStorage', +}); + export const useDictStore = defineStore('dict', { actions: { - 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], - })); + 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.setDictCache(dictCacheData); - }); + + this.dictMap = dictDataMap; + this.isSetDict = true; + + // 将字典数据存储到 sessionStorage 中 + storage.setItem(DICT_STORAGE_KEY, dictDataMap, 60); + } catch (error) { + console.error('Failed to set dictionary values:', error); + } }, }, - persist: { - // 持久化 - pick: ['dictCache'], + 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: ['dictMap', 'isSetDict'] }], state: (): DictState => ({ - dictCache: {}, + dictMap: new Map(), + isSetDict: false, }), });