forked from drone/command-center-dashboard

罗广辉
2025-04-21 2800fa4f32f3900509cb4d6eefaf2bfaf54efdd7
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import * as Cesium from 'cesium'
import aggregationImg from '@/assets/images/home/useUavHome/aggregation.png'
import eventAggregationImg from '@/assets/images/home/useUavHome/eventAggregationImg.png'
import _ from 'lodash'
import { getEventImage } from '@/utils/stateToImageMap/event'
import { getDroneStatusImage } from '@/utils/stateToImageMap/drone'
 
import { useStore } from 'vuex'
import { getCenterPoint } from '@/utils/cesium/mapUtil'
import cesiumOperation from '@/utils/cesium-tsa'
import { getDeviceRegion, getDeviceRegionCount, getEventDetails, getMapEvents } from '@/api/home/aggregation'
import { PolyGradientMaterial } from '@/utils/cesium/Material'
import { start } from 'nprogress'
 
// hook
import { useMapHandlerClick } from '@/hooks/components/useMapHandlerClick'
import { MAP_LEVEL } from '@/const/drc'
 
/**
 * 机巢聚合功能
 */
 
let arrColor = [
  'rgb(15,176,255)',
  'rgb(18,76,154)',
  '#40C4E4',
  '#42B2BE',
  'rgb(51,176,204)',
  '#8CB7E5',
  'rgb(0,244,188)',
  '#139FF0',
]
 
let index = 0
function getColor () {
  return arrColor[++index % arrColor.length]
}
 
