guonan
6 天以前 a57caa72a54efe9de3fe26a6c36d3e8038267377
src/components/monifangzhen/schemeCard.vue
@@ -23,7 +23,15 @@
          <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
          >
          <el-button size="small" v-show="item.type == 2" @click="rePlay(item)"
            >历史回放</el-button
          >
          <!--  :disabled="item.status !== 2" -->
        </div>
      </div>
@@ -45,7 +53,15 @@
<script setup>
import { EventBus } from "@/eventBus"; // 引入事件总线
import { onMounted, ref, watch, defineEmits, onUnmounted } from "vue";
import {
  nextTick,
  onMounted,
  ref,
  watch,
  defineEmits,
  onUnmounted,
  inject,
} from "vue";
import dayjs from "dayjs";
import { initeWaterPrimitiveView } from "@/utils/water";
import Message from "@/components/tools/Message.vue";
@@ -56,12 +72,16 @@
import { ElMessage, ElMessageBox } from "element-plus";
const emit = defineEmits(["start", "end", "reset", "closeBtn"]);
import {
  getRegionData,
  getSimData,
  deleteSimData,
  getSimStart,
  getSimDataById,
  getSimresult,
} from "@/api/trApi.js";
import { getAeraTownCode, getDeviceNWJ } from "@/api/hpApi";
import { createPoint, removeEntities, clearAllPoints } from "@/utils/map";
import { deviceDictList, getDictName } from "@/constant/dict.js";
const simStore = useSimStore();
const simAPIStore = SimAPIStore();
@@ -96,7 +116,146 @@
// 实时模拟五分钟请求一次的定时器
const realTimeSimInterval = ref(null);
const { startSimulate, endSimulate } = inject("simulateActions");
const BJCode = ref([
  { label: "密云区", value: "110118000000" },
  { label: "房山区", value: "110111000000" },
  { label: "门头沟区", value: "110109000000" },
  { label: "延庆区", value: "110119000000" },
  { label: "怀柔区", value: "110116000000" },
  { label: "昌平区", value: "110114000000" },
  { label: "平谷区", value: "110117000000" },
  { label: "海淀区", value: "110108000000" },
  { label: "石景山区", value: "110107000000" },
  { label: "丰台区", value: "110106000000" },
]);
async function startPlay(item) {
  simStore.openDia = false;
  clearAllPoints();
  const areaName = item.areaName;
  let districtCode;
  // 1. 判断是否包含 “区”
  if (!areaName.includes("区")) {
    console.log(
      `方案中模拟【${areaName}】不包含“区”,使用默认编码:怀柔区(110116000000)`
    );
    districtCode = "110116000000"; // 手动指定为怀柔区编码
  } else {
    // 2. 在 BJCode 中查找匹配的区域 value
    const matchedArea = BJCode.value.find((area) => area.label === areaName);
    if (!matchedArea) {
      console.warn(`未找到 ${areaName} 对应的区域编码`);
      return;
    }
    districtCode = matchedArea.value;
  }
  // 1. 获取乡镇区域编码
  const areaRes = await getAeraTownCode(districtCode);
  const districtCodes = areaRes.data.map((item) => item.districtCode);
  // 2. 泥位计类型ID
  const ids = "1437295811";
  // 3. 并行请求所有设备数据
  const requests = districtCodes.map((code) =>
    getDeviceNWJ(ids, code)
      .then((res) => res.data?.pageData || []) // 安全提取 pageData
      .catch((err) => {
        console.error(`请求失败 (code: ${code})`, err);
        return []; // 出错时也返回空数组,避免 Promise.all 中断
      })
  );
  // 4. 等待所有请求完成
  const allPageDataArrays = await Promise.all(requests);
  // 5. 合并二维数组为一维数组
  const mergedPageData = allPageDataArrays.flat();
  // 6. 如果不是“区”,则过滤出 deviceName 包含 "孙胡沟" 的设备
  const filteredPageData = areaName.includes("区")
    ? mergedPageData
    : mergedPageData.filter((device) => device.deviceName.includes("孙胡沟"));
  // 孙胡沟设备经纬度映射
  const deviceMapping = {
    怀柔区琉璃庙镇孙胡沟椴树底下东沟泥位计5007: {
      lon: 116.598891,
      lat: 40.554979,
    },
    怀柔区琉璃庙镇孙胡沟村上台子河东南沟泥位计5006: {
      lon: 116.593215,
      lat: 40.554399,
    },
  };
  let displayedData = filteredPageData;
  if (!areaName.includes("区")) {
    // 添加 lon 和 lan 字段
    displayedData = filteredPageData.map((device) => {
      const mapping = deviceMapping[device.deviceName];
      return {
        ...device,
        ...(mapping && { lon: mapping.lon, lat: mapping.lat }), // 如果 mapping 存在,才添加
      };
    });
    // 添加额外的两个点位
    const extraPoint1 = {
      deviceName: "弯沟1",
      longitude: 116.597836,
      latitude: 40.564962,
      // height: 530.14,
      type: "泥位计",
      lon: 116.597836,
      lat: 40.564962,
      dictDeviceType: "1437295811",
      deviceId: "custom_extraPoint1", // 手动加一个唯一 ID
    };
    const extraPoint2 = {
      deviceName: "弯沟2",
      longitude: 116.591571,
      latitude: 40.573093,
      // height: 483.89,
      type: "泥位计",
      lon: 116.591571,
      lat: 40.573093,
      dictDeviceType: "1437295811",
      deviceId: "custom_extraPoint2", // 手动加一个唯一 ID
    };
    displayedData = [...displayedData, extraPoint1, extraPoint2];
    // displayedData = [...displayedData, extraPoint1];
  }
  console.log(
    displayedData,
    areaName.includes("区")
      ? "全部泥位计设备列表"
      : "孙胡沟泥位计 + 额外点位列表"
  );
  // 7. 创建点
  displayedData.forEach((item) => {
    // 根据需求可增删
    item.type = getDictName(deviceDictList, item.dictDeviceType);
    item.name = item.deviceName;
    item.id = item.deviceId;
    item.className = "device";
    item.showLabel = true;
    createPoint(item);
  });
  if (item.status === 2) {
    ElMessage.warning("当前方案正在分析中,无法进入模拟!");
    return;
@@ -125,7 +284,7 @@
    currentScheme.value = item;
    schemeInfoShow.value = true;
    emit("closeBtn", false);
    emit("start");
    startSimulate();
    return;
  }
@@ -145,49 +304,35 @@
    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);
}
// 封装实时模拟的异步操作
async function executeRealTimeSimulation(item) {
  try {
    const ress = await getSimStart(item.id);
    console.log(ress, "resssssssss");
    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;
      console.log(simStore.layerDate,'aaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb')
      initeWaterPrimitiveView();
      emit("start");
    }
  } catch (e) {
    console.error("实时模拟获取模拟数据失败:", e);
  }
