吉安感知网项目-前端
shuishen
2026-02-03 89380e6260a75d1d3b94de687ebcc2f50d50659d
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
<template>
    <el-dialog
        class="command-page-view-dialog"
        v-model="visible"
        :title="titleEnum[dialogMode]"
        :close-on-click-modal="false"
    >
        <div class="detail-container" v-if="readonly">
            <el-row>
                <el-col :span="12">
                    <div class="label">派出所名称</div>
                    <div class="val">{{ formData.stationName }}</div>
                </el-col>
                <el-col :span="12">
                    <div class="label">联系人</div>
                    <div class="val">{{ formData.contactPerson }}</div>
                </el-col>
                <el-col :span="12">
                    <div class="label">联系方式</div>
                    <div class="val">{{ formData.contactPhone }}</div>
                </el-col>
                <el-col :span="12">
                    <div class="label">位置</div>
                    <div class="val">{{ formData.address }}</div>
                </el-col>
            </el-row>
        </div>
        <el-form
            class="dialog-form"
            v-else
            ref="formRef"
            :model="formData"
            :rules="rules"
            :disabled="readonly"
            label-width="100px"
        >
            <el-row>
                <el-col :span="24">
                    <el-form-item label="派出所名称" prop="stationName">
                        <el-input
                            class="command-input"
                            v-model="formData.stationName"
                            maxlength="50"
                            placeholder="请输入"
                            clearable
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="联系人" prop="contactPerson">
                        <el-input
                            class="command-input"
                            v-model="formData.contactPerson"
                            maxlength="50"
                            placeholder="请输入"
                            clearable
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="联系方式" prop="contactPhone">
                        <el-input
                            class="command-input"
                            v-model="formData.contactPhone"
                            maxlength="50"
                            placeholder="请输入"
                            clearable
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="位置" prop="longitude">
                        <div class="location-container">
                            <div class="location-info" v-if="formData.longitude && formData.latitude">
                                经度: {{ formData.longitude }}, 纬度: {{ formData.latitude }}
                                <span v-if="formData.address">, {{ formData.address }}</span>
                            </div>
                            <div class="location-info" v-else>请在地图上点击选择位置</div>
                            <div class="inline-map-container">
                                <CommonCesiumMap
                                    ref="inlineMapRef"
                                    class="inline-map command-cesium"
                                    :active="visible && !readonly"
                                    :flat-mode="false"
                                    :terrain="true"
                                    :layer-mode="4"
                                    :boundary="false"
                                />
                            </div>
                        </div>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button v-if="dialogMode != 'view'" color="#2B2B4C" @click="handleCancel">
                {{ readonly ? '关闭' : '取消' }}
            </el-button>
            <el-button
                color="#284FE3"
                v-if="!readonly"
                type="primary"
                :loading="submitting"
                :disabled="submitting"
                @click="handleSubmit"
            >
                确定
            </el-button>
        </template>
    </el-dialog>
</template>
 
<script setup>
import { computed, nextTick, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { fwPoliceStationDetailApi, fwPoliceStationSubmitApi } from './precinctInfoApi'
import { contactPhoneRules, fieldRules } from '@ztzf/utils'
import CommonCesiumMap from '@/components/map-container/common-cesium-map.vue'
import * as Cesium from 'cesium'
import axios from 'axios'
import { saveOperationLog } from '@ztzf/apis'
import { useRoute } from 'vue-router'
import positionIcon from '@/assets/images/areaManage/positionIcon.png'
import { MapTooltip } from '@ztzf/utils'
 
const initForm = () => ({
    address: '', // 位置
    contactPerson: '', // 联系人
    contactPhone: '', // 联系方式
    stationName: '', // 派出所名称
    longitude: null,
    latitude: null,
})
 
const emit = defineEmits(['success'])
const formRef = ref(null) // 表单实例
const formData = ref(initForm()) // 表单数据
const visible = defineModel() // 弹框显隐
const dialogMode = ref('add') // 弹框模式
const submitting = ref(false) // 提交中
const readonly = computed(() => dialogMode.value === 'view')
const titleEnum = ref({ edit: '编辑', view: '查看', add: '新增' })
const inlineMapRef = ref(null) // 内嵌地图引用
const route = useRoute()
 
const rules = {
    address: fieldRules(true, 50),
    stationName: fieldRules(true, 50),
    contactPerson: fieldRules(true, 50),
    contactPhone: contactPhoneRules(true, 50),
    longitude: fieldRules(true),
}
 
// 关闭弹框
function handleCancel() {
    visible.value = false
}
 
// 提交新增/编辑
async function handleSubmit() {
    const isValid = await formRef.value?.validate().catch(() => false)
    if (!isValid) return
    submitting.value = true
    try {
        await fwPoliceStationSubmitApi(formData.value)
        saveOperationLog({
            requestUri: route.path,
            title: `${route.name || '派出所管理'}-${dialogMode.value === 'add' ? '新增' : '编辑'}`,
            type: 1,
        })
        ElMessage.success(dialogMode.value === 'add' ? '新增成功' : '更新成功')
        visible.value = false
        emit('success')
    } finally {
        submitting.value = false
    }
}
 
// 加载详情
async function loadDetail() {
    if (!formData.value.id) return
    const res = await fwPoliceStationDetailApi({ id: formData.value.id })
    formData.value = res?.data?.data ?? {}
}
 
let viewer
let redPointEntity
let leftClickBound = false
let mouseMoveBound = false
let mapTooltip
let publicCesium
const tooltipEventKey = 'precinct-info-point-tip'
const labelParams = {
    font: '18px',
    fillColor: Cesium.Color.WHITE, // 文字颜色:白色
    backgroundColor: Cesium.Color.BLACK, //背景颜色
    backgroundPadding: new Cesium.Cartesian2(5, 5), // 水平/垂直内边距(像素)
    pixelOffset: new Cesium.Cartesian2(0, -20), // 可选:微调位置
    showBackground: true, // 确保背景显示(某些版本需要)
    style: Cesium.LabelStyle.FILL_AND_OUTLINE,
    horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
    verticalOrigin: Cesium.VerticalOrigin.CENTER,
    outlineWidth: 1,
    outlineColor: Cesium.Color.WHITE,
    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
}
 
function LeftClickEvent(click) {
    const pos = click.position
    // 优先使用 pickPosition 获取贴地坐标(包含地形高度)
    let cartesian3 = viewer.scene.pickPosition(pos)
    // 如果 pickPosition 失败,回退到 pickEllipsoid
    if (!cartesian3 || !Cesium.defined(cartesian3)) {
        cartesian3 = viewer.camera.pickEllipsoid(pos, viewer.scene.globe.ellipsoid)
    }
    if (!cartesian3) return
    const carto = Cesium.Cartographic.fromCartesian(cartesian3)
    const longitude = Cesium.Math.toDegrees(carto.longitude)
    const latitude = Cesium.Math.toDegrees(carto.latitude)
    formData.value.longitude = _.round(longitude, 6)
    formData.value.latitude = _.round(latitude, 6)
    getLocationName(longitude, latitude)
 
    // 添加红点(全局仅保留一个)并渲染经纬度标签
    if (!redPointEntity) {
        redPointEntity = viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(longitude, latitude),
            billboard: {
                image: positionIcon,
                width: 20,
                height: 20,
                verticalOrigin: Cesium.VerticalOrigin.CENTER,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
            label: {
                text: `${longitude.toFixed(6)}, ${latitude.toFixed(6)}`,
                ...labelParams,
            },
        })
    } else {
        redPointEntity.position = Cesium.Cartesian3.fromDegrees(longitude, latitude)
        redPointEntity.label.text = `${longitude.toFixed(6)}, ${latitude.toFixed(6)}`
    }
}
 
