dict.ts 864 B

123456789101112131415161718192021222324252627
  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 => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })
  20. );
  21. useDictStore().setDict(dictType, res.value[dictType]);
  22. });
  23. }
  24. });
  25. return res.value;
  26. })();
  27. };