feat: dict store

pull/48/MERGE
xingyu 2024-11-21 13:54:39 +08:00
parent 91e1ee7ae4
commit 358d03c5b5
4 changed files with 177 additions and 0 deletions

View File

@ -0,0 +1,49 @@
import { requestClient } from '#/api/request';
export type DictDataVO = {
colorType: string;
createTime: Date;
cssClass: string;
dictType: string;
id: number | undefined;
label: string;
remark: string;
sort: number | undefined;
status: number;
value: string;
};
// 查询字典数据(精简)列表
export function getSimpleDictDataList() {
return requestClient.get('/system/dict-data/simple-list');
}
// 查询字典数据列表
export function getDictDataPage(params: any) {
return requestClient.get('/system/dict-data/page', params);
}
// 查询字典数据详情
export function getDictData(id: number) {
return requestClient.get(`/system/dict-data/get?id=${id}`);
}
// 新增字典数据
export function createDictData(data: DictDataVO) {
return requestClient.post('/system/dict-data/create', data);
}
// 修改字典数据
export function updateDictData(data: DictDataVO) {
return requestClient.put('/system/dict-data/update', data);
}
// 删除字典数据
export function deleteDictData(id: number) {
return requestClient.delete(`/system/dict-data/delete?id=${id}`);
}
// 导出字典类型数据
export function exportDictData(params: any) {
return requestClient.download('/system/dict-data/export', params);
}

View File

@ -0,0 +1,44 @@
import { requestClient } from '#/api/request';
export type DictTypeVO = {
createTime: Date;
id: number | undefined;
name: string;
remark: string;
status: number;
type: string;
};
// 查询字典(精简)列表
export function getSimpleDictTypeList() {
return requestClient.get('/system/dict-type/list-all-simple');
}
// 查询字典列表
export function getDictTypePage(params: any) {
return requestClient.get('/system/dict-type/page', params);
}
// 查询字典详情
export function getDictType(id: number) {
return requestClient.get(`/system/dict-type/get?id=${id}`);
}
// 新增字典
export function createDictType(data: DictTypeVO) {
return requestClient.post('/system/dict-type/create', data);
}
// 修改字典
export function updateDictType(data: DictTypeVO) {
return requestClient.put('/system/dict-type/update', data);
}
// 删除字典
export function deleteDictType(id: number) {
return requestClient.delete(`/system/dict-type/delete?id=${id}`);
}
// 导出字典类型
export function exportDictType(params: any) {
return requestClient.download('/system/dict-type/export', params);
}

View File

@ -0,0 +1,83 @@
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;
colorType?: string;
cssClass?: string;
}
// interface DictTypeType {
// dictType: string;
// dictValue: DictValueType[];
// }
interface DictState {
dictMap: Map<string, DictValueType[]>;
isSetDict: boolean;
}
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<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);
}
},
},
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 => ({
dictMap: new Map<string, DictValueType[]>(),
isSetDict: false,
}),
});
// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
hot.accept(acceptHMRUpdate(useDictStore, hot));
}

View File

@ -1 +1,2 @@
export * from './auth';
export * from './dict';