无人机管理后台前端(已迁走)
罗广辉
2025-10-11 02409bfbe15f22fc3b5dccadabfd860a660a49d9
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
<template>
    <el-dialog class="airspace-dialog" :title="props.title" v-model="dialogShow" width="60%" align-center
        @close="handleClose" :close-on-click-modal="false" :close-on-press-escape="false">
        <div class="custom-container">
            <div class="form-container">
                <avue-form ref="formEle" :option="option" v-model="form"></avue-form>
            </div>
 
            <div class="map-container" v-loading="mapLoading" element-loading-text="地形初始化中,请稍后..."
                element-loading-background="rgba(0, 0, 0, 0.7)">
                <div class="tool-tip warning" v-show="isShowWaringTip">
                    <span class="icon">
                        <el-icon>
                            <WarningFilled />
                        </el-icon>
                    </span>
                    <span>空域不支持交叉面,无法生成空域</span>
                </div>
                <div id="AirspaceMap" class="ztzf-cesium"></div>
            </div>
 
            <div class="btn-container">
                <el-button icon="el-icon-check" type="primary" @click="handleSubmit">确定</el-button>
                <el-button icon="el-icon-close" @click="dialogShow = false">取消</el-button>
            </div>
        </div>
    </el-dialog>
</template>
 
<script setup>
import _, { cloneDeep, throttle } from 'lodash'
 
import * as Cesium from 'cesium'
import * as turf from '@turf/turf'
 
import { getPointPositionsHeight } from '@/utils/cesium/mapUtil'
import { DrawPolygon } from '@/utils/drawPolygon/drawPolygon'
import { PublicCesium } from '@/utils/cesium/publicCesium'
import { ElMessageBox, ElMessage } from 'element-plus'
 
import {
    airspaceEnteringAdd,
    airspaceEnteringUpdate
} from '@/api/airspace/airspace'
 
 
const props = defineProps({
    title: {
        type: String,
        default: '新增空域',
    },
 
    type: {
        type: String,
        default: 'add',
    },
 
    curEditRow: {
        type: Object,
        default: () => { },
    }
})
 
const emit = defineEmits(['submitClick'])
 
const dialogShow = defineModel('show')
 
const formEle = ref(null)
const form = ref({
    height: 120,
})
const option = ref({
    submitBtn: false,
    emptyBtn: false,
    menuBtn: false,
    column: [
        {
            label: '名称',
            prop: 'name',
            type: 'input',
            rules: [
                {
                    required: true,
                    message: '请输入名称',
                    trigger: 'blur'
                }
            ]
        },
 
        {
            label: '高度',
            prop: 'height',
            type: 'input',
            value: 120,
            min: 5,
            max: 500,
            type: 'number',
            change ({ column, value }) {
                drawPolygonExample?.setPolygonHeight?.(value)
            },
            rules: [
                {
                    required: true,
                    message: '请输入高度',
                    trigger: 'blur'
                },
                {
                    pattern: /^(?:[5-9]|[1-9]\d|[1-4]\d{2}|500)$/, // 5-500
                    message: '高度需在5-500之间',
                    trigger: ['blur', 'change']
                }
            ]
        },
 
        {
            label: '类型',
            prop: 'flight_type',
            type: 'select',
            dicUrl: `/drone-device-core/airspace/page?current=1&size=10000`,
            props: {
                label: 'type_name',
                value: 'id',
            },
            dicFormatter (res) {
                return res.data.records
            },
            rules: [
                {
                    required: true,
                    message: '请选择类型',
                    trigger: 'blur'
                }
            ]
        },
        {
            label: '管控时段',
            prop: 'time_period',
            type: 'daterange',
            format: 'YYYY-MM-DD',
            valueFormat: 'YYYY-MM-DD',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期',
            rules: [
                {
                    required: true,
                    message: '请选择管控时段',
                    trigger: 'blur'
                }
            ]
        },
 
        {
            label: '管控原因',
            prop: 'remark',
            type: 'textarea',
            span: 24,
            rows: 1,
            minRows: 1,
            maxRows: 1,
            placeholder: '请输入管控原因',
        },
    ],
})
 
let drawPolygonExample = null
let publicCesiumInstance = null
let viewer = null
// 地图
const mapLoading = ref(true)
 
const isShowWaringTip = ref(false)
 
const curDrawPolygonData = ref([])
 
