dict.ts 879 B

1234567891011121314151617181920212223242526272829
  1. // 系统全局字典
  2. import useDictStore from '@/stores/modules/dict'
  3. import { getDictsApi } from '@/api/modules/system/dictData'
  4. /**
  5. * 获取字典数据
  6. */
  7. export const useDict = (...args: string[]): { [key: string]: DictDataOption[] } => {
  8. const res = ref<{
  9. [key: string]: DictDataOption[]
  10. }>({})
  11. return (() => {
  12. args.forEach(async dictType => {
  13. res.value[dictType] = []
  14. const dicts = useDictStore().getDict(dictType)
  15. if (dicts) {
  16. res.value[dictType] = dicts
  17. } else {
  18. await getDictsApi(dictType).then(resp => {
  19. res.value[dictType] = resp.data.map(
  20. (p): DictDataOption => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })
  21. )
  22. useDictStore().setDict(dictType, res.value[dictType])
  23. })
  24. }
  25. })
  26. return res.value
  27. })()
  28. }