31 lines
927 B
JavaScript
31 lines
927 B
JavaScript
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)
|
||
})()
|
||
}
|