guonan
8 天以前 a57caa72a54efe9de3fe26a6c36d3e8038267377
src/components/menu/Location.vue
@@ -10,7 +10,7 @@
        <el-select
          @change="handleChange1"
          v-model="selectValue1"
          placeholder="Select"
          placeholder="请选择行政区"
          size="mini"
          style="width: 240px"
        >
@@ -27,7 +27,7 @@
        <el-select
          @change="handleChange"
          v-model="selectValue"
          placeholder="Select"
          placeholder="请选择重点沟"
          size="mini"
          style="width: 240px"
        >
@@ -57,7 +57,7 @@
          @click="handleClick(item)"
        >
          <div class="district-item-icon"></div>
          <div class="district-item-text">{{ item.hdName }}</div>
          <div class="district-item-text" :title="item.hdName">{{ item.hdName }}</div>
        </div>
      </div>
    </div>
@@ -66,74 +66,60 @@
<script setup>
import { ref, onMounted, watch, onBeforeUnmount } from "vue";
import { createPoint, removeEntities } from "@/utils/map";
import { createPoint, clearAllPoints } from "@/utils/map";
import { useSimStore } from "@/store/simulation";
import { initeWaterPrimitiveView } from "@/utils/water"; //相机flyTo函数,后续options列表中有对应经纬度后弃用
import { useRoute, onBeforeRouteUpdate } from "vue-router";
import { Loading } from "@element-plus/icons-vue";
import { fetchAndLoadDangerPoints } from "@/api/hpApi.js";
import { getDangerPointByPage } from "@/api/hpApi";
const districtList = ref([]);
const displayData = ref([]);
const loadCallback = async (newData) => {
  districtList.value = [...newData];
  console.log(districtList.value, "aaaaaaaaaaaaaaaaaaaaaaaaa");
  await initializeDevicePoints();
};
onMounted(() => {
  fetchAndLoadDangerPoints(loadCallback);
});
const simStore = useSimStore();
// onBeforeRouteUpdate((to, from, next) => {
//   if (to.path !== "/yhgl") {
//     handleCleanup();
//   }
//   next();
// });
const route = useRoute();
onBeforeUnmount(() => {
  if (route.path !== "/yhgl") {
    handleCleanup();
    clearAllPoints();
  }
});
const selectValue = ref("孙胡沟");
const selectValue1 = ref("北京市");
const selectValue1 = ref("");
const BJoptions = ref([]);
const BJoptions = 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" },
]);
const options = ref([
  {
    value: "孙胡沟",
    label: "孙胡沟",
  },
  {
    value: "鱼水洞后沟",
    label: "鱼水洞后沟",
  },
  {
    value: "于家西沟",
    label: "于家西沟",
  },
  {
    value: "北河沟",
    label: "北河沟",
  },
  {
    value: "龙泉峪村",
    label: "龙泉峪村",
  },
  { value: "孙胡沟", label: "孙胡沟" },
  { value: "鱼水洞后沟", label: "鱼水洞后沟" },
  { value: "于家西沟", label: "于家西沟" },
  { value: "北河沟", label: "北河沟" },
  { value: "龙泉峪村", label: "龙泉峪村" },
]);
const loading = ref(true); // 控制加载状态
function handleClick(district) {
  if (selectValue.value) {
    // 此处调用是因为GisView页面会在点击下一乡镇之前把上一个选择的区域的隐患点清除掉(如果刚好选择了孙胡沟,那么下一个点击将会清空孙胡沟的隐患点)
    initializeDevicePoints();
  } else {
    // 行政区划的点位太多了,只能选中哪个渲染哪个
    DevicePoints(district);
  }
  const entity = viewer.entities.getById(district.hdId);
  if (entity) {
    viewer.flyTo(entity, {
@@ -145,11 +131,7 @@
    });
  }
}
const handleCleanup = async () => {
  await Promise.all(
    districtList.value.map((item) => removeEntities(item.hdId))
  );
};
const initializeDevicePoints = async () => {
  await Promise.all(
    districtList.value.map(async (item, index) => {
@@ -168,8 +150,20 @@
  );
};
const DevicePoints = async (item) => {
  // 根据需求可增删
  item.id = item.hdId;
  item.name = item.hdName;
  item.latitude = item.lat;
  item.longitude = item.lon;
  item.showBillboard = true;
  item.type = item.disasterType;
  item.className = "district";
  await createPoint(item);
};
const filterDataByArea = async (areaName) => {
  handleCleanup();
  clearAllPoints();
  if (!areaName || !simStore.DangerPoint || simStore.DangerPoint.length === 0) {
    districtList.value = [];
    return;
@@ -179,39 +173,65 @@
  );
  if (JSON.stringify(districtList.value) !== JSON.stringify(filteredData)) {
    // districtList.value = filteredData;
    districtList.value = filteredData;
    await initializeDevicePoints();
  }
};
const handleChange1 = async (item) => {
  if (!item) {
    ElMessage("请选择一个区域");
    return;
  }
  selectValue1.value = item;
  selectValue.value = "";
  // 开启加载状态
  // loading.value = true;
  districtList.value = []; // 清空旧数据
  let pageNum = 1;
  let hasMore = true;
  try {
    while (hasMore) {
      const res = await getDangerPointByPage(item, pageNum);
      // 将新数据追加到列表中
      districtList.value = [...districtList.value, ...res.data.pageData];
      // 判断是否还有下一页
      if (pageNum < res.data.pageCount) {
        pageNum++;
        await new Promise((resolve) => setTimeout(resolve, 300));
      } else {
        hasMore = false;
      }
    }
  } catch (err) {
    console.error("获取隐患点失败", err);
    districtList.value = []; // 可选:清空或保留已加载部分
  } finally {
    // 暂时先关闭加载状态,因为此处为分页请求数据,然后一点一点的渲染,最终会加载完成
    // 如果开启加载状态,最后还是会等所有数据都请求回来才会渲染
    // loading.value = false; // 关闭加载状态
  }
};
// 处理区域变化事件
const handleChange = (item) => {
  const areaName = item;
  if (!areaName) {
  selectValue1.value = "";
  selectValue.value = item;
  if (!item) {
    ElMessage.warning("请选择一个区域");
    return;
  }
  filterDataByArea(areaName);
  filterDataByArea(item);
};
let isInitialized = false;
watch(
  () => simStore.DangerShowSwitch,
  async (newValue, oldValue) => {
    console.log("当前状态:", newValue);
    if (newValue) {
      if (!isInitialized) {
        await initializeDevicePoints();
        isInitialized = true;
      }
    } else {
      handleCleanup();
      isInitialized = false;
    }
  }
);
// 监听 simStore.DangerPoint 变化
watch(
  () => simStore.DangerPoint,
@@ -220,7 +240,7 @@
      filterDataByArea(selectValue.value);
      loading.value = false; // 数据加载完成
    } else {
      handleCleanup();
      clearAllPoints();
      districtList.value = [];
      loading.value = true; // 数据未准备就绪
    }
@@ -228,7 +248,7 @@
);
onMounted(() => {
  handleCleanup();
  clearAllPoints();
  // initeWaterPrimitiveView();
  // 默认先检查一遍数据
  if (simStore.DangerPoint && simStore.DangerPoint.length > 0) {