wangjuncheng
2025-04-28 9567f017a3423bafafaf5f22ec1e5541d2b0f4bd
change
已修改2个文件
309 ■■■■■ 文件已修改
src/components/menu/TimeLine.vue 131 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/water.js 178 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/components/menu/TimeLine.vue
@@ -82,7 +82,13 @@
  inject,
} from "vue";
import dayjs from "dayjs";
import { createWaterPrimitive, destoryWaterPrimitive } from "@/utils/water";
import {
  createWaterPrimitive,
  destoryWaterPrimitive,
  pauseWaterSimulation,
  resumeWaterSimulation,
  setTimeForWaterSimulation,
} from "@/utils/water";
import { fetchWaterSimulationData } from "@/api/trApi.js";
import { EventBus } from "@/eventBus";
import { ElMessage } from "element-plus";
@@ -105,13 +111,14 @@
const playbackFinished = ref(true);
const currentTime = ref(0);
const duration = ref(60); // 一天的秒数
const playbackRate = ref(8);
const playbackRate = ref(1);
const playbackRates = ref([1, 2, 4, 8]);
const showSpeedMenu = ref(false);
const waterTimestamps = ref([]); // 存储时间轴数据
const timeMarkers = ref([]);
const timelineTrack = ref(null);
// 新增标识变量
const isWaterPrimitiveCreated = ref(false);
let playInterval = null;
// 计算属性
@@ -128,35 +135,46 @@
const currentTimeFormatted = computed(() => formatTime(currentTime.value));
// 播放控制
// 播放控制
const togglePlay = () => {
  // 如果当前是停止状态且已经播放完毕,点击时重置时间
  if (!isPlaying.value && currentTime.value >= duration.value)
    currentTime.value = 0;
    currentTime.value = 0; // 如果已经播放完毕,重置时间
  isPlaying.value = !isPlaying.value;
  emit("isPlaying", isPlaying.value);
  if (isPlaying.value) {
    startPlayback();
    // 如果是从头开始播放
    if (!isWaterPrimitiveCreated.value) {
      // 第一次播放时创建水体模拟层
      createWaterPrimitive({ interval: intervalMap[playbackRate.value] });
      isWaterPrimitiveCreated.value = true; // 标记为已创建
    } else {
      // 后续播放时调用恢复接口
      resumeWaterSimulation();
    }
    if (currentTime.value === 0) emit("playbackFinished", false);
  } else stopPlayback();
  } else {
    stopPlayback();
    pauseWaterSimulation(); // 调用暂停接口
  }
};
const intervalMap = {
  1: 1000, // 1倍速
  2: 500, // 2倍速
  4: 250, // 4倍速
  8: 125, // 8倍速
};
// 播放逻辑
const startPlayback = () => {
  // 根据当前倍速获取对应的 interval
  const interval = intervalMap[playbackRate.value] || 1000; // 默认为1000
  // 调用 createWaterPrimitive 并传递 interval
  createWaterPrimitive({ interval });
  clearInterval(playInterval);
  clearInterval(playInterval); // 确保清除之前的定时器
  playInterval = setInterval(() => {
    // 计算每次增加的时间量
    const timeIncrement = playbackRate.value; // 倍速直接作为增量
    const timeIncrement = playbackRate.value; // 倍速作为增量
    currentTime.value += timeIncrement;
    // 如果超过总时长,则停止播放
    if (currentTime.value >= duration.value) {
      currentTime.value = duration.value; // 停在最后一帧
      stopPlayback();
@@ -164,39 +182,93 @@
      emit("isPlaying", false);
      emit("playbackFinished", true);
    }
    // 触发时间更新事件
    emit("timeUpdate", progressPercentage.value);
  }, 1000); // 每秒更新一次
  }, interval); // 根据速率调整间隔
};
const stopPlayback = () => clearInterval(playInterval);
const stopPlayback = () => {
  clearInterval(playInterval);
};
const skipForward = () =>
  (currentTime.value = Math.min(currentTime.value + 1, duration.value)); // 向前跳转1秒
const skipBackward = () =>
  (currentTime.value = Math.max(currentTime.value - 1, 0)); // 向后跳转1秒
