Files
BMS/bms-front/src/utils/dict.js
T
2026-09-08 10:51:20 +08:00

31 lines
927 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import useDictStore from '@/store/modules/dict'
import { getDicts } from '@/api/system/dict/data'
/**
* 获取字典数据(优先使用登录后全量缓存,按 dictType code 匹配)
*/
export function useDict(...args) {
const res = ref({})
return (() => {
args.forEach((dictType) => {
res.value[dictType] = []
const dicts = useDictStore().getDict(dictType)
if (dicts) {
res.value[dictType] = dicts
} else {
// 缓存未命中时按类型补拉(如字典管理刚改完被 removeDict)
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)
})()
}