guonan
2025-07-01 62c24bfb0f54998fceeb3b37eb14a74e6bfd83e9
src/components/monifangzhen/schemeCard.vue
@@ -16,10 +16,6 @@
          <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> -->
        </p>
      </div>
      <div class="cardMenu">
@@ -27,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>
@@ -37,10 +38,7 @@
      :selectedScheme="currentScheme"
      @back="handleBack"
    />
    <flowRateTab
    v-if="schemeInfoShow">
      123
    </flowRateTab>
    <flowRateTab v-if="schemeInfoShow"> 123 </flowRateTab>
  </div>
  <Message
    @close="close"
@@ -81,14 +79,6 @@
  selectedId.value = id;
}
const statusText = {
  0: "创建仿真",
  1: "预处理",
  2: "分析中",
  10: "完成",
  20: "出错",
};
function formatTime(time) {
  return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
}
@@ -108,61 +98,100 @@
  messageShow.value = false;
}
function startPlay(item) {
  // 分析中
  if (item.status == 2) {
// 实时模拟五分钟请求一次的定时器
const realTimeSimInterval = ref(null);
async function startPlay(item) {
  console.log(item, "item");
  if (item.status === 2) {
    ElMessage.warning("当前方案正在分析中,无法进入模拟!");
    return;
  }
  // 出错
  if (item.status == 20) {
  if (item.status === 20) {
    ElMessage.error("当前方案分析出错,请重新新建方案!");
    return;
  }
  // 调用求解器并拿到最新生成的serviceName
  // 新创建的方案没有状态以及serviceName则执行调用求解器
  if (!item.status && !item.serviceName) {
    getSimStart(item.id).then((res) => {
      getSimDataById(item.id).then((res) => {
        item.serviceName = res.data[0].serviceName;
        simStore.setSelectedScheme(item);
        console.log(item, "无服务名称");
        ElMessage.warning("当前方案正在分析中,请稍后再模拟");
        getScheme();
      });
    });
  } else {
  // 如果是已完成的方案(status == 10)
  if (item.status === 10) {
    const flyHeight = item.areaType === 1 ? 100000 : 50000;
    simStore.setSelectedScheme(item);
    console.log("有服务名称");
  }
  const flyHeight = ref(100000);
  const shouldShowFill = false;
  // 求解器求解完成之后才可以显示时间轴
  if (item.status == 10) {
    // 只有行政区划执行
    if (item.areaType == 1) {
      flyHeight.value = 100000;
    if (item.areaType === 1) {
      EventBus.emit("select-geom", {
        geom: item.geom,
        flyHeight: flyHeight.value,
        shouldShowFill: shouldShowFill,
        flyHeight,
        shouldShowFill: false,
      });
    } else {
      // 孙胡沟区域跳转视角
      initeWaterPrimitiveView();
    }
    currentScheme.value = item;
    schemeInfoShow.value = true;
    emit("closeBtn", false);
    emit("start");
    return;
  }
  // 新建方案,没有 status 和 serviceName 且 type != 2
  if (!item.status && !item.serviceName && item.type !== 2) {
    try {
      await getSimStart(item.id);
      const res = await getSimDataById(item.id);
      item.serviceName = res.data[0]?.serviceName || null;
      simStore.setSelectedScheme(item);
      ElMessage.warning("当前方案正在分析中,请稍后再模拟");
      getScheme();
    } catch (e) {
      console.error("获取模拟数据失败:", e);
    }
    return;
  }
  // 处理 type == 2 的情况(实时模拟)
  if (item.type === 2) {
    // 清除已有定时器,防止重复启动
    if (realTimeSimInterval.value) {
      clearInterval(realTimeSimInterval.value);
    }
    // 即刻执行一次
    await executeRealTimeSimulation(item);
    // 每隔 5 分钟执行一次
    realTimeSimInterval.value = setInterval(() => {
      executeRealTimeSimulation(item);
    }, 5 * 60 * 1000); // 5分钟
    return;
  }
  // 默认情况:有服务名称
  simStore.setSelectedScheme(item);
}
function endPlay() {
  emit("end");
// 封装实时模拟的异步操作
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) {
@@ -191,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) {
@@ -212,7 +243,6 @@
watch(
  () => simAPIStore.shouldPoll,
  (isStarted) => {
    console.log(isStarted, "定时器");
    if (isStarted) {
      getScheme(); // 首次立即获取一次
      intervalId = setInterval(getScheme, 60 * 1000); // 每隔一分钟执行