function setMapPoint(longitude, latitude) {
    if (!viewer || longitude == null || latitude == null) return
    if (!redPointEntity) {
        redPointEntity = viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(longitude, latitude),
            billboard: {
                image: positionIcon,
                width: 20,
                height: 20,
                verticalOrigin: Cesium.VerticalOrigin.CENTER,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
            label: {
                text: `${longitude.toFixed(6)}, ${latitude.toFixed(6)}`,
                ...labelParams,
            },
        })
    } else {
        redPointEntity.position = Cesium.Cartesian3.fromDegrees(longitude, latitude)
        redPointEntity.label.text = `${longitude.toFixed(6)}, ${latitude.toFixed(6)}`
    }
}
 
function formatCoord(value) {
    if (value == null || value === '') return '-'
    return _.round(Number(value), 6)
}
 
// 初始化内嵌地图
async function initInlineMap() {
    await nextTick()
    const map = inlineMapRef.value?.getMap()
    viewer = map?.viewer || null
    publicCesium = map?.publicCesium || null
    if (viewer && !leftClickBound) {
        map.publicCesium?.addLeftClickEvent?.(null, LeftClickEvent)
        leftClickBound = true
    }
    if (viewer && !readonly.value) {
        ensurePointTooltip()
    }
    if (formData.value.longitude != null && formData.value.latitude != null) {
        setMapPoint(formData.value.longitude, formData.value.latitude)
    }
}
 
function ensurePointTooltip() {
    if (!viewer || !publicCesium || mouseMoveBound) return
    mapTooltip ||= new MapTooltip(viewer, { offset: { x: 16, y: 24 } })
    publicCesium.addMouseHandler?.(
        null,
        movement => {
            if (!visible.value || readonly.value) return
            mapTooltip?.show('点击地图选择派出所位置', movement.endPosition)
        },
        tooltipEventKey
    )
    mouseMoveBound = true
}
 
function clearPointTooltip() {
    mapTooltip?.hide()
    if (publicCesium && mouseMoveBound) {
        publicCesium.removeMouseHandler?.(tooltipEventKey)
        mouseMoveBound = false
    }
}
 
async function getLocationName(longitude, latitude) {
    const tk = import.meta.env.VITE_APP_TDT_TOKEN
    const http = axios.create({ withCredentials: false })
    const res = await http({
        url: `https://api.tianditu.gov.cn/geocoder?postStr={'lon':${longitude},'lat':${latitude},'ver':1}&tk=${tk}`,
        method: 'get',
    })
    formData.value.address = res.data.result.formatted_address
}
 
// 打开弹框
async function open({ mode, row } = {}) {
    dialogMode.value = mode || 'add'
    formData.value = dialogMode.value === 'add' ? initForm() : row
    redPointEntity = null
    leftClickBound = false
    clearPointTooltip()
    if (dialogMode.value !== 'add') {
        await loadDetail()
    }
    // 延迟初始化地图,确保 DOM 渲染完成
    setTimeout(() => {
        initInlineMap()
    }, 500)
}
 
watch(
    () => visible.value,
    val => {
        if (!val) {
            clearPointTooltip()
        }
    }
)
 
defineExpose({ open })
</script>
<style scoped lang="scss">
.location-container {
    width: 100%;
}
 
.location-info {
    margin-bottom: 8px;
    color: #fff;
    font-size: 14px;
}
 
.inline-map-container {
    width: 100%;
    height: 300px;
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.1);
}
 
.inline-map {
    width: 100%;
    height: 100%;
}
</style>