guonan
2025-06-10 2280e8be717608bb36c3cf921f129db24349396d
src/components/menu/Device.vue
@@ -72,7 +72,7 @@
<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 { getDeviceInfoShg, getDeviceInfo } from "@/api/hpApi";
import { initeWaterPrimitiveView } from "@/utils/water"; //相机flyTo函数,后续options列表中有对应经纬度后弃用
@@ -85,48 +85,46 @@
onMounted(async () => {
  try {
    await getData();
    await loadDeviceList(selectValue.value);
    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 = (val) => {
const initializeDevicePoints = () => {
  const list = [];
  val.forEach((item, index) => {
  // val.forEach((item, index) => {
  deviceListAll.value.forEach((item, index) => {
    // 根据需求可增删
    item.type = getDictName(deviceDictList, item.dictDeviceType);
    item.name = item.deviceName.split(selectValue.value)[1] || item.deviceName;
@@ -137,56 +135,38 @@
    // console.log(`设备名称: ${item.id}, 设备类型: ${item.name}`);
    createPoint(item);
  });
  deviceEntities.value = list;
};
const allDevices = ref([]);
const getData = async () => {
  const res = await getDeviceInfoShg();
  allDevices.value = res.data.pageData;
};
// 根据区域名称加载设备列表
const loadDeviceList = async (areaName) => {
  try {
    clearAllPoints();
    isLoading.value = true;
    handleCleanup();
    // const res = await getDeviceInfoShg();
    // const allDevices = res.data.pageData;
    const devicesInArea = allDevices.value.filter((item) =>
    deviceListAll.value = simStore.devices.filter((item) =>
      item.deviceName?.includes(areaName)
    );
    // deviceListAll.value = devicesInArea;
    getDeviceInfo().then((res) => {
      const list = res.data.pageData;
      deviceListAll.value = [];
      let index = 0;
      const batchSize = 50; // 每次处理的数量
      const delay = 100; // 每隔多少毫秒处理一次
      const intervalId = setInterval(() => {
        // 取出当前批次的数据
        const batch = list.slice(index, index + batchSize);
        if (batch.length === 0) {
          clearInterval(intervalId); // 数据处理完了,停止定时器
          return;
        }
        // 把当前批次的数据 push 到 deviceListAll
        deviceListAll.value = [...deviceListAll.value, ...batch];
        // 对当前批次执行初始化方法
        initializeDevicePoints(batch);
        index += batchSize;
      }, delay);
    });
    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) => {
@@ -196,6 +176,8 @@
  }
  // 根据新区域名重新加载设备列表
  loadDeviceList(item);
  initializeDevicePoints();
  // console.log(deviceListAll.value);
};
@@ -247,6 +229,7 @@
});
function handleTreeNodeClick(data) {
  initializeDevicePoints();
  // 只有设备节点才处理点击事件
  if (!data.children) {
    const entity = viewer.entities.getById(data.deviceId);
@@ -290,8 +273,8 @@
.tree-container {
  position: relative;
  height: calc(100% - 80px);
  margin-top: 10px;
  height: calc(100% - 22px);
  // margin-top: 10px;
  overflow-y: auto;
}