123
wangjuncheng
2025-06-16 4b7b8185bdce4272cd5f256fcc777056f54add6d
src/components/menu/flowRate_waterLevel.vue
@@ -1,30 +1,66 @@
<template>
   <div style="display: flex;justify-content: space-between;">
      <!-- 选点按钮 -->
      <div @click="togglePick" :class="['pick-button', { active: isPickingActive }]">
         <img src="@/assets/img/timeline/坐标.png" style="width: 26px;height: 26px;" />
         <!-- <span v-if="isPickingActive">停止拾取</span>
    <span v-else>开始拾取</span> -->
      </div>
      <!-- 开始计算按钮 -->
      <!-- <div @click="startCalculation">
         <img src="@/assets/img/timeline/水位.png" style="margin-top: -4px;width: 34px;height: 34px;" />
      </div> -->
      <!-- 结束计算按钮 -->
      <div @click="endCalculation">
         <img src="@/assets/img/timeline/清除.png" style="width: 26px;height: 26px;" />
      </div>
      <el-tooltip class="box-item" effect="dark" placement="top" show-after="1000">
         <template #content>
            水深/流速分析功能说明:
            <br /><br />
            🔹 点击按钮切换状态:
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;- 白色:关闭拾取功能(不可选点)
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;- 黄色:开启拾取功能(可点击地图选择分析点)
            <br /><br />
            🔹 使用流程:
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;1. 点击按钮切换为黄色状态 ➜ 开始拾取坐标点
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;2. 在地图上点击所需位置 ➜ 添加水深/流速分析点
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;3. 完成选点后,请将按钮切回白色 ➜ 关闭拾取功能
            <br /><br />
            ⚠️ 温馨提示:
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;使用完毕请务必关闭拾取功能,避免误操作影响其他功能。
         </template>
         <div @click="togglePick" :class="['pick-button', { active: isPickingActive }]">
            <img v-if="!isPickingActive" src="@/assets/img/timeline/流速.png" style="width: 28px;height: 28px;" />
            <img v-else src="@/assets/img/timeline/已流速.png" style="width: 28px;height: 28px;" />
         </div>
      </el-tooltip>
      <el-tooltip class="box-item" effect="dark" placement="top" show-after="1000">
         <template #content>
            清除所有分析坐标点及分析结果:
            <br /><br />
            🔁 功能说明:
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;点击后将移除地图上的所有水深/流速分析点以及相关分析图表
            <br /><br />
            ⚠️ 温馨提示:
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;此操作会清空当前分析进度,请确认后再执行
         </template>
         <div @click="endCalculation">
            <img src="@/assets/img/timeline/清除.png" style="width: 26px;height: 26px;" />
         </div>
      </el-tooltip>
   </div>
