dict.ts 1000 B

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