Преглед изворни кода

[Pref 0000] 优化首页请求逻辑

houzekong пре 5 дана
родитељ
комит
326dd4cadd
1 измењених фајлова са 178 додато и 138 уклоњено
  1. 178 138
      src/views/dashboard/SealedGoaf/index.vue

+ 178 - 138
src/views/dashboard/SealedGoaf/index.vue

@@ -101,99 +101,107 @@
 
   async function fetchHomeData() {
     try {
-      // 2. 异步获取所有接口数据(并行请求提升性能)
-      const [coalSeamFireData, productionStatusData, overLimitData, goafAlarmData, accessDetails] = await Promise.all([
-        getCoalSeamFireNum(appStore.simpleMapParams).then((r) => {
-          return map(r, (val, key) => {
-            return {
+      // 每个请求 .then() 中处理数据并 return,.catch() 返回兜底值使 Promise.all 永不 reject
+      const [coalSeamFireData, productionStatusResult, overLimitData, goafAlarmResult, accessResult] = await Promise.all([
+        getCoalSeamFireNum(appStore.simpleMapParams)
+          .then((r) => {
+            return map(r, (val, key) => ({
               coalSeamLevel: `${key}:${val}`,
               num: val,
-            };
-          }) as any;
-        }), // 煤层自燃倾向数据
-        getMineProductionStatusNum(appStore.simpleMapParams), // 当日生产状态数据
-        getOverLimitNum(appStore.simpleMapParams), // 超限数据(可按需处理)
-        getGoafAlarmNum(appStore.simpleMapParams), // 执法处风险统计
-        getGoafAccessCount(appStore.simpleMapParams),
-
-        // getGoafAlarmLevel(appStore.simpleMapParams),
-        // getGoafAlarmLevel({ mineCode: '100008' }),
-      ]);
-
-      const totalProductionNum = Array.isArray(productionStatusData) ? productionStatusData.reduce((sum, item) => sum + (item.num || 0), 0) : 0;
-      productionStatusData.push({
-        num: [1, 7, 2, 0, 6, 8, 9].reduce((total, index) => total + productionStatusData[index].num, 0),
-        name: '停产停建',
-      });
-      productionStatusData.push({
-        num: [10, 11, 12].reduce((total, index) => total + productionStatusData[index].num, 0),
-        name: '长期停产停建',
-      });
+            })) as any;
+          })
+          .catch((e) => {
+            console.error('煤层自燃倾向数据获取失败:', e);
+            return null;
+          }),
 
-      let alarm1Total = 0;
-      let alarm2Total = 0;
-      let alarm3Total = 0;
-      let alarm4Total = 0;
+        getMineProductionStatusNum(appStore.simpleMapParams)
+          .then((r) => {
+            const totalProductionNum = Array.isArray(r) ? r.reduce((sum, item) => sum + (item.num || 0), 0) : 0;
+            if (r.length >= 13) {
+              r.push({
+                num: [1, 7, 2, 0, 6, 8, 9].reduce((total, index) => total + (r[index]?.num || 0), 0),
+                name: '停产停建',
+              });
+              r.push({
+                num: [10, 11, 12].reduce((total, index) => total + (r[index]?.num || 0), 0),
+                name: '长期停产停建',
+              });
+            }
+            return { data: r, totalProductionNum };
+          })
+          .catch((e) => {
+            console.error('当日生产状态数据获取失败:', e);
+            return { data: [], totalProductionNum: 0 };
+          }),
 
-      // 遍历数组累加(边界处理:确保是数组,字段非数字则取0)
-      if (Array.isArray(goafAlarmData)) {
-        goafAlarmData.forEach((item) => {
-          alarm1Total += Number(item.alarm1) || 0;
-          alarm2Total += Number(item.alarm2) || 0;
-          alarm3Total += Number(item.alarm3) || 0;
-          alarm4Total += Number(item.alarm4) || 0;
-        });
-      }
-
-      // 计算所有alarm的总合计
-      const alarmTotal = alarm1Total + alarm2Total + alarm3Total + alarm4Total;
-      // console.log('所有alarm的总合计:', alarmTotal);
-      const totalNum = { productionNum: totalProductionNum, alarmNum: { alarm1Total, alarm2Total, alarm3Total, alarm4Total, alarmTotal } };
-
-      // 联网状态模块数据处理:遍历 accessDetails 数组,累加所有项的 yjNum、zxNum、lxNum、wjNum
-      let yjTotal = 0;
-      let zxTotal = 0;
-      let lxTotal = 0;
-      let wjTotal = 0;
-      let zdTotal = 0;
+        getOverLimitNum(appStore.simpleMapParams)
+          .then((r) => r)
+          .catch((e) => {
+            console.error('超限数据获取失败:', e);
+            return null;
+          }),
 
-      if (Array.isArray(accessDetails)) {
-        accessDetails.forEach((item) => {
-          yjTotal += Number(item.yjNum) || 0;
-          zxTotal += Number(item.zxNum) || 0;
-          lxTotal += Number(item.lxNum) || 0;
-          wjTotal += Number(item.wjNum) || 0;
-          zdTotal += Number(item.zdNum) || 0;
-        });
-      }
-      const goafAccessCount = {
-        accessDetails,
-        totalNum: { yjTotal, zxTotal, lxTotal, wjTotal, zdTotal },
-      };
+        getGoafAlarmNum(appStore.simpleMapParams)
+          .then((r) => {
+            let alarm1Total = 0;
+            let alarm2Total = 0;
+            let alarm3Total = 0;
+            let alarm4Total = 0;
+            if (Array.isArray(r)) {
+              r.forEach((item) => {
+                alarm1Total += Number(item.alarm1) || 0;
+                alarm2Total += Number(item.alarm2) || 0;
+                alarm3Total += Number(item.alarm3) || 0;
+                alarm4Total += Number(item.alarm4) || 0;
+              });
+            }
+            const alarmTotal = alarm1Total + alarm2Total + alarm3Total + alarm4Total;
+            return { data: r, alarmNum: { alarm1Total, alarm2Total, alarm3Total, alarm4Total, alarmTotal } };
+          })
+          .catch((e) => {
+            console.error('预警统计数据获取失败:', e);
+            return { data: null, alarmNum: { alarm1Total: 0, alarm2Total: 0, alarm3Total: 0, alarm4Total: 0, alarmTotal: 0 } };
+          }),
 
-      // const coalSeamFireChartData = ['Ⅰ类容易自燃', 'Ⅱ类自燃', 'Ⅲ类不易自燃'].reduce(
-      //   (val: any, key) => {
-      //     const arr = coalSeamFireData[key];
-      //     val.coalSeamLevel.push(key);
-      //     val.num.push(arr.length);
-      //     val.desc.push(arr.map((e) => `${e.mineName}-${e.coalSeamName}`).join('、'));
-      //     // return { coalSeamLevel: key, num: arr?.length, desc:  };
-      //     return val;
-      //   },
-      //   { coalSeamLevel: [], num: [], desc: [] }
-      // );
-      // console.log('debug coal', coalSeamFireChartData);
+        getGoafAccessCount(appStore.simpleMapParams)
+          .then((r) => {
+            let yjTotal = 0;
+            let zxTotal = 0;
+            let lxTotal = 0;
+            let wjTotal = 0;
+            let zdTotal = 0;
+            if (Array.isArray(r)) {
+              r.forEach((item) => {
+                yjTotal += Number(item.yjNum) || 0;
+                zxTotal += Number(item.zxNum) || 0;
+                lxTotal += Number(item.lxNum) || 0;
+                wjTotal += Number(item.wjNum) || 0;
+                zdTotal += Number(item.zdNum) || 0;
+              });
+            }
+            return {
+              accessDetails: r,
+              totalNum: { yjTotal, zxTotal, lxTotal, wjTotal, zdTotal },
+            };
+          })
+          .catch((e) => {
+            console.error('联网状态数据获取失败:', e);
+            return { accessDetails: null, totalNum: { yjTotal: 0, zxTotal: 0, lxTotal: 0, wjTotal: 0, zdTotal: 0 } };
+          }),
+      ]);
 
-      // 3. 把接口数据赋值给响应式变量(备用)
       mineData.value = {
         coalSeamFireData,
-        productionStatusData,
+        productionStatusData: productionStatusResult.data,
         overLimitData,
-        goafAlarmData,
-        totalNum,
-        goafAccessCount,
+        goafAlarmData: goafAlarmResult.data,
+        totalNum: {
+          productionNum: productionStatusResult.totalProductionNum,
+          alarmNum: goafAlarmResult.alarmNum,
+        },
+        goafAccessCount: accessResult,
       };
-      // 5. 更新页面数据
       updateData(mineData.value);
     } catch (error) {
       console.error('数据获取/配置更新失败:', error);
@@ -202,68 +210,100 @@
 
   async function fetchLeafData() {
     try {
-      // 异步获取所有接口数据(并行请求提升性能)
-      const [monitorData, coalSeamFireData, goafStatusNum, goafAlarmNum, provinceAlarm] = await Promise.all([
-        getGoafData(appStore.simpleMapParams), // 密闭监测详情
-        getCoalSeamFireNum(appStore.simpleMapParams), // 煤层自燃倾向数据
-        getGoafStatusNum(appStore.simpleMapParams), // 密闭统计
-        getGoafAlarmNum(appStore.simpleMapParams), // 预警统计
-        getProvinceAlarm(appStore.simpleMapParams), // 预警信息列表
-      ]);
+      // 每个请求 .then() 中处理数据并 return,.catch() 返回兜底值使 Promise.all 永不 reject
+      const [monitorData, coalSeamFireResult, goafStatusNum, goafAlarmNum, provinceAlarm] = await Promise.all([
+        getGoafData(appStore.simpleMapParams)
+          .then((r) => {
+            if (r?.records) {
+              r.records.forEach((record) => {
+                const { coalSeamAlarmRule, sourcePressureAlarm, fireAlarm, fireAlarmReson, fireAlarmOut, fireAlarmOutReson } = record;
+                record.sourcePressureReason = evaluateExpressionLite(
+                  coalSeamAlarmRule[`ycWarn${sourcePressureAlarm.alarmLevel}`],
+                  ['sourcePressureChange'],
+                  DEFAULT_OPERAND_SET,
+                  DEFAULT_DISPLAY_MAP
+                ).join(';');
 
-      if (monitorData?.records) {
-        monitorData.records.forEach((record) => {
-          const { coalSeamAlarmRule, sourcePressureAlarm, fireAlarm, fireAlarmReson, fireAlarmOut, fireAlarmOutReson } = record;
-          record.sourcePressureReason = evaluateExpressionLite(
-            coalSeamAlarmRule[`ycWarn${sourcePressureAlarm.alarmLevel}`],
-            ['sourcePressureChange'],
-            DEFAULT_OPERAND_SET,
-            DEFAULT_DISPLAY_MAP
-          ).join(';');
+                record.fireAlarmReason = evaluateExpressionLite(
+                  coalSeamAlarmRule[`fireWarn${fireAlarm.alarmLevel}`],
+                  fireAlarmReson,
+                  DEFAULT_OPERAND_SET,
+                  DEFAULT_DISPLAY_MAP
+                ).join(';');
 
-          record.fireAlarmReason = evaluateExpressionLite(
-            coalSeamAlarmRule[`fireWarn${fireAlarm.alarmLevel}`],
-            fireAlarmReson,
-            DEFAULT_OPERAND_SET,
-            DEFAULT_DISPLAY_MAP
-          ).join(';');
+                record.fireAlarmOutReason = evaluateExpressionLite(
+                  coalSeamAlarmRule[`fireOutWarn${fireAlarmOut.alarmLevel}`],
+                  fireAlarmOutReson,
+                  DEFAULT_OPERAND_SET,
+                  DEFAULT_DISPLAY_MAP
+                ).join(';');
+              });
+            }
+            return r;
+          })
+          .catch((e) => {
+            console.error('密闭监测详情获取失败:', e);
+            return null;
+          }),
 
-          record.fireAlarmOutReason = evaluateExpressionLite(
-            coalSeamAlarmRule[`fireOutWarn${fireAlarmOut.alarmLevel}`],
-            fireAlarmOutReson,
-            DEFAULT_OPERAND_SET,
-            DEFAULT_DISPLAY_MAP
-          ).join(';');
-        });
-      }
+        getCoalSeamFireNum(appStore.simpleMapParams)
+          .then((r) => {
+            const trans = r ? Object.values(r).flat() : [];
+            return { data: r, trans };
+          })
+          .catch((e) => {
+            console.error('煤层自燃倾向数据获取失败:', e);
+            return { data: null, trans: [] };
+          }),
 
-      if (provinceAlarm?.records) {
-        const alarmTypeMap = {
-          leakageAlarm: '老空区永久密闭漏风状态报警',
-          sourcePressureAlarm: '内外压差风险报警',
-          fireAlarm: '自燃发火隐患报警',
-          fireAlarmOut: '老空区永久密闭墙外自燃发火报警',
-          unsealAlarm: '火区老空区永久密闭启封报警',
-          explosionAlarm: '老空区永久密闭爆炸危险性报警',
-          overLimitAlarm: '超限报警',
-          lateReport: '数据延迟上报预警',
-        };
+        getGoafStatusNum(appStore.simpleMapParams)
+          .then((r) => r)
+          .catch((e) => {
+            console.error('密闭统计数据获取失败:', e);
+            return null;
+          }),
 
-        // 遍历列表,把 alarmType 替换成中文
-        provinceAlarm.records = provinceAlarm.records.map((item) => {
-          item.alarmTypeCont = alarmTypeMap[item.alarmType] || item.alarmType;
-          return item;
-        });
-      }
-      // 遍历煤层自燃倾向性,数据重组
-      let transCoalSeamFireData: any[] = [];
-      if (coalSeamFireData) {
-        transCoalSeamFireData = Object.values(coalSeamFireData).flat();
-      }
+        getGoafAlarmNum(appStore.simpleMapParams)
+          .then((r) => r)
+          .catch((e) => {
+            console.error('预警统计数据获取失败:', e);
+            return null;
+          }),
 
-      // 把接口数据赋值给响应式变量(备用)
-      goafData.value = { monitorData, coalSeamFireData, transCoalSeamFireData, goafStatusNum, goafAlarmNum, provinceAlarm };
-      // 更新页面数据
+        getProvinceAlarm(appStore.simpleMapParams)
+          .then((r) => {
+            if (r?.records) {
+              const alarmTypeMap = {
+                leakageAlarm: '老空区永久密闭漏风状态报警',
+                sourcePressureAlarm: '内外压差风险报警',
+                fireAlarm: '自燃发火隐患报警',
+                fireAlarmOut: '老空区永久密闭墙外自燃发火报警',
+                unsealAlarm: '火区老空区永久密闭启封报警',
+                explosionAlarm: '老空区永久密闭爆炸危险性报警',
+                overLimitAlarm: '超限报警',
+                lateReport: '数据延迟上报预警',
+              };
+              r.records = r.records.map((item) => {
+                item.alarmTypeCont = alarmTypeMap[item.alarmType] || item.alarmType;
+                return item;
+              });
+            }
+            return r;
+          })
+          .catch((e) => {
+            console.error('预警信息列表获取失败:', e);
+            return null;
+          }),
+      ]);
+
+      goafData.value = {
+        monitorData,
+        coalSeamFireData: coalSeamFireResult.data,
+        transCoalSeamFireData: coalSeamFireResult.trans,
+        goafStatusNum,
+        goafAlarmNum,
+        provinceAlarm,
+      };
       updateData(goafData.value);
     } catch (error) {
       console.error('数据获取/配置更新失败:', error);