</template>
<script setup>
import { defineProps, watch, ref, onMounted, defineExpose } from "vue";
import { ElMessage } from 'element-plus';
import { getFlowRate } from "@/api/trApi.js";
import { useSimStore } from "@/store/simulation";
import { storeToRefs } from "pinia";
const simStore = useSimStore();
const { selectedScheme, currentInfo } = storeToRefs(simStore);
import { EventBus } from "@/eventBus";
const pickedPoints = ref([]);
const handler = ref(null);
const isPickingActive = ref(false);
const currentTime = ref(0);
let serviceInfo = ref(null);
const props = defineProps({
   playingTime: {
@@ -33,14 +69,6 @@
   }
});
watch(
   () => props.playingTime,
   (newVal) => {
      currentTime.value = newVal;
   },
   { immediate: true }
);
// Cesium viewer 初始化相关逻辑
const viewer = window.viewer;
function getPickPosition(windowPosition) {
@@ -49,7 +77,7 @@
   const cartesian = viewer.scene.pickPosition(windowPosition);
   if (!cartesian) return null;
   const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
   cartographic.height += 200.0;
   cartographic.height += 110.0;
   return {
      cartesian: Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height),
      longitude: Cesium.Math.toDegrees(cartographic.longitude),
@@ -58,58 +86,82 @@
   };
}
function addPointToViewer(point, index) {
   // 确保 currentTime 有合理值
   const displayTime = currentTime.value || "未设置时间";
   const schemeInfo = selectedScheme.value;
   serviceInfo = schemeInfo.serviceName;
   // 添加标签(确保实体创建时包含label属性)
   const entity = viewer.entities.add({
   // 如果已有选中点,则先清除
   if (pickedPoints.value.length >= 1) {
      endCalculation(); // 清除所有已有点
   }
   // 创建 label 实体
   const labelEntity = viewer.entities.add({
      position: point.cartesian,
      label: {
         text: `测量点 ${index + 1}\n经度: ${point.longitude.toFixed(6)}\n纬度: ${point.latitude.toFixed(6)}\n时间: ${displayTime}`,
         font: '14pt monospace',  // 减小字体大小
         text: `测量点\n水深: 等待启动...\n流速: 等待启动...`,
         font: 'bold 14pt monospace',
         style: Cesium.LabelStyle.FILL_AND_OUTLINE,
         fillColor: Cesium.Color.YELLOW,
         outlineColor: Cesium.Color.BLACK,
         outlineWidth: 2,  // 减小轮廓宽度
         outlineWidth: 2,
         verticalOrigin: Cesium.VerticalOrigin.CENTER,
         horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
         backgroundColor: Cesium.Color.fromCssColorString('rgba(0,0,0,0.7)'),
         backgroundPadding: new Cesium.Cartesian2(10, 10),  // 减小背景填充
         backgroundPadding: new Cesium.Cartesian2(10, 10),
         showBackground: true,
         scale: 1,  // 设置缩放比例
         maximumScale: 1.5,  // 设置最大缩放比例
         scale: 1,
         distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 5000),
         pixelOffsetScaleByDistance: new Cesium.NearFarScalar(100, 1.0, 5000, 0.3),
      }
   });
   // 添加垂直线
   viewer.entities.add({
      polyline: {
         positions: [point.cartesian, Cesium.Cartesian3.fromRadians(point.longitude * Math.PI / 180, point.latitude * Math.PI / 180, 0)],
         width: 2,
         material: new Cesium.PolylineOutlineMaterialProperty({
            outlineWidth: 4,
            outlineColor: Cesium.Color.WHITE
         })
   const groundPosition = Cesium.Cartesian3.fromRadians(
      point.longitude * Math.PI / 180,
      point.latitude * Math.PI / 180,
      0
   );
   const cylinderEntity = viewer.entities.add({
      position: groundPosition, // 底部位置
      cylinder: {
         length: 100.0,
         topRadius: 1.0,
         bottomRadius: 1.0,
         material: Cesium.Color.YELLOW,
         outline: true,
         outlineColor: Cesium.Color.YELLOW,
         slices: 32,
         heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
         distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 5000)
      }
   });
   // 存储点的信息(包含完整的entity引用)
   pickedPoints.value.push({
      entity: entity,
   // 存储实体引用之前就更新 currentInfo
   currentInfo.value = {
      longitude: point.longitude,
      latitude: point.latitude,
      serviceInfo: serviceInfo
   };
   // 请求数据并更新 label
   getFlowRateInfo(point.longitude, point.latitude, displayTime).then(result => {
      updateLabel(pickedPoints.value.length, result.depth, result.velocity);
   });
   // 存储实体引用
   pickedPoints.value.push({
      labelEntity,
      cylinderEntity,
      longitude: point.longitude,
      latitude: point.latitude
   });
}
function initPickHandler() {
   if (!viewer?.scene?.canvas) return;
   if (handler.value) {
      handler.value.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
      handler.value.destroy();
      handler.value = null;
   }
   handler.value = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
   handler.value.setInputAction((movement) => {
@@ -117,21 +169,18 @@
      if (position) {
         const index = pickedPoints.value.length;
         addPointToViewer(position, index);
      }
   }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
function startPicking() {
   // pickedPoints.value = [];
   // viewer.entities.removeAll();
   if (!handler.value) {
      initPickHandler(); // 只有第一次才初始化
      initPickHandler();
   }
   isPickingActive.value = true;
   ElMessage.success('开始拾取坐标,请点击地图选择点位!');
   ElMessage.success(`开始--流量流速--拾取坐标功能,请点击地图选择点位!选取完请及时关闭,避免影响其他功能!`);
}
function stopPicking() {
   if (handler.value) {
      handler.value.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
@@ -140,53 +189,82 @@
   }
   isPickingActive.value = false;
}
function togglePick() {
   if (isPickingActive.value) {
      stopPicking(); // 调用 stopPicking 来正确地停止选取过程
      isPickingActive.value = false; // 确保 isPickingActive 设置为 false
      ElMessage.success('当前坐标已选取完成!水位流速测量准备完毕!');
      console.log(pickedPoints.value, '最终选取的点');
      stopPicking();
      isPickingActive.value = false;
      ElMessage.info('已关闭--流量流速--拾取点坐标功能!');
   } else {
      startPicking();
      isPickingActive.value = true; // 在开始选取前设置为 true
      isPickingActive.value = true;
   }
}
// 当 currentTime 改变时更新所有点的 label 中的时间戳
// 修改watch逻辑
watch(
   () => props.playingTime,
   (newVal) => {
      currentTime.value = newVal || "未设置时间";
      updateAllLabels();
   async (newVal, oldVal) => {
      if (newVal !== oldVal) {
         currentTime.value = newVal || "未设置时间";
         await updateAllLabels();
      }
   },
   { immediate: true }
);
function updateAllLabels() {
   pickedPoints.value.forEach((pointInfo, index) => {
      if (pointInfo.entity && pointInfo.entity.label) {
         pointInfo.entity.label.text =
            `测量点 ${index + 1}\n经度: ${pointInfo.longitude.toFixed(6)}\n纬度: ${pointInfo.latitude.toFixed(6)}\n时间: ${currentTime.value}`;
async function updateAllLabels() {
   for (let i = 0; i < pickedPoints.value.length; i++) {
      const pointInfo = pickedPoints.value[i];
      if (!pointInfo || !pointInfo.labelEntity) continue;
      const result = await getFlowRateInfo(pointInfo.longitude, pointInfo.latitude, currentTime.value);
      updateLabel(i, result.depth, result.velocity);
   }
}
function updateLabel(index, depth, velocity) {
   const pointInfo = pickedPoints.value[index];
   if (pointInfo && pointInfo.labelEntity && pointInfo.labelEntity.label) {
      pointInfo.labelEntity.label.text = `
测量点
水深: ${depth} m
流速: ${velocity} m/s
`.trim();
   }
}
function endCalculation() {
   pickedPoints.value.forEach(pointInfo => {
      if (pointInfo.labelEntity) viewer.entities.remove(pointInfo.labelEntity);
      if (pointInfo.cylinderEntity) viewer.entities.remove(pointInfo.cylinderEntity);
   });
   pickedPoints.value = [];
   EventBus.emit("clear-water-depth");
   EventBus.emit("clear-water-velocity");
}
defineExpose({
   endCalculation,
   stopPicking
});
function getFlowRateInfo(lon, lat, time) {
   const params = {
      lon: lon,
      lat: lat,
      time: time,
      serviceName: serviceInfo
   };
   return getFlowRate(params).then(data => {
      console.log('获取到的数据:', data);
      if (data && data.code === 200) {
         return {
            depth: data.data.depth.toFixed(2),
            velocity: data.data.velocity.toFixed(2)
         };
      } else {
         return { depth: 'N/A', velocity: 'N/A' };
      }
   }).catch(error => {
      console.error('获取数据时发生错误:', error);
      return { depth: 'N/A', velocity: 'N/A' };
   });
}
function startCalculation() {
   console.log('选取的坐标点:', pickedPoints.value);
   console.log(`当前时间:${currentTime.value}`);
}
function endCalculation() {
   ElMessage.success('清除所有测量点!');
   pickedPoints.value = [];
   viewer.entities.removeAll();
   // currentTime.value = 0;
}
defineExpose({
   endCalculation
});
</script>
<style lang="less" scoped></style>