guonan
2025-07-01 62c24bfb0f54998fceeb3b37eb14a74e6bfd83e9
src/components/monifangzhen/schemeCard.vue
@@ -23,7 +23,12 @@
          <el-button size="small" @click="setSchemClick(item)"
            >方案详情</el-button
          >
          <el-button size="small" @click="startPlay(item)">进入模拟</el-button>
          <el-button
            size="small"
            v-show="item.type !== 2"
            @click="startPlay(item)"
            >进入模拟</el-button
          >
          <!--  :disabled="item.status !== 2" -->
        </div>
      </div>
@@ -74,7 +79,6 @@
  selectedId.value = id;
}
function formatTime(time) {
  return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
}
@@ -94,7 +98,11 @@
  messageShow.value = false;
}
// 实时模拟五分钟请求一次的定时器
const realTimeSimInterval = ref(null);
async function startPlay(item) {
  console.log(item, "item");
  if (item.status === 2) {
    ElMessage.warning("当前方案正在分析中,无法进入模拟!");
    return;
@@ -127,7 +135,7 @@
    return;
  }
  // 调用求解器(不在实时模拟的情况下)
  // 新建方案,没有 status 和 serviceName 且 type != 2
  if (!item.status && !item.serviceName && item.type !== 2) {
    try {
      await getSimStart(item.id);
@@ -143,23 +151,21 @@
    return;
  }
  // 实时模拟
  // 处理 type == 2 的情况(实时模拟)
  if (item.type === 2) {
    try {
      // 实时模拟调用求解器会直接在接口中返回结果
      const ress = await getSimStart(item.id);
      const res = await getSimDataById(item.id);
      item.serviceName = res.data[0]?.serviceName || null;
      simStore.setSelectedScheme(item);
      getScheme();
      if (ress.code === 200) {
        initeWaterPrimitiveView();
        emit("start");
      }
    } catch (e) {
      console.error("实时模拟获取模拟数据失败:", e);
    // 清除已有定时器,防止重复启动
    if (realTimeSimInterval.value) {
      clearInterval(realTimeSimInterval.value);
    }
    // 即刻执行一次
    await executeRealTimeSimulation(item);
    // 每隔 5 分钟执行一次
    realTimeSimInterval.value = setInterval(() => {
      executeRealTimeSimulation(item);
    }, 5 * 60 * 1000); // 5分钟
    return;
  }
@@ -167,7 +173,26 @@
  simStore.setSelectedScheme(item);
}
// 封装实时模拟的异步操作
async function executeRealTimeSimulation(item) {
  try {
    const ress = await getSimStart(item.id);
    const res = await getSimDataById(item.id);
    item.serviceName = res.data[0]?.serviceName || null;
    simStore.setSelectedScheme(item);
    getScheme();
    if (ress.code === 200) {
      simStore.layerDate = ress.data;
      initeWaterPrimitiveView();
      emit("start");
    }
  } catch (e) {
    console.error("实时模拟获取模拟数据失败:", e);
  }
}
function handleBack(value) {
  if (value === false) {
@@ -195,9 +220,11 @@
      (item) =>
        item.result == "创建仿真" ||
        item.result == "完成" ||
        item.result == "-1"
        item.result == "-1" ||
        item.result == null
    );
    simAPIStore.shouldPoll = !shouldStop; // 修改 Pinia 状态
    console.log(shouldStop, "aaaaaaaaaaaaaaaa");
    // 3. 如果需要停止
    if (shouldStop) {
      if (intervalId) {
@@ -216,7 +243,6 @@
watch(
  () => simAPIStore.shouldPoll,
  (isStarted) => {
    console.log(isStarted, "定时器");
    if (isStarted) {
      getScheme(); // 首次立即获取一次
      intervalId = setInterval(getScheme, 60 * 1000); // 每隔一分钟执行