Sfoglia il codice sorgente

feat(系统模块): 添加批量查询字典接口

shizhongming 2 anni fa
parent
commit
423a133c82

+ 9 - 6
src/components/Form/src/smart-boot/components/SmartApiSelectDict.vue

@@ -29,7 +29,7 @@
 </template>
 
 <script lang="ts" setup>
-  import { type PropType, ref, Ref, unref, watch } from 'vue';
+  import { type PropType, ref, Ref, watch } from 'vue';
   import type { SelectValue } from 'ant-design-vue/es/select';
 
   import { computed, inject } from 'vue';
@@ -54,30 +54,33 @@
 
   const { t } = useI18n();
 
-  const registerHandler = inject(SmartProviderConstants.dictRegisterKey) as Function | undefined;
+  const registerHandler = inject(SmartProviderConstants.dictRegisterKey, null) as Function | null;
   if (registerHandler) {
     registerHandler(props.dictCode);
   }
   /**
    * 加载状态
    */
-  const loadingRef = inject(SmartProviderConstants.dictLoadingKey) as Ref<boolean> | undefined;
+  const loadingRef = inject(SmartProviderConstants.dictLoadingKey, null) as Ref<boolean> | null;
   /**
    * 是否有注入
    */
   const computedHasProvider = computed(() => {
-    return registerHandler !== undefined;
+    return registerHandler !== null;
   });
 
   /**
    * 注入OPTIONS
    */
-  const dictDataRef = inject(SmartProviderConstants.dictData) as Ref<Recordable<any[]>> | undefined;
+  const dictDataRef = inject(SmartProviderConstants.dictData, null) as Map<
+    string,
+    Recordable[]
+  > | null;
   const computedOptions = computed(() => {
     if (!dictDataRef) {
       return [];
     }
-    const dictData = unref(dictDataRef)?.[props.dictCode];
+    const dictData = dictDataRef.get(props.dictCode);
     if (!dictData) {
       return [];
     }

+ 6 - 6
src/components/SmartPageProvider/src/constants.ts

@@ -1,6 +1,6 @@
-export enum SmartProviderConstants {
-  dictRegisterKey = 'SMART_PROVIDER_DICT_SELECT_REGISTER',
-  dictLoadingKey = 'SMART_PROVIDER_DICT_SELECT_LOADING',
-  dictData = 'SMART_PROVIDER_DICT_SELECT_DATA',
-  dictMap = 'SMART_PROVIDER_DICT_SELECT_MAP',
-}
+export const SmartProviderConstants = {
+  dictRegisterKey: Symbol('smart_provider_dict_select_register'),
+  dictLoadingKey: Symbol('SMART_PROVIDER_DICT_SELECT_LOADING'),
+  dictData: Symbol('SMART_PROVIDER_DICT_SELECT_DATA'),
+  dictMap: Symbol('SMART_PROVIDER_DICT_SELECT_MAP'),
+};

+ 31 - 20
src/components/SmartPageProvider/src/hooks/useProviderDict.ts

@@ -1,22 +1,19 @@
-import { computed, onMounted, provide, reactive, ref, unref } from 'vue';
+import { computed, onMounted, provide, reactive, ref, watch } from 'vue';
 import { SmartProviderConstants } from '@/components/SmartPageProvider/src/constants';
 import { ApiServiceEnum, defHttp } from '@/utils/http/axios';
 
 export const useProviderDict = () => {
+  let hasInitLoad = false;
   const dictCodeList = reactive<string[]>([]);
+  const dictDataMap = reactive(new Map<string, Recordable>());
+
   // 字典加载状态
   const dictLoadingRef = ref(false);
 
-  /**
-   * 字典数据
-   */
-  const dictMapRef = ref<Recordable<any[]>>({});
-
   const computedDictMap = computed(() => {
     const result: Recordable<Recordable> = {};
 
-    Object.keys(unref(dictMapRef)).forEach((key) => {
-      const list = unref(dictMapRef)[key];
+    dictDataMap.forEach((list, key) => {
       const itemMap: Recordable = {};
       list.forEach((item) => {
         itemMap[item.dictItemCode] = item.dictItemName;
@@ -27,7 +24,10 @@ export const useProviderDict = () => {
     return result;
   });
 
-  onMounted(() => loadDictData());
+  onMounted(async () => {
+    await loadDictData();
+    hasInitLoad = true;
+  });
 
   /**
    * 批量加载字典数据
@@ -36,30 +36,41 @@ export const useProviderDict = () => {
     if (dictCodeList.length === 0) {
       return;
     }
+    const noLoadDictCodeList = dictCodeList.filter((item) => !dictDataMap.has(item));
+    if (noLoadDictCodeList.length === 0) {
+      return;
+    }
     try {
       dictLoadingRef.value = true;
       const result =
         (await defHttp.post({
           service: ApiServiceEnum.SMART_SYSTEM,
           url: 'sys/dict/batchListItemByCode',
-          data: dictCodeList,
+          data: noLoadDictCodeList,
         })) || {};
-      const dictMap: Recordable<any[]> = {};
       for (const key in result) {
-        dictMap[key] = result[key].map((item) => {
-          return {
-            ...item,
-            label: `${item.dictItemCode}-${item.dictItemName}`,
-            value: item.dictItemCode,
-          };
-        });
+        dictDataMap.set(
+          key,
+          result[key].map((item) => {
+            return {
+              ...item,
+              label: item.dictItemName,
+              value: item.dictItemCode,
+            };
+          }),
+        );
       }
-      dictMapRef.value = dictMap;
     } finally {
       dictLoadingRef.value = false;
     }
   };
 
+  watch(dictCodeList, () => {
+    if (hasInitLoad) {
+      loadDictData();
+    }
+  });
+
   /**
    * 注入注册函数
    */
@@ -72,7 +83,7 @@ export const useProviderDict = () => {
    */
   provide(SmartProviderConstants.dictLoadingKey, dictLoadingRef);
 
-  provide(SmartProviderConstants.dictData, dictMapRef);
+  provide(SmartProviderConstants.dictData, dictDataMap);
 
   provide(SmartProviderConstants.dictMap, computedDictMap);
 };