2025-03-25 22:51:51 +08:00
|
|
|
import { StorageManager } from '@vben/utils';
|
|
|
|
|
|
2025-03-25 22:30:22 +08:00
|
|
|
import { acceptHMRUpdate, defineStore } from 'pinia';
|
|
|
|
|
|
2025-03-25 22:51:51 +08:00
|
|
|
import { getSimpleDictDataList } from '#/api/system/dict/dict.data';
|
|
|
|
|
|
|
|
|
|
const DICT_STORAGE_KEY = 'DICT_STORAGE__';
|
|
|
|
|
|
|
|
|
|
interface DictValueType {
|
|
|
|
|
value: any;
|
|
|
|
|
label: string;
|
2025-03-25 22:30:22 +08:00
|
|
|
colorType?: string;
|
|
|
|
|
cssClass?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-25 22:51:51 +08:00
|
|
|
// interface DictTypeType {
|
|
|
|
|
// dictType: string;
|
|
|
|
|
// dictValue: DictValueType[];
|
|
|
|
|
// }
|
2025-03-25 22:30:22 +08:00
|
|
|
|
|
|
|
|
interface DictState {
|
2025-03-25 22:51:51 +08:00
|
|
|
dictMap: Map<string, DictValueType[]>;
|
|
|
|
|
isSetDict: boolean;
|
2025-03-25 22:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-25 22:51:51 +08:00
|
|
|
const storage = new StorageManager({
|
|
|
|
|
prefix: import.meta.env.VITE_APP_NAMESPACE,
|
|
|
|
|
storageType: 'sessionStorage',
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-25 22:30:22 +08:00
|
|
|
export const useDictStore = defineStore('dict', {
|
|
|
|
|
actions: {
|
2025-03-25 22:51:51 +08:00
|
|
|
async setDictMap() {
|
|
|
|
|
try {
|
|
|
|
|
const dataRes = await getSimpleDictDataList();
|
|
|
|
|
|
|
|
|
|
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);
|
2025-03-25 22:30:22 +08:00
|
|
|
}
|
|
|
|
|
},
|
2025-03-25 22:51:51 +08:00
|
|
|
},
|
|
|
|
|
getters: {
|
|
|
|
|
getDictMap: (state) => state.dictMap,
|
|
|
|
|
getDictData: (state) => (dictType: string) => {
|
|
|
|
|
return state.dictMap.get(dictType);
|
2025-03-25 22:30:22 +08:00
|
|
|
},
|
2025-03-25 22:51:51 +08:00
|
|
|
getDictOptions: (state) => (dictType: string) => {
|
|
|
|
|
return state.dictMap.get(dictType);
|
2025-03-25 22:30:22 +08:00
|
|
|
},
|
|
|
|
|
},
|
2025-03-25 22:51:51 +08:00
|
|
|
persist: [{ pick: ['dictMap', 'isSetDict'] }],
|
2025-03-25 22:30:22 +08:00
|
|
|
state: (): DictState => ({
|
2025-03-25 22:51:51 +08:00
|
|
|
dictMap: new Map<string, DictValueType[]>(),
|
|
|
|
|
isSetDict: false,
|
2025-03-25 22:30:22 +08:00
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 解决热更新问题
|
|
|
|
|
const hot = import.meta.hot;
|
|
|
|
|
if (hot) {
|
|
|
|
|
hot.accept(acceptHMRUpdate(useDictStore, hot));
|
|
|
|
|
}
|