From e7b9aa637e3353c3a3cd4c47ce2e9386d727eb28 Mon Sep 17 00:00:00 2001 From: rashstarfish <2857387872@qq.com> Date: Wed, 12 Mar 2025 20:49:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AD=97=E5=85=B8=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/dict.ts | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/utils/dict.ts diff --git a/src/utils/dict.ts b/src/utils/dict.ts new file mode 100644 index 0000000..f0cddde --- /dev/null +++ b/src/utils/dict.ts @@ -0,0 +1,70 @@ +import { fetchGetDictionaryDataList, fetchGetDictionaryTypeList } from '@/service/api'; +// export dict +// { +// type:{ +// sys_sex:[ +// {label:"男", +// value: "1"}, +// {label:"女", +// value: "2"} +// ] +// } + +// } +type indexAbleDictData = { + label: string; + value: string; +}; +type indexAbleDictType = { + [key: string]: indexAbleDictData[]; +}; + +type indexAbleDict = { + type: indexAbleDictType; +}; + +async function init() { + // const dictTypeList = reactive([]); + // const dict: indexAbleDict = { + // type: { + // fruits: [ + // { label: '苹果', value: 'apple' }, + // { label: '香蕉', value: 'banana' }, + // { label: '橙子', value: 'orange' } + // ], + // colors: [ + // { label: '红色', value: 'red' }, + // { label: '绿色', value: 'green' }, + // { label: '蓝色', value: 'blue' } + // ], + // animals: [ + // { label: '狗', value: 'dog' }, + // { label: '猫', value: 'cat' }, + // { label: '鸟', value: 'bird' } + // ] + // } + // }; + const typeList: string[] = (await fetchGetDictionaryTypeList()).data?.data.map(item => item.dictType) || []; + const dataList: any[] = + (await fetchGetDictionaryDataList()).data?.data.map(item => ({ + dictType: item.dictType, + dictLabel: item.dictLabel, + dictValue: item.dictValue + })) || []; + const dict: indexAbleDict = { + type: Array.from(typeList).reduce((acc, dictType) => { + // 筛选出当前 dictType 对应的数据 + const items = dataList.filter(item => item.dictType === dictType); + // 将数据转换为 indexAbleDictData 类型 + acc[dictType] = items.map(item => ({ + label: item.dictLabel, + value: item.dictValue + })); + return acc; + }, {} as indexAbleDictType) + }; + return dict; +} +const dict = init(); + +export default dict;