Browse Source

fix: 字典逻辑修改

hanxiaohui 5 months ago
parent
commit
61543a58a1
1 changed files with 14 additions and 28 deletions
  1. 14 28
      src/utils/dict.js

+ 14 - 28
src/utils/dict.js

@@ -1,13 +1,12 @@
 import { dictApi } from '/@/api/support/dict-api';
 import { reactive } from 'vue';
+import { mapValues } from 'lodash';
 
 const useBsDict = class {
   dicts = {};
 
   constructor() {
-    this.dicts = reactive({
-      items: [],
-    });
+    this.dicts = reactive({});
   }
 
   /**
@@ -15,7 +14,7 @@ const useBsDict = class {
   async init() {
     const dictStr = localStorage.getItem('dicts');
     if (dictStr) {
-      this.dicts.items = JSON.parse(dictStr);
+      this.dicts = JSON.parse(dictStr);
     } else {
       await this.fetchDictData();
     }
@@ -34,7 +33,7 @@ const useBsDict = class {
    * @returns {void}
    */
   clear() {
-    this.dicts.items = [];
+    this.dicts = {};
     localStorage.removeItem('dicts');
   }
 
@@ -43,40 +42,27 @@ const useBsDict = class {
     return obj?.label;
   }
 
-  getDictByCode(dictCode) {
-    return this.dicts.items.find((v) => v.dictCode === dictCode);
-  }
-
-  getDictItems(dictCode) {
-    const dict = this.getDictByCode(dictCode);
-    return dict?.dictItems;
-  }
-
   getDictItem(dictCode, dictValue) {
-    const dict = this.getDictByCode(dictCode);
-    return dict?.dictItems.find((v) => v.value === dictValue);
+    return this.dicts[dictCode].find(v => v.value === dictValue);
   }
 
   // 参数归一化
   normalizeDictNames(data) {
-    return data.map((v) => ({
-      dictName: v.key.keyName,
-      dictCode: v.key.keyCode,
-      raw: v,
-      dictItems: v.value.map((vv) => ({
-        label: vv.valueName,
-        value: vv.valueCode,
-        raw: vv,
-      })),
-    }));
+    return mapValues(data, (value, key) => {
+      return value.map((v) => ({
+        label: v.valueName,
+        value: v.valueCode,
+        raw: v,
+      }));
+    });
   }
 
   // 获取字典数据
   async fetchDictData() {
     const res = await dictApi.getDictAll();
     if (res?.code === 0) {
-      this.dicts.items = this.normalizeDictNames(res.data);
-      localStorage.setItem('dicts', JSON.stringify(this.dicts.items));
+      this.dicts = this.normalizeDictNames(res.data);
+      localStorage.setItem('dicts', JSON.stringify(this.dicts));
     }
   }
 };