// 实时模拟历史回放
function rePlay(item) {
  // 当前选中的方案
  simStore.setSelectedScheme(item);
  // 拿id去请求results接口,如果长度不为0,则可以进行历史回放
  getSimresult(item.id)
    .then((res) => {
      if (res.code == 500) {
        // 如果长度为0,提示用户并且不进行后续操作
        ElMessage.warning("提示:没有可回放的数据!");
        return; // 阻止后续操作
      } else {
        simStore.rePlayList = res.data;
        console.log(simStore.rePlayList, "实时模拟历史回放列表");
      }
      // 使用 nextTick 确保 DOM 更新后再执行后续操作
      nextTick(() => {
        initeWaterPrimitiveView();
        startSimulate();
      });
    })
    .catch((error) => {
      console.log("请求失败:", error);
      // 错误处理
    });
}
function handleBack(value) {
@@ -216,7 +361,9 @@
      (item) =>
        item.result == "创建仿真" ||
        item.result == "完成" ||
        item.result == "-1"
        item.result == "-1" ||
        item.result == "已停止" ||
        item.result == "运行中"
    );
    simAPIStore.shouldPoll = !shouldStop; // 修改 Pinia 状态
    // 3. 如果需要停止
@@ -237,7 +384,6 @@
watch(
  () => simAPIStore.shouldPoll,
  (isStarted) => {
    console.log(isStarted, "定时器");
    if (isStarted) {
      getScheme(); // 首次立即获取一次
      intervalId = setInterval(getScheme, 60 * 1000); // 每隔一分钟执行