dict.ts 1010 B

1234567891011121314151617181920212223242526
  1. // import { getDicts } from '@/api/system/dict/data';
  2. import { useDictStore } from '@/store/modules/dict';
  3. import { useClientRequest } from '@/utils/request';
  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. const resp: any = await useClientRequest.get(`/system/dict/data/type/${dictType}`);
  19. res.value[dictType] = resp.data.map((p): DictDataOption => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }));
  20. useDictStore().setDict(dictType, res.value[dictType]);
  21. }
  22. });
  23. return res.value;
  24. })();
  25. };