guonan
2025-06-10 2280e8be717608bb36c3cf921f129db24349396d
src/components/menu/Location.vue
@@ -5,20 +5,57 @@
    </div>
    <div class="left-content district-content">
      <div style="margin-left: 5px; margin-bottom: 5px">
        <span style="color: white">北京市:</span>
        <el-select
          @change="handleChange1"
          v-model="selectValue1"
          placeholder="Select"
          size="mini"
          style="width: 240px"
        >
          <el-option
            v-for="item in BJoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div style="margin-left: 5px">
        <span style="color: white">重点沟:</span>
        <el-select @change="handleChange" v-model="selectValue" placeholder="Select" size="large" style="width: 240px">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        <el-select
          @change="handleChange"
          v-model="selectValue"
          placeholder="Select"
          size="mini"
          style="width: 240px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <!-- 滚动区域 -->
      <div style="overflow-y: auto; height: 95%">
      <div style="overflow-y: auto; height: 91%; position: relative">
        <!-- 加载遮罩层 -->
        <div v-if="loading" class="loading-overlay">
          <div class="spinner"></div>
          <div class="loading-content">
            <el-icon class="loading-icon"><Loading /></el-icon>
            <span class="loading-text">数据加载中...</span>
          </div>
        </div>
        <div v-else v-for="(item, key) in districtList" :key="key" class="district-item" @click="handleClick(item)">
        <div
          v-else
          v-for="(item, key) in districtList"
          :key="key"
          class="district-item"
          @click="handleClick(item)"
        >
          <div class="district-item-icon"></div>
          <div class="district-item-text">{{ item.hdName }}</div>
        </div>
