diff --git a/apps/web-antd/src/api/system/dict/dict.data.ts b/apps/web-antd/src/api/system/dict/dict.data.ts new file mode 100644 index 000000000..674b4ca28 --- /dev/null +++ b/apps/web-antd/src/api/system/dict/dict.data.ts @@ -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); +} diff --git a/apps/web-antd/src/api/system/dict/dict.type.ts b/apps/web-antd/src/api/system/dict/dict.type.ts new file mode 100644 index 000000000..59af57114 --- /dev/null +++ b/apps/web-antd/src/api/system/dict/dict.type.ts @@ -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); +} diff --git a/apps/web-antd/src/store/dict.ts b/apps/web-antd/src/store/dict.ts new file mode 100644 index 000000000..52a44ad8e --- /dev/null +++ b/apps/web-antd/src/store/dict.ts @@ -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; + +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) => 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], + })); + }); + this.setDictCache(dictCacheData); + }); + }, + }, + persist: { + // 持久化 + pick: ['dictCache'], + }, + state: (): DictState => ({ + dictCache: {}, + }), +}); + +// 解决热更新问题 +const hot = import.meta.hot; +if (hot) { + hot.accept(acceptHMRUpdate(useDictStore, hot)); +} diff --git a/apps/web-antd/src/store/index.ts b/apps/web-antd/src/store/index.ts index 269586ee8..b6a7763b1 100644 --- a/apps/web-antd/src/store/index.ts +++ b/apps/web-antd/src/store/index.ts @@ -1 +1,2 @@ export * from './auth'; +export * from './dict';