const toggleSpeedMenu = () => (showSpeedMenu.value = !showSpeedMenu.value);
// 设置播放速率
const setPlaybackRate = (rate) => {
  playbackRate.value = rate;
  showSpeedMenu.value = false;
  if (isPlaying.value) stopPlayback(), startPlayback(); // 如果正在播放,则重新启动以应用新的速率
  // 停止当前播放
  stopPlayback();
  // 重置时间轴到初始状态
  currentTime.value = 0; // 时间归零
  emit("timeUpdate", progressPercentage.value);
  isPlaying.value = false;
  // 销毁现有的水体模拟层
  if (isWaterPrimitiveCreated.value) {
    destoryWaterPrimitive();
    isWaterPrimitiveCreated.value = false; // 重置标志变量
  }
  isPlaying.value = false;
  pauseWaterSimulation(); // 调用暂停接口
};
// 时间轴跳转
const seekToPosition = (event) => {
  // 检查是否已经创建了水体模拟层
  if (!isWaterPrimitiveCreated.value) {
    ElMessage({
      message: "请先启动水体模拟后再进行时间轴跳转。",
      type: "warning",
    });
    return; // 阻止后续逻辑执行
  }
  const rect = timelineTrack.value.getBoundingClientRect();
  const percentage = (event.clientX - rect.left) / rect.width;
  // 计算当前点击位置对应的时间值
  currentTime.value = Math.round(percentage * duration.value);
  emit("timeUpdate", progressPercentage.value);
  if (waterTimestamps.value.length > 0) {
    const clickedTimestamp = dayjs(waterTimestamps.value[0]).add(
      currentTime.value,
      "second"
    );
    // 找到最接近的时间戳索引
    const closestIndex = findClosestTimestampIndex(currentTime.value);
    console.log(
      "Clicked timestamp:",
      clickedTimestamp.valueOf(),
      clickedTimestamp.format("YYYY-MM-DD HH:mm:ss")
      "Clicked timestamp index:",
      closestIndex,
      "Time:",
      dayjs(waterTimestamps.value[closestIndex]).format("YYYY-MM-DD HH:mm:ss")
    );
    // 调用跳转接口,传递索引值
    setTimeForWaterSimulation(closestIndex);
    // 如果当前是暂停状态,调用 pauseWaterSimulation
    if (!isPlaying.value) {
      pauseWaterSimulation();
    }
  }
};
// 辅助函数:找到最接近的时间戳索引
function findClosestTimestampIndex(currentTimeValue) {
  let closestIndex = 0;
  let minDiff = Infinity;
  waterTimestamps.value.forEach((timestamp, index) => {
    const diff = Math.abs(dayjs(timestamp).diff(dayjs(waterTimestamps.value[0]), "second") - currentTimeValue);
    if (diff < minDiff) {
      minDiff = diff;
      closestIndex = index;
    }
  });
  return closestIndex;
}
watch(
  () => currentTime.value,
  () => {
@@ -209,6 +281,7 @@
    }
  }
);
// 时间标记生成
function generateTimeMarkers(timestamps) {
  if (!timestamps || timestamps.length === 0) return [];
@@ -222,6 +295,7 @@
      .format("mm:ss")
  );
}
watch(
  () => waterTimestamps.value,
  (newTimestamps) => {
@@ -264,15 +338,18 @@
    });
  }
}
onBeforeUnmount(() => {
  stopPlayback();
  // destoryWaterPrimitive();
  destoryWaterPrimitive();
});
const { endSimulate } = inject("simulateActions");
function handleBack() {
  ElMessage({ message: "模拟进程正在关闭中...", type: "success" }); // 显示消息通知用户模拟进程正在关闭
  endSimulate();
  // destoryWaterPrimitive();
  isWaterPrimitiveCreated.value = false
  destoryWaterPrimitive();
  EventBus.emit("hide-schemeInfo");
}
</script>
src/utils/water.js
@@ -1,110 +1,39 @@
import { cartesianToXY } from "@/utils/map";
let water = null;
/**
 * 销毁水体模拟层
 */
