wangjuncheng
2025-04-23 8a368655576fd4eb3f517610aa8b30d633f1ea84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<template>
  <Left @start="startSimulate" @end="endSimulate" />
  <echartInfo
    :isDynamicMode="isDynamicMode"
    :isFinish="isFinish"
    v-if="rightRiverShow"
  />
  <TimeLine
    v-if="showWaterSimulate"
    @time-update="timeUpdate"
    @is-playing="isPlaying"
    :waterSimulateParams="waterSimulateParams"
    @playbackFinished="playbackFinished"
    @end="endSimulate"
  />
  <DebuffDetail
    v-if="showDebuffDetail"
    @open="openDetail"
    @close="showDebuffDetail = false"
  />
  <DebuffTable v-if="showDebuffTable" @close="closeDebuffTable" />
</template>
 
<script setup>
import { EventBus } from "@/eventBus"; // 引入事件总线
import { ref, onMounted, onUnmounted, provide } from "vue";
import TimeLine from "@/components/menu/TimeLine.vue";
import Left from "./left/Left.vue";
import echartInfo from "@/components/monifangzhen/echartInfo.vue";
import DebuffDetail from "@/components/tools/DebuffDetail.vue";
import DebuffTable from "@/components/tools/DebuffTable.vue";
import { getMaxInfluenceArea } from "@/api/index";
import { createPoint, geomToGeoJSON } from "@/utils/map.js";
import { loadAreaPolygon, clearAreaPolygon } from "@/utils/area";
 
import danger from "@/assets/img/left/danger.png";
import { checkedKeys } from "@/store/index";
 
import { useSimStore } from "@/store/simulation";
import { storeToRefs } from "pinia";
const simStore = useSimStore();
const { rightRiverShow } = storeToRefs(simStore);
 
const waterSimulateParams = ref({});
const showWaterSimulate = ref(false);
const showDebuffDetail = ref(false);
const showDebuffTable = ref(false);
const isDynamicMode = ref(false);
const isFinish = ref(true);
 
const treeMap = new Map();
 
// 提供方法给所有子组件
provide("simulateActions", {
  startSimulate,
  endSimulate,
});
 