@@ -28,111 +65,162 @@
</template>
<script setup>
import { ref, computed, onMounted } from "vue";
import { deviceDictList, getDictName } from "@/constant/dict.js";
import { ref, onMounted, watch, onBeforeUnmount } from "vue";
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";
// 定义一个响应式引用存储设备列表
const deviceListAll = ref([]);
const districtList = ref([]);
// 当前选中的区域
const simStore = useSimStore();
const route = useRoute();
onBeforeUnmount(() => {
  if (route.path !== "/yhgl") {
    clearAllPoints();
  }
});
const selectValue = ref("孙胡沟");
// 区域选项
const selectValue1 = ref("北京市");
const BJoptions = ref([]);
const options = ref([
  { value: "孙胡沟", label: "孙胡沟" },
  { value: "鱼水洞后沟", label: "鱼水洞后沟" },
  // 其他选项...
  {
    value: "孙胡沟",
    label: "孙胡沟",
  },
  {
    value: "鱼水洞后沟",
    label: "鱼水洞后沟",
  },
  {
    value: "于家西沟",
    label: "于家西沟",
  },
  {
    value: "北河沟",
    label: "北河沟",
  },
  {
    value: "龙泉峪村",
    label: "龙泉峪村",
  },
]);
// 树形结构属性配置
const treeProps = {
  label: "deviceName",
  children: "children",
const loading = ref(true); // 控制加载状态
function handleClick(district) {
  // 此处调用是因为GisView页面会在点击下一乡镇之前把上一个选择的区域的隐患点清除掉(如果刚好选择了孙胡沟,那么下一个点击将会清空孙胡沟的隐患点)
  initializeDevicePoints();
  const entity = viewer.entities.getById(district.hdId);
  if (entity) {
    viewer.flyTo(entity, {
      offset: {
        heading: Cesium.Math.toRadians(0),
        pitch: Cesium.Math.toRadians(-45),
        range: 4000,
      },
    });
  }
}
const initializeDevicePoints = async () => {
  await Promise.all(
    districtList.value.map(async (item, index) => {
      // 根据需求可增删
      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);
      // 打印每个设备的名称和设备类型
      // console.log(`设备名称: ${item.id}, 设备类型: ${item.name}`);
    })
  );
};
const filterDataByArea = async (areaName) => {
  clearAllPoints();
  if (!areaName || !simStore.DangerPoint || simStore.DangerPoint.length === 0) {
    districtList.value = [];
    return;
  }
  const filteredData = simStore.DangerPoint.filter((item) =>
    item.position?.includes(areaName)
  );
  if (JSON.stringify(districtList.value) !== JSON.stringify(filteredData)) {
    districtList.value = filteredData;
    await initializeDevicePoints();
  }
};
// 处理区域变化事件
const handleChange = (areaName) => {
const handleChange = (item) => {
  const areaName = item;
  if (!areaName) {
    console.error("请选择一个区域");
    ElMessage.warning("请选择一个区域");
    return;
  }
  selectValue.value = areaName; // 更新选中的区域值
  console.log(deviceListAll.value); // 这里已包含所有区域的数据
  filterDataByArea(areaName);
};
let isInitialized = false;
// 计算属性:将设备列表转换为树形结构,依据当前选中的区域
const deviceTree = computed(() => {
  const typeMap = {};
// watch(
//   () => simStore.DangerShowSwitch,
//   async (newValue, oldValue) => {
//     console.log("当前状态:", newValue);
  // 过滤出属于当前选中区域的设备
  const filteredDevices = deviceListAll.value.filter(device =>
    device.deviceName.includes(selectValue.value)
  );
  // 按设备类型分组
  filteredDevices.forEach((device) => {
    const typeName = getDictName(deviceDictList, device.dictDeviceType);
    if (!typeName) {
      console.warn("未找到设备类型:", device);
      return;
//     if (newValue) {
//       if (!isInitialized) {
//         await initializeDevicePoints();
//         isInitialized = true;
//       }
//     } else {
//       clearAllPoints();
//       isInitialized = false;
//     }
//   }
// );
// 监听 simStore.DangerPoint 变化
watch(
  () => simStore.DangerPoint,
  (newVal) => {
    if (newVal && newVal.length > 0) {
      filterDataByArea(selectValue.value);
      loading.value = false; // 数据加载完成
    } else {
      clearAllPoints();
      districtList.value = [];
      loading.value = true; // 数据未准备就绪
    }
  }
);
    if (!typeMap[typeName]) {
      typeMap[typeName] = [];
    }
    // 直接使用原始的设备名称,不进行任何替换操作
    typeMap[typeName].push({
      ...device,
      deviceName: device.deviceName.trim(), // 只去除首尾空格
    });
  });
  // 转换为树形结构
  return Object.keys(typeMap).map((typeName) => ({
    deviceName: typeName,
    children: typeMap[typeName],
  }));
});
// 假设在组件初始化之前,deviceListAll 已被填充了所有区域的数据
// 如果不是这样,则需要保留对 loadDeviceList 的调用,或者找到一种方法来预填充 deviceListAll
onMounted(() => {
  // 如果需要在此处加载全部数据,请取消注释以下行并确保 getDeviceInfo 返回所有区域的数据
  // loadDeviceList("");
  clearAllPoints();
  // initeWaterPrimitiveView();
  // 默认先检查一遍数据
  if (simStore.DangerPoint && simStore.DangerPoint.length > 0) {
    filterDataByArea("孙胡沟");
    loading.value = false;
  } else {
    loading.value = true;
  }
});
</script>
<style lang="less" scoped>
.loading-overlay {
  position: absolute;
  top: 120px;
  left: 0px;
  right: 0px;
  bottom: 10px;
  background-color: rgba(236, 233, 233, 0.5);
  /* 半透明遮罩 */
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #fff;
  border-top: 4px solid transparent;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
.district {
  position: absolute;
  width: 345px;
@@ -151,6 +239,8 @@
.district-content {
  padding: 10px;
  box-sizing: border-box;
  height: calc(100% - 70px);
  position: relative;
}
.district-item-icon {
@@ -168,12 +258,44 @@
  overflow: hidden;
  text-overflow: ellipsis;
}
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  // background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  // border-radius: 4px;
}
.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #fff;
}
.loading-icon {
  font-size: 24px;
  margin-bottom: 8px;
  animation: rotating 2s linear infinite;
}
.loading-text {
  color: white;
  font-size: 14px;
  text-align: center;
  margin-top: 20px;
}
@keyframes rotating {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
/deep/ .el-select__placeholder {