export function destoryWaterPrimitive() {
  if (water) {
    water.destroy();
    water = null;
    console.log("Water simulation destroyed.");
  }
}
// export function createWaterPrimitive(rainfall) {
//   if (water) {
//     water.rainfall = rainfall;
//     return;
//   }
// //   let positionArray = [
// //     {
// //       x: -2172418.4199554003,
// //       y: 4339741.174232391,
// //       z: 4124707.9687457774,
// //     },
// //     {
// //       x: -2173054.1197314346,
// //       y: 4339420.091643301,
// //       z: 4124710.896091218,
// //     },
// //     {
// //       x: -2172832.10576698,
// //       y: 4338665.615224519,
// //       z: 4125615.341361447,
// //     },
// //     {
// //       x: -2171464.714295606,
// //       y: 4337884.628113293,
// //       z: 4127145.919793182,
// //     },
// //     {
// //       x: -2170738.824348358,
// //       y: 4338621.865525843,
// //       z: 4126755.442367371,
// //     },
// //     {
// //       x: -2171270.181257778,
// //       y: 4338967.11477535,
// //       z: 4126117.1692552743,
// //     },
// //     {
// //       x: -2171120.444962885,
// //       y: 4339310.237335228,
// //       z: 4125837.0031219805,
// //     },
// //     {
// //       x: -2171911.8873938583,
// //       y: 4339707.853907823,
// //       z: 4125007.7462886167,
// //     },
// //     {
// //       x: -2171934.494218292,
// //       y: 4339685.285760623,
// //       z: 4125019.506657141,
// //     },
// //   ];
// //   const coordinates = [];
// //   positionArray.forEach((item) => {
// //     const result = cartesianToXY(item);
// //     coordinates.push(result.longitude, result.latitude, result.height);
// //   });
//   // water = earthCtrl.analysis.createWaterSimulateGPU({
//   //     //isDraw: true, // 是否绘制水面
//   //     height: -5,
//   //     maxHeight: 2000,
//   //     coordinates: coordinates,
//   //     // coordinates: [
//   //     //     116.57628914253382, 40.5654461866556, 0, 116.57628914253382, 40.552898897029046, 0,
//   //     //     116.60855621443835, 40.552898897029046, 0, 116.60855621443835, 40.5654461866556, 0,
//   //     // ],
//   //     // coordinates: [
//   //     //     116.568865, 40.590729, 0, 116.568865, 40.533505, 0,
//   //     //     116.608505, 40.533505, 0, 116.608505, 40.590729, 0,
//   //     // ],
//   // })
//   water = earthCtrl.simulate.createWaterSimulateLayer({
//     // baseUrl: "http://192.168.10.187:81/20250412",
//     // baseUrl: "http://192.168.56.107:8088/simu/c2h1d", //仿真服务url
//     baseUrl: "/simu/c2h1dc", //仿真服务url
//     interval: 125, //两帧请求的时间间隔,单位ms
//     // color: new SmartEarth.Cesium.Color.fromCssColorString('#f4a460'),
//     //清水色
//     color: new SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"),
//     //浑水色
//     // color: new SmartEarth.Cesium.Color.fromCssColorString('#DEB887'),
//   });
//   console.log("water", water);
//   // water.rainfall = 0.0001;    // 调节雨量
//   // water.rainPointMax = 2.0;   // 渗水阈值
//   // water.attenuation = 0.995;  // 衰减
//   // water.strenght = 0.25    // 水流强度
// }
/**
 * 创建水体模拟层
 * @param {Object} options - 可选参数
 * @param {number} options.interval - 水体模拟的时间间隔(单位:毫秒)
 */
export function createWaterPrimitive(options = {}) {
    const { interval = 1000 } = options; // 默认 interval 为 1000
    water = earthCtrl.simulate.createWaterSimulateLayer({
      baseUrl: "/simu/c2h1dc", // 仿真服务url
      interval, // 动态设置 interval
      color: new SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"),
    });
    console.log(`Water simulation started with interval: ${interval}ms`);
  }
  const { interval = 1000 } = options; // 默认 interval 为 1000
  water = earthCtrl.simulate.createWaterSimulateLayer({
    baseUrl: "/simu/c2h1dc", // 仿真服务 URL
    interval, // 动态设置 interval
    color: new SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"),
    loop: false, // 是否循环播放
    callback: timeCallback, // 回调函数
  });
  console.log(`Water simulation started with interval: ${interval}ms`);
}
/**
 * 初始化水体模拟视图
 */
export function initeWaterPrimitiveView() {
  let view = {
    destination: {
@@ -119,5 +48,60 @@
    },
  };
  viewer.scene.camera.flyTo(view);
  console.log("Camera view initialized for water simulation.");
}
/**
 * 暂停水体模拟
 */
export function pauseWaterSimulation() {
  if (water) {
    water.pause();
    console.log("Water simulation paused.");
  } else {
    console.warn("No water simulation to pause.");
  }
}
/**
 * 恢复水体模拟
 */
export function resumeWaterSimulation() {
  if (water) {
    water.resume();
    console.log("Water simulation resumed.");
  } else {
    console.warn("No water simulation to resume.");
  }
}
/**
 * 跳转到某个时间点的水面状态
 */
export function setTimeForWaterSimulation(closestIndex) {
  console.log(closestIndex,'index');
  if (water) {
    const imageList = water.getTimeList(); // 获取所有可用时间戳
    if (imageList.length === 0) {
      console.warn("No timestamps available for water simulation.");
      return;
    }
    const idx = Math.floor(Math.random() * imageList.length); // 随机选择一个时间戳
    console.log(
      `Jumping to timestamp: count:[${imageList.length}], index:[${idx}]`
    );
    water.setTime(imageList[closestIndex]); // 设置时间戳,跳转到对应时刻
  } else {
    console.warn("No water simulation to set time for.");
  }
}
/**
 * 时间戳回调函数
 * @param {number} timeStamp - 当前时间戳
 */
function timeCallback(timeStamp) {
  console.log(`Current timestamp: ${timeStamp}`);
}