function startSimulate(form) {
  // console.log("form", form);
  showWaterSimulate.value = true;
  rightRiverShow.value = true;
  waterSimulateParams.value = form;
}
function endSimulate() {
  rightRiverShow.value = false;
  showDebuffDetail.value = false;
  clearTrailLine();
  removeEmergencyPoints();
  removeDataSources();
  setTimeout(() => {
    showWaterSimulate.value = false;
    isDynamicMode.value = false;
    // 清除威胁对象表格内容
    EventBus.emit("reset-table");
    // 清除降雨数据内容
    EventBus.emit("clear-echart");
  }, 1000);
}
const MaxInfluenceAreaList = ref([]);
// 初始化 dataSources 全局数组
const dataSources = [];
async function getTimeMarkers() {
  const list = MaxInfluenceAreaList.value;
  try {
    const loadPromises = list.map(async (item) => {
      const geojson = JSON.parse(item.geom);
      const dataSource = await Cesium.GeoJsonDataSource.load(geojson);
      dataSource.entities.values.forEach((entity) => {
        if (!entity.properties) {
          entity.properties = new Cesium.PropertyBag();
        }
        entity.properties.addProperty("id", item.id);
        entity.properties.addProperty("warningLevel", item.warningLevel);
        entity.properties.addProperty("zoneId", item.zoneId);
        entity.polygon.outlineColor = Cesium.Color.YELLOW;
        entity.polygon.outline = true;
        const position = Cesium.Cartesian3.fromDegrees(item.X, item.Y, item.Z);
        const billboardEntity = viewer.entities.add({
          position: position,
          billboard: {
            image: danger,
            scale: 1.0,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
              0,
              1500
            ),
          },
        });
        dataSource.entities.add(billboardEntity);
      });
      viewer.dataSources.add(dataSource);
      dataSources.push(dataSource);
    });
    await Promise.all(loadPromises);
    setupRowClickListener(dataSources);
  } catch (error) {}
}
// 清除隐患点
function removeDataSources() {
  dataSources.forEach((dataSource, index) => {
    // 遍历当前数据源的所有实体
    const entities = dataSource.entities.values;
    entities.forEach((entity, entityIndex) => {
      if (entity.billboard) {
        viewer.entities.remove(entity);
      }
    });
    viewer.dataSources.remove(dataSource);
  });
  dataSources.length = 0;
}
// 避险点,绿色富文本
function addTetrahedron(visible) {
  const emergencyAreaList = [];
  //  这里是添加避险点底层面片
  loadAreaPolygon("/json/emergency_area.geojson", true).then((entities) => {
    emergencyAreaList.push(...entities);
  });
  // console.log('polygon', polygon);
 
  let list = [
    {
      name: "尹建华家",
      longitude: 116.593517,
      latitude: 40.568391,
      altitude: 528.45,
    },
    // {
    //   name: "范振江家",
    //   longitude: 116.591059,
    //   latitude: 40.574068,
    //   altitude: 528,
    // },
    // {
    //   name: "后坡",
    //   longitude: 116.597975,
    //   latitude: 40.558199,
    //   altitude: 528,
    // },
  ];
  // 这里是添加避险点富文本高亮显示
  list.forEach((item) => {
    let point = earthCtrl.factory.createRichTextPoint(
      "避险点",
      [item.longitude, item.latitude, item.altitude - 10],
      {
        distanceDisplayCondition:
          new SmartEarth.Cesium.DistanceDisplayCondition(0, 2000),
        fontColor: "#ffffff",
        fontSize: 20,
      },
      "0"
    );
    console.log("point", point);
    emergencyAreaList.push(point);
  });
  treeMap.set("避险点", emergencyAreaList);
}
// 删除避险点的富文本实体
function removeEmergencyPoints() {
  const emergencyAreaList = treeMap.get("避险点"); // 获取存储的避险点实体列表
  if (emergencyAreaList && emergencyAreaList.length > 0) {
    emergencyAreaList.forEach((entity) => {
      if (entity && typeof entity.deleteObject === "function") {
        // 如果有 deleteObject 方法,优先调用
        entity.deleteObject();
      } else if (entity && typeof entity.clear === "function") {
        // 如果有 clear 方法,调用 clear
        entity.clear();
      } else if (entity && earthCtrl && earthCtrl.coreMap) {
        // 如果是 Cesium 实体,使用 coreMap.entities.remove 移除
        earthCtrl.coreMap.entities.remove(entity);
      }
    });
    treeMap.set("避险点", []); // 清空存储的避险点列表
  }
}
 
// 避险路线
let pathLayer = null; // 存储创建的图层
function showLine() {
  // 创建新图层
  pathLayer = earthCtrl.factory.createPathLayer({
    url: "/json/line.json",
    color: "#0033FF",
    width: 15.0,
    pointColor: "#FFFFFF",
    speed: 2,
    far: 50000,
  });
}
 
// 清除避险路线
function clearTrailLine() {
  if (pathLayer) {
    pathLayer.clear();
  }
  // if (item && typeof item.deleteObject === "function") {
  //   item.deleteObject();
  // } else if (item && typeof item.clear === "function") {
  //   item.clear();
  // } else if (item && earthCtrl && earthCtrl.coreMap) {
  //   earthCtrl.coreMap.entities.remove(item);
  // }
  // });
  // pathLayer = [];
}
function timeUpdate(percentage) {
  if (percentage > 99) {
    if (showDebuffDetail.value) {
      return;
    }
    checkedKeys.value = ["避险点"];
    showDebuffDetail.value = true;
    getTimeMarkers();
    addTetrahedron();
    showLine();
  }
}
function openDetail() {
  showDebuffTable.value = true;
}
function closeDebuffTable() {
  showDebuffTable.value = false;
  showDebuffDetail.value = true;
}
function getMaxInfluenceAreaData() {
  getMaxInfluenceArea().then((res) => {
    MaxInfluenceAreaList.value = res.data.items;
  });
}
 
