From 6641cefa084db2636a997de00fde5be167788e1e Mon Sep 17 00:00:00 2001
From: guonan <guonan201020@163.com>
Date: 星期四, 29 五月 2025 10:41:28 +0800
Subject: [PATCH] 优化定时加载

---
 src/components/monifangzhen/schemeCard.vue |   97 +++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 74 insertions(+), 23 deletions(-)

diff --git a/src/components/monifangzhen/schemeCard.vue b/src/components/monifangzhen/schemeCard.vue
index 8a0b811..3d1d2d1 100644
--- a/src/components/monifangzhen/schemeCard.vue
+++ b/src/components/monifangzhen/schemeCard.vue
@@ -13,7 +13,10 @@
         <p>鍒涘缓鏃堕棿 : {{ formatTime(item.createTime) }}</p>
         <p>
           鏂规鐘舵�� :
-          <span style="color: aquamarine">{{ item.result || "鍒涘缓浠跨湡" }}</span>
+          <span style="color: aquamarine">
+            {{ item.result === -1 ? "鍑洪敊" : item.result || "鍒涘缓浠跨湡" }}
+          </span>
+          <!-- <span style="color: aquamarine">{{ item.result || "鍒涘缓浠跨湡" }}</span> -->
           <!-- <span style="color: aquamarine">{{
             statusText[item.status] || "鏈煡"
           }}</span> -->
@@ -50,12 +53,20 @@
 import { initeWaterPrimitiveView } from "@/utils/water";
 import Message from "@/components/tools/Message.vue";
 import { useSimStore } from "@/store/simulation.js";
+import { SimAPIStore } from "@/store/simAPI";
 import schemeInfo from "@/components/monifangzhen/schemeInfo.vue";
 import { ElMessage, ElMessageBox } from "element-plus";
 const emit = defineEmits(["start", "end", "reset", "closeBtn"]);
-import { getSimStart, getSimDataById } from "@/api/trApi";
+import {
+  getRegionData,
+  getSimData,
+  deleteSimData,
+  getSimStart,
+  getSimDataById,
+} from "@/api/trApi.js";
 
 const simStore = useSimStore();
+const simAPIStore = SimAPIStore();
 // 閫変腑鐨勬柟妗� ID
 const selectedId = ref(null);
 // 褰撳墠閫変腑鐨勬柟妗堜俊鎭�
@@ -72,16 +83,22 @@
   10: "瀹屾垚",
   20: "鍑洪敊",
 };
+
 function formatTime(time) {
   return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
 }
+
 const messageShow = ref(false);
+
 const schemeInfoShow = ref(false);
+
 const mesData = ref(null);
+
 function setSchemClick(item) {
   mesData.value = item;
   messageShow.value = true;
 }
+
 function close() {
   messageShow.value = false;
 }
@@ -113,8 +130,11 @@
     simStore.setSelectedScheme(item);
     console.log("鏈夋湇鍔″悕绉�");
   }
+
   const flyHeight = ref(100000);
+
   const shouldShowFill = false;
+
   // 姹傝В鍣ㄦ眰瑙e畬鎴愪箣鍚庢墠鍙互鏄剧ず鏃堕棿杞�
   if (item.status == 10) {
     // 鍙湁琛屾斂鍖哄垝鎵ц
@@ -135,14 +155,17 @@
     emit("start");
   }
 }
+
 function endPlay() {
   emit("end");
 }
+
 function handleBack(value) {
   if (value === false) {
     schemeInfoShow.value = false;
   }
 }
+
 const handleHideSchemeInfo = () => {
   schemeInfoShow.value = false;
   emit("closeBtn", true);
@@ -151,26 +174,6 @@
 // 娉ㄥ唽浜嬩欢鐩戝惉鍣�
 EventBus.on("hide-schemeInfo", handleHideSchemeInfo);
 
-/////////////////////// 璋冪敤鎺ュ彛锛堜娇鐢ㄦ椂鎵撳紑锛� ///////////////////////
-import { getRegionData, getSimData, deleteSimData } from "@/api/trApi.js";
-
-onMounted(() => {
-  getScheme(); // 椤甸潰鍔犺浇鏃剁珛鍗宠幏鍙栨暟鎹�
-  intervalId = setInterval(getScheme, 60 * 1000); // 姣忛殧涓�鍒嗛挓鎵ц涓�娆�
-});
-
-onUnmounted(() => {
-  if (intervalId !== null) {
-    clearInterval(intervalId); // 娓呴櫎瀹氭椂鍣�
-    intervalId = null; // 閲嶇疆 intervalId
-  }
-});
-
-const props = defineProps({
-  deleteSim: Boolean, // 鎺ユ敹鐖剁粍浠朵紶閫掔殑鍑芥暟
-  showAddIns: Boolean,
-});
-
 const schemeList = ref([]);
 let intervalId = null; // 鐢ㄤ簬瀛樺偍 setInterval 鐨勮繑鍥炲��
 // 鑾峰彇鏂规鍒楄〃
@@ -178,10 +181,49 @@
   try {
     const res = await getSimData();
     schemeList.value = res.data;
+
+    const shouldStop = schemeList.value.every(
+      (item) =>
+        item.result == "鍒涘缓浠跨湡" ||
+        item.result == "瀹屾垚" ||
+        item.result == "-1"
+    );
+    simAPIStore.shouldPoll = !shouldStop; // 淇敼 Pinia 鐘舵��
+    // 3. 濡傛灉闇�瑕佸仠姝�
+    if (shouldStop) {
+      if (intervalId) {
+        clearInterval(intervalId);
+        intervalId = null;
+        console.log("鍋滄杞");
+      }
+      return;
+    }
   } catch (error) {
     console.error("Error fetching data:", error);
   }
 }
+
+// 鐩戝惉 shouldPoll 鐘舵�佸彉鍖�
+watch(
+  () => simAPIStore.shouldPoll,
+  (isStarted) => {
+    console.log(isStarted, "瀹氭椂鍣�");
+    if (isStarted) {
+      getScheme(); // 棣栨绔嬪嵆鑾峰彇涓�娆�
+      intervalId = setInterval(getScheme, 60 * 1000); // 姣忛殧涓�鍒嗛挓鎵ц
+    }
+    // else if (intervalId !== null) {
+    //   clearInterval(intervalId);
+    //   intervalId = null;
+    // }
+  },
+  { immediate: true }
+);
+
+const props = defineProps({
+  deleteSim: Boolean, // 鎺ユ敹鐖剁粍浠朵紶閫掔殑鍑芥暟
+  showAddIns: Boolean,
+});
 
 // 鏂板缓鏂规瀹屾垚涔嬪悗鏂规鍒楄〃闇�瀹炴椂鍒锋柊
 watch(
@@ -231,9 +273,18 @@
     })
     .catch(() => {});
 };
-/////////////////////// 璋冪敤鎺ュ彛缁撴潫 ///////////////////////
+
+onMounted(() => {
+  getScheme(); // 椤甸潰鍔犺浇鏃剁珛鍗宠幏鍙栨暟鎹�
+});
+
 onUnmounted(() => {
   EventBus.off("hide-schemeInfo", handleHideSchemeInfo);
+
+  if (intervalId !== null) {
+    clearInterval(intervalId); // 娓呴櫎瀹氭椂鍣�
+    intervalId = null; // 閲嶇疆 intervalId
+  }
 });
 </script>
 

--
Gitblit v1.9.3