dict.ts 809 B

1234567891011121314151617181920212223242526
  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. args.forEach(async (dictType) => {
  11. res.value[dictType] = [];
  12. const dicts = useDictStore().getDict(dictType);
  13. if (dicts) {
  14. res.value[dictType] = dicts;
  15. } else {
  16. await getDicts(dictType).then((resp) => {
  17. res.value[dictType] = resp.data.map(
  18. (p): DictDataOption => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })
  19. );
  20. useDictStore().setDict(dictType, res.value[dictType]);
  21. });
  22. }
  23. });
  24. return res.value;
  25. };