65 lines
1.9 KiB
JavaScript
Raw Normal View History

2025-05-08 15:37:28 +08:00
import useDictStore from '@/store/modules/dict'
import { getDicts } from '@/api/system/dict/data'
2025-05-20 16:34:38 +08:00
import { getComDicts } from '@/api/liswork/dict/ComDict.js'
2025-05-08 15:37:28 +08:00
/**
* 获取字典数据
*/
export function useDict(...args) {
const res = ref({});
return (() => {
args.forEach((dictType, index) => {
res.value[dictType] = [];
const dicts = useDictStore().getDict(dictType);
if (dicts) {
res.value[dictType] = dicts;
} else {
getDicts(dictType).then(resp => {
res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
useDictStore().setDict(dictType, res.value[dictType]);
})
}
})
return toRefs(res.value);
})()
2025-05-20 16:34:38 +08:00
}
/**
* 获取字典数据(改进版)
* 返回 Promise,确保所有字典数据加载完成后才 resolve
*/
export function comDict(...args) {
const res = ref({});
const promises = [];
args.forEach((dictType) => {
res.value[dictType] = [];
const dicts = useDictStore().getDict(dictType);
if (dicts) {
// 缓存存在,直接赋值
res.value[dictType] = dicts;
} else {
// 缓存不存在,发起请求并记录 Promise
const promise = getComDicts({ zdlb: dictType }).then(resp => {
const dictList = resp.data.map(p => ({
label: p.zdmc,
value: p.zddh,
elTagType: "",
elTagClass: ""
}));
res.value[dictType] = dictList;
useDictStore().setDict(dictType, dictList);
// console.log(`${dictType} 字典加载完成`, dictList);
return dictList;
});
promises.push(promise);
}
});
// 等待所有 Promise 完成后才返回
return Promise.all(promises).then(() => {
// console.log('所有请求的字典已加载完成', res.value);
return toRefs(res.value);
});
2025-05-08 15:37:28 +08:00
}