const initMap = async () => {
    if (!document.getElementById('AirspaceMap')) {
        return
    }
    publicCesiumInstance = new PublicCesium({
        dom: 'AirspaceMap',
        flatMode: false,
        terrain: true,
        layerMode: 4,
        contour: true,
        flyToContour: true,
        terrainInit () {
            setTimeout(async () => {
                initDrawPolygon()
                mapLoading.value = false
            }, 1500)
        }
    })
 
    viewer = publicCesiumInstance.getViewer()
 
    drawPolygonExample = new DrawPolygon()
 
    drawPolygonExample.initHandler(viewer)
 
    drawPolygonExample.subscribe('getShowWaringTip', data => {
        isShowWaringTip.value = data
    })
 
    drawPolygonExample.subscribe('getPolygonPositions', data => {
        curDrawPolygonData.value = data.map(item => {
            let cartographic = Cesium.Cartographic.fromCartesian(item)
 
            let lng = Cesium.Math.toDegrees(cartographic.longitude) // 经度
            let lat = Cesium.Math.toDegrees(cartographic.latitude) // 纬度
            let height = cartographic.height
 
            return {
                lng: _.round(lng, 6),
                lat: _.round(lat, 6),
                height
            }
        })
    })
}
 
watch(dialogShow, async (show) => {
    if (show) {
        if (props.type === 'edit') {
            form.value = { ...props.curEditRow }
        }
 
        initDrawPolygon()
 
        if (viewer) return
 
        await nextTick()
        cesiumContextMenu()
        initMap()
    }
})
 
const initDrawPolygon = async () => {
    if (props.type === 'edit' && drawPolygonExample) {
        drawPolygonExample?.setPolygonHeight?.(props.curEditRow.height)
 
        let polygon = JSON.parse(props.curEditRow.flight_range)
 
        if (polygon.length > 0) {
            const aslData = await getPointPositionsHeight(polygon, viewer)
            drawPolygonExample?.initPolygon(viewer, aslData.map(item => ({ ...item, height: item.ASL })))
        }
    }
}
 
const handleSubmit = () => {
    if (ElMessage.value) {
        ElMessage.warning('空域不支持交叉,请重新绘制或调整')
        return
    }
 
    if (curDrawPolygonData.value.length === 0) {
        ElMessage.warning('请绘制空域区域')
        return
    }
 
    if (formEle.value) {
        formEle.value.validate((valid, done, msg) => {
            if (valid) {
                let disposePolygon = curDrawPolygonData.value.map(i => [i.lng, i.lat])
 
                let polygon = turf.polygon([
                    [
                        ...disposePolygon,
                        disposePolygon[0]
                    ]
                ])
 
                let area = turf.area(polygon)
 
                if (props.type === 'edit') {
                    airspaceEnteringUpdate({
                        id: props.curEditRow.id,
                        name: form.value.name,
                        flight_range: JSON.stringify(curDrawPolygonData.value),
                        height: form.value.height,
                        time_period: form.value.time_period.join('~'),
                        flight_type: form.value.flight_type,
                        remark: form.value.remark,
                        area: area
                    }).then(res => {
                        ElMessage.success('更新成功')
                        dialogShow.value = false
                        emit('submitClick')
                        done()
                    })
                } else {
                    airspaceEnteringAdd({
                        name: form.value.name,
                        flight_range: JSON.stringify(curDrawPolygonData.value),
                        height: form.value.height,
                        time_period: form.value.time_period.join('~'),
                        flight_type: form.value.flight_type,
                        remark: form.value.remark,
                        area: area
                    }).then(res => {
                        ElMessage.success('新增成功')
                        dialogShow.value = false
                        emit('submitClick')
                        done()
                    })
                }
            } else {
                return false
            }
        })
    }
}
 
const handleClose = () => {
    drawPolygonExample?.delPolygon()
    isShowWaringTip.value = false
    curDrawPolygonData.value = []
    form.value = {
        height: 120
    }
    formEle.value.resetForm()
    dialogShow.value = false
}
 
// 阻止右键默认行为
const preventDefault = event => {
    event.preventDefault()
    return
}
 
const cesiumContextMenu = (isAdd = true) => {
    let cesium = document.getElementById('AirspaceMap')
    if (!cesium) return
    if (isAdd) {
        cesium.addEventListener('contextmenu', preventDefault)
    } else {
        cesium.removeEventListener('contextmenu', preventDefault)
    }
}
 
onBeforeUnmount(() => {
    drawPolygonExample?.destroy()
    drawPolygonExample = null
    cesiumContextMenu(false)
    publicCesiumInstance?.viewerDestroy()
    publicCesiumInstance = null
    viewer = null
})
</script>
 
<style scoped lang="scss">
.custom-container {
    .map-container {
        height: 620px;
 
        #AirspaceMap {
            position: relative;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    }
 
    .btn-container {
        margin-top: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
 
.tool-tip {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    width: 800px;
    height: 64px;
    font-family: Source Han Sans CN, Source Han Sans CN;
    font-size: 18px;
    color: #ffffff;
 
    background: rgba(11, 31, 58, 0.7);
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
    z-index: 9;
}
 
.warning {
    top: 234px;
    left: 50%;
    transform: translateX(-50%);
    color: #fff;
    background: rgba(140, 0, 0, 0.4);
 
    .icon {
        display: flex;
        align-items: center;
        color: #ff1f1f;
    }
}
</style>