function isPlaying(val) {
  isDynamicMode.value = val;
}
 
function playbackFinished(val) {
  isFinish.value = val;
}
// 定义全局变量存储当前正在闪动的面片
let flashingPolygon = null;
 
// 添加事件监听器,接收来自表格组件的事件
function setupRowClickListener(dataSources) {
  if (!Array.isArray(dataSources) || dataSources.length === 0) {
    return;
  }
  EventBus.on("row-clicked", (id) => {
    const clickedEntity = findEntityById(id, dataSources);
    if (clickedEntity) {
      // 如果点击的是同一个实体,则停止闪动并清空选择
      if (flashingPolygon && flashingPolygon === clickedEntity) {
        stopFlashing(flashingPolygon);
        flashingPolygon = null; // 清空当前选中的实体
        return;
      }
      // 如果有其他实体正在闪动,先停止它的闪动
      if (flashingPolygon && flashingPolygon !== clickedEntity) {
        stopFlashing(flashingPolygon);
      }
      // 开始新的闪动
      startFlashing(clickedEntity);
      flashingPolygon = clickedEntity;
    } else {
      console.warn(`No entity found with ID: ${id}`);
    }
  });
}
 
// 根据ID查找实体
function findEntityById(id, dataSources) {
  if (!Array.isArray(dataSources) || dataSources.length === 0) {
    return null;
  }
  console.log("Searching for ID:", id);
  for (const dataSource of dataSources) {
    const entities = dataSource.entities.values;
    for (const entity of entities) {
      const entityId = entity.properties?.id?.getValue();
      if (String(entityId) === String(id)) {
        return entity;
      }
    }
  }
  return null;
}
 
// 开始闪动效果
function startFlashing(polygonEntity) {
  // 存储原始颜色
  const originalColor = polygonEntity.polygon.material.color.getValue();
  polygonEntity._originalColor = originalColor; // 将原始颜色保存到实体中
 
  // 创建颜色变化的回调函数
  let isFlashing = true; // 标记是否正在闪动
  polygonEntity.polygon.material = new Cesium.ColorMaterialProperty(
    new Cesium.CallbackProperty(() => {
      if (!isFlashing) return originalColor; // 如果停止闪动,恢复原始颜色
      // 切换颜色(例如红色和原始颜色交替)
      const currentTime = Date.now();
      const flashColor = Cesium.Color.RED.withAlpha(1); // 闪动颜色
      return Math.floor(currentTime / 500) % 2 === 0
        ? flashColor
        : originalColor;
    }, false)
  );
 
  // 将闪动状态保存到实体上,便于后续控制
  polygonEntity._isFlashing = isFlashing;
}
 
// 停止闪动效果
function stopFlashing(polygonEntity) {
  // 恢复原始颜色
  const originalColor = polygonEntity._originalColor || Cesium.Color.WHITE; // 如果没有原始颜色,默认使用白色
  polygonEntity.polygon.material = new Cesium.ColorMaterialProperty(
    originalColor
  );
 
  // 清空闪动状态
  polygonEntity._isFlashing = false;
  polygonEntity._originalColor = null; // 清除保存的原始颜色
}
onMounted(() => {
  setupRowClickListener();
  getMaxInfluenceAreaData();
});
onUnmounted(() => {
  endSimulate();
});
// // 转换坐标系,笛卡尔坐标转为WGS48
// const positions = [[-2171569.1995107993, 4338474.198855222, 4127198.938949332]];
// const wgs84Positions = positions.map((xyz) => {
//   const cartesian = Cesium.Cartesian3.fromArray(xyz);
//   const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
 
//   // 弧度转角度
//   const lon = Cesium.Math.toDegrees(cartographic.longitude);
//   const lat = Cesium.Math.toDegrees(cartographic.latitude);
//   const height = cartographic.height; // 单位:米
 
//   return [lon, lat, height];
// });
</script>
<style lang="less" scoped>
@import url("../assets/css/home.css");
</style>