dict.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { getDicts } from '@/api/system/dict/data';
  2. import { vipLevelList } from '@/api/dgtmedicine/member';
  3. import { useDictStore } from '@/store/modules/dict';
  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. if(dictType == 'vip_level'){
  19. await vipLevelList().then((resp) => {
  20. res.value[dictType] = resp.rows.map(
  21. (p): DictDataOption => ({ label: p.vipName, value: p.vipLevel + '', elTagType: null, elTagClass: null })
  22. );
  23. useDictStore().setDict(dictType, res.value[dictType]);
  24. });
  25. } else{
  26. await getDicts(dictType).then((resp) => {
  27. res.value[dictType] = resp.data.map(
  28. (p): DictDataOption => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })
  29. );
  30. useDictStore().setDict(dictType, res.value[dictType]);
  31. });
  32. }
  33. }
  34. });
  35. return res.value;
  36. })();
  37. };