Przeglądaj źródła

getDictValue和requestDict 获取字典项函数优化,自动加载字典避免重复请求

lucw 8 miesięcy temu
rodzic
commit
d26247c632

+ 9 - 4
src/store/getters.js

@@ -2,6 +2,8 @@
  * vuex getter
  */
 import dictEnum from '@/enum/dict';
+import store from './index';
+
 export default {
   user: (state) => state.user,
   paramsSetData: (state) => state.paramsSetData,
@@ -17,10 +19,13 @@ export default {
     const obj = (state.dict[dictEnum[enumName]] || []).find((item) => {
       return item.dictCode == dictCode;
     });
-    // console.log(dictEnum);
-    // console.log(enumName);
-    // console.log(state.dict);
-    // console.log('obj-----', obj);
+
+    if (!obj) {
+      // 提交dict模块下的actions的requestDict
+      store.dispatch('dict/requestDict', enumName);
+      return '';
+    }
+
     return obj && obj.dictValue;
   }
 };

+ 32 - 37
src/store/modules/dict.js

@@ -12,7 +12,10 @@ import { getByCode } from '@/api/system/dictionary-data';
 //   //   }
 // };
 
-const state = {};
+const state = {
+  // 加载中的字典项
+  dictLoading: []
+};
 
 const mutations = {
   //根据字典code 添加字典
@@ -25,46 +28,38 @@ const actions = {
   // 根据字典enumName请求字典 已获取的不做重复请求
   async requestDict({ commit, state }, enumName) {
     const code = dictEnum[enumName];
+    // 如果字典正在加载中,则不重复请求
+    if (state.dictLoading.includes(code)) return;
+
+    state.dictLoading.push(code);
 
     if (state[code]?.length) return state[code];
-    let res;
-    // if (otherDictConfig[dictEnum[enumName]]) {
-    //   const config = otherDictConfig[dictEnum[enumName]];
-    //   console.log(config);
-    //   //非枚举定义
-    //   res = await config.request();
-    //   if (res?.code == 0) {
-    //     let list = config.resKey ? res.data[config.resKey] : res.data;
-    //     commit('ADD_DICT', {
-    //       code,
-    //       dict: list.map((item) => ({
-    //         ...item,
-    //         dictCode: item[config.dictCode],
-    //         dictValue: item[config.dictValue]
-    //       }))
-    //     });
-    //     return res.list;
-    //   }
-    // } else {
-    res = await getByCode(code);
-    if (res?.code == 0) {
-      const isNumber = numberList.includes(code);
-      commit('ADD_DICT', {
-        code,
-        dict: res.data.map((item) => {
-          const arr = Object.entries(item)[0] || [];
 
-          return {
-            dictCode: isNumber ? Number(arr[0]) : arr[0],
-            dictValue: arr[1]
-          };
-        })
-      });
-      return res.data;
-    }
-    // }
+    try {
+      let res = await getByCode(code);
+      if (res?.code == 0) {
+        const isNumber = numberList.includes(code);
+        commit('ADD_DICT', {
+          code,
+          dict: res.data.map((item) => {
+            const arr = Object.entries(item)[0] || [];
 
-    return [];
+            return {
+              dictCode: isNumber ? Number(arr[0]) : arr[0],
+              dictValue: arr[1]
+            };
+          })
+        });
+        state.dictLoading = state.dictLoading.filter((item) => item !== code);
+        return res.data;
+      }
+
+      state.dictLoading = state.dictLoading.filter((item) => item !== code);
+      return [];
+    } catch (error) {
+      state.dictLoading = state.dictLoading.filter((item) => item !== code);
+      return [];
+    }
   },
   // 更新字典
   async reloadRequestDict({ commit }, enumName) {

+ 7 - 2
src/views/rulesManagement/releaseRules/components/permitAdd.vue

@@ -291,7 +291,6 @@
             >
               <el-input
                 type="text"
-                :disabled="formData.executeMethodTitle == '无执行方式'"
                 v-model="executeMethodTitleComputed"
               />
             </el-form-item>
@@ -303,7 +302,6 @@
             v-model="row.paramType"
             placeholder="请选择参数类型"
             size="mini"
-            required
           >
             <el-option label="数值" :value="1" />
             <el-option label="选择" :value="2" />
@@ -1116,6 +1114,13 @@
             }
           }
 
+          // 判断参数类型是否选择
+          for (const detail of this.formData.details) {
+            if (!detail.paramType) {
+              return this.$message.warning('请选择规则明细中的参数类型!');
+            }
+          }
+
           try {
             this.btnLoading = true;
 

+ 2 - 2
vue.config.js

@@ -36,13 +36,13 @@ module.exports = {
         // target: 'http://192.168.1.158:18086',
         // target: 'http://192.168.1.176:18086',
         // target: 'http://192.168.1.125:18086',
-        // target: 'http://192.168.1.251:18186',
+        target: 'http://192.168.1.251:18186', // 测试环境
         // target: 'http://192.168.1.251:18087',
         // target: 'http://192.168.1.116:18086',
         // target: 'http://192.168.1.251:18086',
 
         // target: 'http://192.168.1.251:18086', // 开发
-        target: 'http://192.168.1.116:18086', // 赵沙金
+        // target: 'http://192.168.1.116:18086', // 赵沙金
 
         changeOrigin: true, // 只有这个值为true的情况下 才表示开启跨域
         pathRewrite: {