export const useMapAggregation = (type, status) => {
  const { flyTo } = cesiumOperation()
 
  const mergeImg = type === 'device' ? aggregationImg : eventAggregationImg
  const styleTransform = type === 'device' ? 'translateY(-50%)' : 'translate(-50%,-110%)'
  const eventType = type === 'device' ? ['deviceAggregation', 'device'] : 'event'
 
  const {
    handlerInit: mapHandlerInit,
    removeAll: removeMapHandlerAll,
    removeLabel: removeMapLabel
  } = useMapHandlerClick({
    popupType: `${type}-popup`,
    eventType: eventType,
    styleTransform,
    getViewer () {
      return viewer
    }
  })
 
  let scalingJudgment = _.cloneDeep(MAP_LEVEL).map(i => ({...i,gJson: null,splashedList:[], show: false, outline: {}}))
 
  let viewer = null
  let active = null
 
  const store = useStore()
  const userAreaCode = computed(() => store.state.user.userInfo.detail.areaCode)
  const selectedAreaCode = computed(() => store.state.user.selectedAreaCode)
 
  const eventTimeType = computed(() => store.state.home.eventTimeType)
  const eventTimeRang = computed(() => store.state.home.eventTimeRang)
 
  const singleUavHome = computed(() => store.state.home.singleUavHome)
 
  let needFly = true
 
  const combinedValues = computed(() => ({
    selectedAreaCode: selectedAreaCode.value,
    eventTimeType: eventTimeType.value,
    eventTimeRang: eventTimeRang.value,
    singleUavHome: singleUavHome.value,
  }))
 
  let saveParams = { area_code: '', date_enum: 'CURRENT_WEEK' }
 
  // 确定缩放比例
  const determineScaling = () => {
    // console.log('确定缩放比例');
    if (!viewer) return
    let height = viewer.camera.positionCartographic.height
    // 根据高度展示对应的 gJson
    for (let [index, item] of scalingJudgment.entries()) {
      if (!item.show) return
      if (height > item.heightRange[0] && height <= item.heightRange[1]) {
        if (active === item.name) return
        active = item.name
        removeEntities()
        removeMapLabel()
        renderOutline(item)
        if (!item.gJson && !item.splashedList?.length) return
        item.gJson ? aggregation(item) : splashed(item)
        break
      }
    }
  }
 
  // 转换为目录结构, 返回数组,
  function convertToHierarchy (code) {
    const codeStr = code.toString()
    const provinceCode = codeStr.slice(0, 2) + '0000'
    const cityCode = codeStr.slice(0, 4) + '00'
    if (codeStr.slice(2, 4) === '00' && codeStr.slice(4, 6) === '00') {
      return [provinceCode]
    } else if (codeStr.slice(4, 6) === '00') {
      return [provinceCode, cityCode]
    }
    return [provinceCode, cityCode, code]
  }
 
  // 获取设备聚合
  function getDeviceCount (areaCode) {
    return getDeviceRegionCount({ areaCode }).then(res => {
      return res?.data?.data || []
    })
  }
  // 获取设备散点
  function getDeviceList (areaCode) {
    return getDeviceRegion({ areaCode }).then(res => {
 
      return (res?.data?.data || []).map(i => ({ ...i, type }))
 
 
    })
  }
 
  // 事件散点
  let eventList = []
  function processChildren (childrens) {
    return (childrens || []).map(item => {
      const arr = processChildren(item.childrens)
      if (item.data) {
        eventList = eventList.concat(item.data)
      }
      const returnObj = {
        total_device_count: item.number,
        region_code: item.id,
        region_name: item.name,
      }
      if (arr.length !== 0) {
        returnObj.childrens = arr
      }
      return returnObj
    })
  }
 
  // 获取事件聚合
  function getMapEventCount (params) {
    return getMapEvents(params).then(res => {
      const resData = res?.data?.data
      if (resData?.data) {
        eventList = resData?.data
        return []
      }
      return processChildren(resData?.childrens)
    })
  }
 
  const findFun = (featItem, numItem) => Number(featItem.region_code.slice(0, 6)) === numItem.properties.adcode
  const { VITE_APP_BASE, VITE_APP_ENV } = import.meta.env
  const defaultDir = `${VITE_APP_BASE}${VITE_APP_ENV === 'development' ? 'public/' : ''}geoJson/100000/`
 
  const getFiler = async url => {
    const res = await fetch(url)
    return await res.json()
  }
  const getOutLine = async (jsonPathPre, hierarchy) => {
    const parentGJson = await getFiler(`${defaultDir}${jsonPathPre}/index.json`)
    let features = parentGJson.features.find(item => item.properties.adcode === Number(hierarchy[hierarchy.length - 1]))
    return { type: 'FeatureCollection', features: [features] }
  }
 
  const injectData = (gJson, dataList) => {
    return {
      ...gJson,
      features: gJson.features.map(item => {
        const findData = dataList.find(item1 => findFun(item1, item))
        return { ...item, data: { ...findData, type: type + 'Aggregation' } }
      }),
    }
  }
 
  const initMapData = async areaCode => {
    eventList = []
    if (!areaCode) return
    saveParams.area_code = areaCode
    const list = type === 'device' ? await getDeviceCount(areaCode) : await getMapEventCount(saveParams)
 
    const splashedList =
      type === 'device'
        ? await getDeviceList(areaCode)
        : eventList.map(i => ({
          eventId: i.id,
          latitude: Number(i.latitude),
          longitude: Number(i.longitude),
          type: 'event',
          status: i.status
        }))
    const hierarchy = convertToHierarchy(areaCode.slice(0, 6))
    const jsonPath = hierarchy.join('/')
    const jsonPathPre = hierarchy.slice(0, hierarchy.length - 1).join('/')
    scalingJudgment = scalingJudgment.map(item => ({ ...item, show: true }))
    scalingJudgment[0].gJson = null
    scalingJudgment[0].splashedList = splashedList
    active = null
    // 省
    if (hierarchy.length === 1) {
      const gJson1 = await getFiler(`${defaultDir}${jsonPath}/indexDistrict.json`)
      const gJson2 = await getFiler(`${defaultDir}${jsonPath}/index.json`)
      scalingJudgment[1].gJson = {
        ...gJson1,
        features: gJson1.features.map(item => {
          const findData = list.flatMap(item => item.childrens || []).find(item1 => findFun(item1, item))
          return { ...item, data: { ...findData, type: type + 'Aggregation' } }
        }),
      }
      scalingJudgment[2].gJson = injectData(gJson2, list)
    }
    // 市
    if (hierarchy.length === 2) {
      scalingJudgment[2].gJson = null
      scalingJudgment[2].show = false
      const gJson1 = await getFiler(`${defaultDir}${jsonPath}/index.json`)
      scalingJudgment[1].gJson = injectData(gJson1, list)
    }
    // 区县
    if (hierarchy.length === 3) {
      scalingJudgment[1].gJson = null
      scalingJudgment[1].show = false
      scalingJudgment[2].gJson = null
      scalingJudgment[2].show = false
    }
    // 轮廓
    const outlineGJson = await getOutLine(jsonPathPre, hierarchy)
    scalingJudgment.forEach(item => item.show && (item.outline = outlineGJson))
    const [longitude, latitude] = outlineGJson.features[0].properties.centroid
    const height = scalingJudgment[(hierarchy.length - 3) * -1].height
    setCenterPosition({ longitude, latitude, height })
    flyTo({ longitude, latitude }, 0, height)
  }
 
  const userAreaPosition = computed(() => store.state.home.userAreaPosition)
 
  const setCenterPosition = position => {
    store.commit('setCurrentAreaPosition', position)
    if (!userAreaPosition.value.longitude) {
      store.commit('setUserAreaPosition', position)
    }
  }
 
  watch(
    combinedValues,
    async (newValue, oldValue) => {
      if (newValue.singleUavHome?.device_sn) {
        clearMapEntity()
        return
      }
 
      if (newValue.eventTimeType) {
        saveParams = { area_code: newValue.selectedAreaCode, date_enum: store.state.home.eventTimeParams }
      }
 
      if (newValue.eventTimeRang) {
        saveParams = {
          area_code: newValue.selectedAreaCode,
          start_date: newValue.eventTimeRang[0],
          end_date: newValue.eventTimeRang[1],
        }
      }
 
      needFly = true
      if (!viewer) return
      mapHandlerInit()
 
      viewer.scene.postRender.removeEventListener(determineScaling)
 
      initMapData(newValue.selectedAreaCode).then(() => {
        viewer.scene.postRender.addEventListener(determineScaling)
      })
    },
    { deep: true }
  )
 
  //散点机巢
  function splashed (row) {
    row.splashedList.forEach((item, index) => {
      const image = type === 'device' ? getDroneStatusImage(item.status) : getEventImage(item.status)
 
      viewer.entities.add({
        id: `aggregation-splashed-${index}`,
        position: Cesium.Cartesian3.fromDegrees(item.longitude, item.latitude),
        label: {
          text: item.nickname,
          font: '12pt Source Han Sans CN',
          fillColor: Cesium.Color.WHITE,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 2,
          style: Cesium.LabelStyle.FILL_AND_OUTLINE,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -9),
        },
        billboard: {
          // singleImg
          image: new Cesium.ConstantProperty(image),
          width: 24,
          height: 24,
        },
        properties: {
          customData: {
            data: item,
          },
        },
      })
    })
  }
 
  // 渲染轮廓
  const renderOutline = item => {
    item.outline &&
      Cesium.GeoJsonDataSource.load(item.outline).then(dataSource => {
        const entities = dataSource.entities.values
        entities.forEach((entity, index) => {
          // 创建独立折线作为轮廓
          const positions = entity.polygon.hierarchy.getValue().positions
          const randomInt = Math.floor(Math.random() * 5) + 1
 
          let polygon = {}
 
          if (item.name === '县') {
            let material = new PolyGradientMaterial({
              color: Cesium.Color.fromCssColorString(arrColor[randomInt]),
              opacity: 0.7,
              alphaPower: 1.3,
            })
 
            entity.polygon.extrudedHeight = (entity.properties.childrenNum._value || 1) * 500
 
            entity.polygon.material = material
 
            polygon = entity.polygon
            entity.polygon.outline = false // 显示边框
          }
 
          viewer.entities.add({
            id: `aggregation-outline-${index}`,
            polyline: {
              positions: positions,
              width: 5, // 直接设置宽度
              material: new Cesium.PolylineGlowMaterialProperty({
                glowPower: 0.5,
                color: Cesium.Color.AQUA,
              }),
            },
 
            polygon,
          })
        })
      })
  }
 
  // 聚合机巢
  const aggregation = item => {
    if (!item.gJson) return
    const featuresList = item.gJson.features.map(item1 => {
      // const {lng,lat} = getCenterPoint(item1.geometry.coordinates[0][0])
      return {
        name: item1.properties.name,
        position: item1.properties.centroid,
        data: item1.data,
        id: item1.region_code,
      }
    })
    // 遍历特征并添加实体
    featuresList.forEach((feature, index) => {
      if (!feature.position) return
      const position = Cesium.Cartesian3.fromDegrees(feature.position[0], feature.position[1], 0)
      viewer.entities.add({
        position: position,
        id: `aggregation-name-${index}`,
        label: {
          text: feature.name,
          font: '14pt Source Han Sans CN',
          fillColor: Cesium.Color.WHITE,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 2,
          style: Cesium.LabelStyle.FILL_AND_OUTLINE,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -9),
        },
      })
      viewer.entities.add({
        id: `aggregation-count-${index}`,
        position: position,
        label: {
          text: (feature.data.total_device_count || 0).toString(),
          font: '12pt Source Han Sans CN',
          fillColor: Cesium.Color.BLACK,
          outlineColor: Cesium.Color.BLACK,
          style: Cesium.LabelStyle.FILL_AND_OUTLINE,
          eyeOffset: new Cesium.Cartesian3(0, 0, -10), // 让label "浮" 在广告牌前面
        },
        billboard: {
          image: new Cesium.ConstantProperty(mergeImg),
          width: 35,
          height: 35,
        },
        properties: {
          id: feature.id,
          customData: {
            data: feature.data,
          },
        },
      })
    })
 
    // 加载边界
    Cesium.GeoJsonDataSource.load(item.gJson).then(dataSource => {
      viewer.dataSources.add(dataSource)
      item.BJDataSource = dataSource // 保存数据源以便后续删除
      // 获取数据源中的实体
      const entities = dataSource.entities.values
 
      entities.forEach(entity => {
        let material = new PolyGradientMaterial({
          color: Cesium.Color.fromCssColorString(getColor()),
          opacity: 0.7,
          alphaPower: 1.3,
        })
 
        const randomInt = Math.floor(Math.random() * 8) + 1
 
        entity.polygon.extrudedHeight = (entity.properties.childrenNum._value || randomInt) * 500
 
        entity.polygon.material = material
        entity.polygon.outline = false // 显示边框
      })
 
      needFly &&
        viewer.flyTo(dataSource, {
          offset: new Cesium.HeadingPitchRange(0, Cesium.Math.toRadians(-60), 0),
          duration: 0.5,
        })
      needFly = false
    })
  }
 
 
  /**
   * 根据条件获取项
   * @param {Array} arr 数据源
   * @param {Function} condition 条件
   * @returns
   */
  const findTypeItem = (arr, condition) => {
    return arr.find(item => condition(item))
  }
 
  // 移除 点 和 gjson 实体
  const removeEntities = () => {
    // dataSources移除
    scalingJudgment.forEach(item => {
      item.BJDataSource && viewer.dataSources.remove(item.BJDataSource)
      item.BJDataSource = null
    })
    // entities移除
    const entitiesIDs = viewer.entities.values.map(i => i.id)
    entitiesIDs.forEach(item => {
      item.includes('aggregation-') && viewer.entities.removeById(item)
    })
  }
 
 
  // 移除所有监听事件,变量置空
  const removeAll = () => {
    clearMapEntity()
    viewer = null
  }
 
  const clearMapEntity = () => {
    if (!viewer) return
    removeEntities()
    removeMapHandlerAll()
 
    // viewer.camera.moveEnd.removeEventListener(determineScaling);
    viewer.scene.postRender.removeEventListener(determineScaling)
 
    active = null
  }
 
  const init = () => {
    viewer = window.$viewer
    viewer.scene.postRender.removeEventListener(determineScaling)
    initMapData(selectedAreaCode.value || userAreaCode.value).then(() => {
      viewer.scene.postRender.addEventListener(determineScaling)
    })
 
    mapHandlerInit()
  }
 
  onBeforeUnmount(() => { })
  // onMounted(() => {
  //   nextTick(() => {
  //     viewer = window.$viewer;
  //     handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  //     handler.setInputAction(singleMachineEvent, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  //   });
  // });
 
  return { init, removeAll }
}