feat: 字典相关放入auth store 中【b7465498】

pull/62/head
YunaiV 2025-03-26 12:46:57 +08:00
parent ea4892a5f6
commit efa27360a3
2 changed files with 50 additions and 59 deletions

View File

@ -11,6 +11,7 @@ import { useAuthStore, useDictStore } from '#/store';
import { generateAccess } from './access'; import { generateAccess } from './access';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
import { $t } from '@vben/locales'; 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);
// 生成路由表 // 生成路由表
// 当前登录用户拥有的角色标识列表 // 当前登录用户拥有的角色标识列表

View File

@ -1,78 +1,68 @@
import { StorageManager } from '@vben/utils';
import { acceptHMRUpdate, defineStore } from 'pinia'; import { acceptHMRUpdate, defineStore } from 'pinia';
import { getSimpleDictDataList } from '#/api/system/dict/dict.data'; export interface DictItem {
const DICT_STORAGE_KEY = 'DICT_STORAGE__';
interface DictValueType {
value: any;
label: string;
colorType?: string; colorType?: string;
cssClass?: string; cssClass?: string;
label: string;
value: string;
} }
// interface DictTypeType { export type Dict = Record<string, DictItem[]>;
// dictType: string;
// dictValue: DictValueType[];
// }
interface DictState { interface DictState {
dictMap: Map<string, DictValueType[]>; dictCache: Dict;
isSetDict: boolean;
} }
const storage = new StorageManager({
prefix: import.meta.env.VITE_APP_NAMESPACE,
storageType: 'sessionStorage',
});
export const useDictStore = defineStore('dict', { export const useDictStore = defineStore('dict', {
actions: { actions: {
async setDictMap() { getDictData(dictType: string, value: any) {
try { const dict = this.dictCache[dictType];
const dataRes = await getSimpleDictDataList(); if (!dict) {
return undefined;
const dictDataMap = new Map<string, DictValueType[]>();
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);
} }
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<string, any>) => Promise<Record<string, any>[]>,
params: Record<string, any> = {},
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: { persist: {
getDictMap: (state) => state.dictMap, // 持久化
getDictData: (state) => (dictType: string) => { pick: ['dictCache'],
return state.dictMap.get(dictType);
},
getDictOptions: (state) => (dictType: string) => {
return state.dictMap.get(dictType);
},
}, },
persist: [{ pick: ['dictMap', 'isSetDict'] }],
state: (): DictState => ({ state: (): DictState => ({
dictMap: new Map<string, DictValueType[]>(), dictCache: {},
isSetDict: false,
}), }),
}); });