guonan
2025-06-10 2280e8be717608bb36c3cf921f129db24349396d
src/components/menu/Device.vue
@@ -72,9 +72,9 @@
<script setup>
import { ref, computed, onMounted, watch, onBeforeUnmount } from "vue";
import { useRoute, onBeforeRouteUpdate } from "vue-router";
import { createPoint, removeEntities } from "@/utils/map";
import { createPoint, removeEntities, clearAllPoints } from "@/utils/map";
import { deviceDictList, getDictName } from "@/constant/dict.js";
import { getDeviceInfo } from "@/api/hpApi";
import { getDeviceInfoShg, getDeviceInfo } from "@/api/hpApi";
import { initeWaterPrimitiveView } from "@/utils/water"; //相机flyTo函数,后续options列表中有对应经纬度后弃用
import { useSimStore } from "@/store/simulation";
import { Loading } from "@element-plus/icons-vue";
@@ -85,47 +85,45 @@
onMounted(async () => {
  try {
    await getData();
    await loadDeviceList(selectValue.value);
    initeWaterPrimitiveView();
    if (simStore.devices && simStore.devices.length > 0) {
      await loadDeviceList(selectValue.value);
    }
    // initeWaterPrimitiveView();
  } finally {
    isLoading.value = false;
  }
});
onBeforeRouteUpdate((to, from, next) => {
  if (to.path !== "/zhjc") {
    handleCleanup();
  }
  next();
});
// onBeforeRouteUpdate((to, from, next) => {
//   if (to.path !== "/zhjc") {
//     handleCleanup();
//   }
//   next();
// });
const route = useRoute();
onBeforeUnmount(() => {
  if (route.path !== "/zhjc") {
    handleCleanup();
    clearAllPoints();
  }
});
watch(
  () => simStore.DeviceShowSwitch,
  (newValue, oldValue) => {
    if (newValue) {
      initializeDevicePoints();
    } else {
      handleCleanup();
    }
  }
);
// watch(
//   () => simStore.DeviceShowSwitch,
//   (newValue, oldValue) => {
//     if (newValue) {
//       initializeDevicePoints();
//     } else {
//       clearAllPoints();
//     }
//   }
// );
const deviceListAll = ref([]);
const deviceEntities = ref([]);
const handleCleanup = () => {
  deviceListAll.value.forEach((item) => {
    removeEntities(item.deviceId);
  });
};
const initializeDevicePoints = () => {
  const list = [];
  // val.forEach((item, index) => {
  deviceListAll.value.forEach((item, index) => {
    // 根据需求可增删
    item.type = getDictName(deviceDictList, item.dictDeviceType);
@@ -137,32 +135,38 @@
    // console.log(`设备名称: ${item.id}, 设备类型: ${item.name}`);
    createPoint(item);
  });
  deviceEntities.value = list;
};
const allDevices = ref([]);
const getData = async () => {
  const res = await getDeviceInfo();
  allDevices.value = res.data.pageData;
};
// 根据区域名称加载设备列表
const loadDeviceList = async (areaName) => {
  try {
    clearAllPoints();
    isLoading.value = true;
    handleCleanup();
    // const res = await getDeviceInfo();
    // const allDevices = res.data.pageData;
    const devicesInArea = allDevices.value.filter((item) =>
    deviceListAll.value = simStore.devices.filter((item) =>
      item.deviceName?.includes(areaName)
    );
    deviceListAll.value = devicesInArea;
    deviceListAll.length = 0;
    initializeDevicePoints();
    await initializeDevicePoints();
  } catch (error) {
    console.error("加载设备信息失败", error);
  } finally {
    isLoading.value = false;
  }
};
// 监听 simStore.devices 变化
watch(
  () => simStore.devices,
  (newVal) => {
    if (newVal && newVal.length > 0) {
      loadDeviceList(selectValue.value);
    } else {
      clearAllPoints();
      deviceListAll.value = [];
    }
  }
);
// 处理区域变化事件
const handleChange = (item) => {
@@ -172,6 +176,8 @@
  }
  // 根据新区域名重新加载设备列表
  loadDeviceList(item);
  initializeDevicePoints();
  // console.log(deviceListAll.value);
};
@@ -223,6 +229,7 @@
});
function handleTreeNodeClick(data) {
  initializeDevicePoints();
  // 只有设备节点才处理点击事件
  if (!data.children) {
    const entity = viewer.entities.getById(data.deviceId);
@@ -266,8 +273,8 @@
.tree-container {
  position: relative;
  height: calc(100% - 80px);
  margin-top: 10px;
  height: calc(100% - 22px);
  // margin-top: 10px;
  overflow-y: auto;
}