dict.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { storageSession } from "@pureadmin/utils";
  2. import { getDictList } from "@/api/dict";
  3. export interface DictItem {
  4. id?: number;
  5. code: string;
  6. name: string;
  7. value: any;
  8. weight?: number;
  9. remark?: string;
  10. list?: DictItem[];
  11. }
  12. export interface DictGroup {
  13. [code: string]: DictItem[];
  14. }
  15. const DICT_KEY = "dicts";
  16. const dictUtil = {
  17. loadDicts: async (): Promise<DictGroup> => {
  18. try {
  19. const list: any[] = await getDictList({ pageSize: 1024 });
  20. const dictGroup = dictUtil.groupByKey(list, "code");
  21. storageSession().setItem(DICT_KEY, dictGroup);
  22. return dictGroup;
  23. } catch (error) {
  24. console.error("加载字典数据失败:", error);
  25. return {};
  26. }
  27. },
  28. getDicts: (): DictGroup => {
  29. return storageSession().getItem(DICT_KEY) || {};
  30. },
  31. getDictList: (code: string): DictItem[] => {
  32. const dicts = dictUtil.getDicts();
  33. return dicts[code] || [];
  34. },
  35. getDictLabel: (code: string, value: any): string => {
  36. if (value === null || value === undefined || value === "") {
  37. return "--";
  38. }
  39. const dicts = dictUtil.getDicts();
  40. const list = dicts[code];
  41. if (!list || list.length === 0) {
  42. return "--";
  43. }
  44. const item = list.find(k => k.value == value);
  45. return item ? item.name : "--";
  46. },
  47. getDictValue: (code: string, name: string): any => {
  48. const dicts = dictUtil.getDicts();
  49. const list = dicts[code];
  50. if (!list || list.length === 0) {
  51. return null;
  52. }
  53. const item = list.find(k => k.name === name);
  54. return item ? item.value : null;
  55. },
  56. groupByKey: (elements: any[], key: string): DictGroup => {
  57. const map: DictGroup = {};
  58. if (!elements || elements.length === 0) {
  59. return map;
  60. }
  61. for (let i = 0; i < elements.length; i++) {
  62. if (!elements[i][key] && elements[i][key] !== 0) {
  63. continue;
  64. }
  65. const k = elements[i][key].toString();
  66. const tmp = map[k] || [];
  67. tmp.push(elements[i]);
  68. map[k] = tmp;
  69. }
  70. return map;
  71. },
  72. clearDicts: () => {
  73. storageSession().removeItem(DICT_KEY);
  74. }
  75. };
  76. export const formatDict = (code: string, value: any): string => {
  77. return dictUtil.getDictLabel(code, value);
  78. };
  79. export const getDictOptions = (code: string): { label: string; value: any }[] => {
  80. const list = dictUtil.getDictList(code);
  81. return list.map(item => ({
  82. label: item.name,
  83. value: item.value
  84. }));
  85. };
  86. export const getDictColor = (code: string, value: any): string => {
  87. const list = dictUtil.getDictList(code);
  88. const item = list.find(k => k.value == value);
  89. return item?.color || "";
  90. };
  91. export default dictUtil;