Răsfoiți Sursa

516可视化数据解析修改

tangjunhao 3 luni în urmă
părinte
comite
3e113c1636
1 a modificat fișierele cu 95 adăugiri și 83 ștergeri
  1. 95 83
      src/views/home.vue

+ 95 - 83
src/views/home.vue

@@ -3229,39 +3229,45 @@ const curveLine3 = () => {
     })
 }
 
-// 可视化页数据
-const curveLine4 = () => {
-  const params = {
-    transCode: 'MDO0021',
-    pid: pid.value,
-    type: 1,
-  }
-  request(params)
-    .then((res) => {
-      // curvedata.value = JSON.stringify(res.rows);
-      // console.log('数据形式:',curvedata.value);
-      
-      // echartLineref.value.getshuju(curvedata.value);
-      // scatterref.value.getshuju(curvedata.value);
-
-      if (res.rows && res.rows.length > 0) {
-        // 清空数据
-        datatotabledata.value = [];
-        datatotableheader.value = [];
-
-        // 遍历返回的所有行数据
-        res.rows.forEach((row) => {
-          const { vars, vals } = row; // 解析 vars 和 vals
+const curveLine4 = async () => {
+  try {
+    const params = {
+      transCode: 'MDO0021',
+      pid: pid.value,
+      type: 1,
+    };
+    
+    const res = await request(params);
+    
+    if (!res) {
+      throw new Error('响应数据为空');
+    }
+        
+    if (res.rows && res.rows.length > 0) {
+      // 清空数据
+      datatotabledata.value = [];
+      datatotableheader.value = [];
+
+      // 遍历返回的所有行数据
+      res.rows.forEach((row) => {
+        try {
+          const { vars, vals } = row;
           datatotable(vars, vals);
-        });
-      }
-      console.log("表格数据:", datatotabledata.value);
-      console.log("表头数据:", datatotableheader.value);
-    })
-    .catch((err) => {
-      ElMessage.error(err.returnMsg)
-    })
-}
+        } catch (e) {
+          console.error('处理行数据时出错:', e);
+          ElMessage.error(`处理数据时出错: ${e.message}`);
+        }
+      });
+    }
+    
+    console.log("表格数据:", datatotabledata.value);
+    console.log("表头数据:", datatotableheader.value);
+    
+  } catch (err) {
+    console.error('请求出错:', err);
+    ElMessage.error(err.message || err.returnMsg || '操作失败,请重试');
+  }
+};
 
 const initListSe = () => {
   emitter.emit('update:datatotableheader', datatotableheader.value);
@@ -3272,65 +3278,71 @@ let datatotableheader = ref([]);
 let datatotabledata = ref([]);
 // 解析数据到表格
 const datatotable = (vars, vals) => {
-  console.log("表格更新");
-
-  // 解析列名,vars 以空格分隔
-  let headers = vars.split(" ");
+  try {
+    // 1. 安全解析输入数据
+    const headers = vars ? vars.trim().split(/\s+/) : [];
+    const values = vals ? vals.trim().split(/\s+/) : [];
+    
+    // 2. 检查数据有效性
+    if (headers.length === 0 || values.length === 0) {
+      console.warn('无效输入数据', { vars, vals });
+      return;
+    }
 
-  // 解析 vals 数据
-  let values = vals.split(" ");
+    // 3. 构造表格数据对象
+    const rowData = {};
+    const newHeaders = [];
 
-  // 构造表格数据对象
-  let rowData = {};
-  let newHeaders = [];
-
-  headers.forEach((key, index) => {
-    let val = values[index];
-
-    // 处理数组数据,解析为 JSON 或直接填充
-    if (val.startsWith("[") && val.endsWith("]")) {
-      try {
-        let parsedArray = JSON.parse(val); // 解析数组
-        // 解析数组数据并拆分为多个列
-        parsedArray.forEach((item, subIndex) => {
-          let newKey = `${key}_${subIndex + 1}`; // 生成列名:原列名_1, _2, _3...
-          rowData[newKey] = item;
-          // 只在第一次时添加新列
-          if (
-            datatotableheader.value.length === 0 ||
-            !datatotableheader.value.some((h) => h.prop === newKey)
-          ) {
-            newHeaders.push({ label: newKey, prop: newKey });
+    headers.forEach((key, index) => {
+      // 4. 安全获取值
+      const val = values[index] !== undefined ? values[index] : "";
+      
+      // 5. 安全处理数组数据
+      if (typeof val === 'string' && val.startsWith("[") && val.endsWith("]")) {
+        try {
+          const parsedArray = JSON.parse(val);
+          parsedArray.forEach((item, subIndex) => {
+            const newKey = `${key}_${subIndex + 1}`;
+            rowData[newKey] = item;
+            
+            // 6. 安全更新表头
+            if (!datatotableheader.value.some(h => h.prop === newKey)) {
+              newHeaders.push({ label: newKey, prop: newKey });
+            }
+          });
+        } catch (e) {
+          console.warn('数组解析失败,使用原始值:', val);
+          rowData[key] = val;
+          if (!datatotableheader.value.some(h => h.prop === key)) {
+            newHeaders.push({ label: key, prop: key });
           }
-        });
-      } catch (e) {
-        rowData[key] = val; // 解析失败,保留原始字符串
+        }
+      } else {
+        // 7. 处理普通值
+        rowData[key] = val;
+        if (!datatotableheader.value.some(h => h.prop === key)) {
+          newHeaders.push({ label: key, prop: key });
+        }
       }
-    } else {
-      rowData[key] = val !== undefined ? val : ""; // 处理数据长度不匹配情况
-      // 只在第一次时添加原始列
-      if (
-        datatotableheader.value.length === 0 ||
-        !datatotableheader.value.some((h) => h.prop === key)
-      ) {
-        newHeaders.push({ label: key, prop: key });
+    });
+
+    // 8. 批量更新表头(性能优化)
+    if (newHeaders.length > 0) {
+      const existingProps = datatotableheader.value.map(h => h.prop);
+      const headersToAdd = newHeaders.filter(
+        h => !existingProps.includes(h.prop)
+      );
+      if (headersToAdd.length > 0) {
+        datatotableheader.value = [...datatotableheader.value, ...headersToAdd];
       }
     }
-  });
 
-  // 更新表头:防止重复添加
-  if (datatotableheader.value.length === 0) {
-    datatotableheader.value = newHeaders;
-  } else {
-    newHeaders.forEach((header) => {
-      if (!datatotableheader.value.some((h) => h.prop === header.prop)) {
-        datatotableheader.value.push(header);
-      }
-    });
-  }
+    // 9. 更新表格数据
+    datatotabledata.value = [...datatotabledata.value, rowData];
 
-  // 更新表格数据
-  datatotabledata.value.push(rowData);
+  } catch (error) {
+    console.error('datatotable处理出错:', error);
+  }
 };
 
 let listcbval = ref([]);