feat: dict store【358d03c5】
parent
73a5cb8e37
commit
4556a3e03b
|
@ -0,0 +1,50 @@
|
||||||
|
// TODO @芋艿:API 的风格
|
||||||
|
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);
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -0,0 +1,74 @@
|
||||||
|
// TODO @芋艿:【可优化】挪到 packages/stores/src/modules
|
||||||
|
import { acceptHMRUpdate, defineStore } from 'pinia';
|
||||||
|
|
||||||
|
export interface DictItem {
|
||||||
|
colorType?: string;
|
||||||
|
cssClass?: string;
|
||||||
|
label: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Dict = Record<string, DictItem[]>;
|
||||||
|
|
||||||
|
interface DictState {
|
||||||
|
dictCache: Dict;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<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);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
persist: {
|
||||||
|
// 持久化
|
||||||
|
pick: ['dictCache'],
|
||||||
|
},
|
||||||
|
state: (): DictState => ({
|
||||||
|
dictCache: {},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
// 解决热更新问题
|
||||||
|
const hot = import.meta.hot;
|
||||||
|
if (hot) {
|
||||||
|
hot.accept(acceptHMRUpdate(useDictStore, hot));
|
||||||
|
}
|
|
@ -1 +1,2 @@
|
||||||
export * from './auth';
|
export * from './auth';
|
||||||
|
export * from './dict';
|
||||||
|
|
Loading…
Reference in New Issue