From 3f527669ed647dc3456581987fec88ecfa6f31f0 Mon Sep 17 00:00:00 2001 From: wersd <1523826083@qq.com> Date: Sun, 22 Jun 2025 16:27:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=BA=E5=9C=B0=E4=BF=A1=E6=81=AF=E5=9C=B0?= =?UTF-8?q?=E5=9B=BE=E5=AE=9A=E7=82=B9=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/TencentMap/index.vue | 116 ++++++++++++++++++ src/views/crm/stationsite/StationSiteForm.vue | 112 ++++++++++------- src/views/crm/stationsite/index.vue | 4 - 3 files changed, 186 insertions(+), 46 deletions(-) create mode 100644 src/components/TencentMap/index.vue diff --git a/src/components/TencentMap/index.vue b/src/components/TencentMap/index.vue new file mode 100644 index 000000000..89e4eb9e8 --- /dev/null +++ b/src/components/TencentMap/index.vue @@ -0,0 +1,116 @@ + + + + + \ No newline at end of file diff --git a/src/views/crm/stationsite/StationSiteForm.vue b/src/views/crm/stationsite/StationSiteForm.vue index e9bae995d..f3d265c64 100644 --- a/src/views/crm/stationsite/StationSiteForm.vue +++ b/src/views/crm/stationsite/StationSiteForm.vue @@ -81,15 +81,21 @@ - - - + + + +
+ 取消 + 确定 +
+
+ @@ -212,6 +218,7 @@ import { defaultProps } from '@/utils/tree' import * as AreaApi from '@/api/system/area' import { useUserStore } from '@/store/modules/user' import * as CustomerApi from '@/api/crm/customer' +import TencentMap from '@/components/TencentMap/index.vue' /** 场地信息 表单 */ defineOptions({ name: 'StationSiteForm' }) @@ -223,6 +230,7 @@ const dialogVisible = ref(false) // 弹窗的是否展示 const dialogTitle = ref('') // 弹窗的标题 const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 const formType = ref('') // 表单的类型:create - 新增;update - 修改 +const tencentMapRef = ref() const formData = ref({ id: undefined, name: undefined, @@ -230,6 +238,8 @@ const formData = ref({ detailAddress: undefined, locationLng: 0, locationLat: 0, + mapCenterLat: 0, + mapCenterLng: 0, estimatedPiles: 0, parkingSpace: 0, siteStatus: undefined, @@ -268,7 +278,6 @@ const formRules = reactive({ const formRef = ref() // 表单 Ref const mapDialogVisible = ref(false) const tencentLbsUrl = ref('') -const TENCENT_MAP_KEY = import.meta.env.VITE_APP_TENCENT_MAP_KEY // 你的腾讯地图key const ownerDialogVisible = ref(false) const managementDialogVisible = ref(false) const selectedOwner = ref() @@ -277,6 +286,7 @@ const ownerFlag = ref(false) const managementFlag = ref(false) const ownerInputDisabled = computed(() => !!formData.value.ownerId) const managementInputDisabled = computed(() => !!formData.value.managementId) +const tempLocation = ref({ lat: 39.984104, lng: 116.307503 }) /** 打开弹窗 */ const open = async (type: string, id?: number) => { @@ -349,6 +359,8 @@ const resetForm = () => { detailAddress: undefined, locationLng: 0, locationLat: 0, + mapCenterLat: 0, + mapCenterLng: 0, estimatedPiles: 0, parkingSpace: 0, siteStatus: undefined, @@ -390,51 +402,67 @@ const filteredManagementList = computed(() => : customerList.value ); -/** 初始化 **/ -const openMapDialog = () => { - // 拼接带初始点的 url - let url = `https://apis.map.qq.com/tools/locpicker?type=1&key=${TENCENT_MAP_KEY}&referer=myapp` - if (formData.value.locationLat && formData.value.locationLng) { - url += `&coord=${formData.value.locationLat},${formData.value.locationLng}` - } - tencentLbsUrl.value = url - mapDialogVisible.value = true +// // 腾讯地图逆地理编码 +// async function getLocationNameByLatLng(lat, lng) { +// const key = import.meta.env.VITE_APP_TENCENT_MAP_KEY +// const url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=${key}` +// try { +// const res = await fetch(url) +// const data = await res.json() +// if (data.status === 0) { +// return data.result.address +// } +// return `(${lat}, ${lng})` +// } catch { +// return `(${lat}, ${lng})` +// } +// } + +// 地图点击事件 +function handleMapClick(e) { + // 腾讯地图API返回的事件对象 + console.log('地图点击事件:', e) + const lat = e.latLng.getLat() + const lng = e.latLng.getLng() + tempLocation.value = { lat, lng } + // getLocationNameByLatLng(lat, lng).then(name => { + // formData.value.locationName = name + // }) } -function selectAddress(loc: any): void { - if (loc.poiname) { - formData.value.locationName = loc.poiname - } else { - formData.value.locationName = '' + +// 确认选点 +async function confirmMapSelection() { + if (!tempLocation.value.lat || !tempLocation.value.lng) { + message.error('请在地图上选择位置') + return } - if (loc.latlng && loc.latlng.lat) { - formData.value.locationLat = Number(loc.latlng.lat) - formData.value.locationLng = Number(loc.latlng.lng) + // 获取地图中心点 + let center = { lat: 0, lng: 0 } + if (tencentMapRef.value && tencentMapRef.value.getCenter) { + center = tencentMapRef.value.getCenter() } + formData.value.locationLat = tempLocation.value.lat + formData.value.locationLng = tempLocation.value.lng + formData.value.mapCenterLat = center.lat + formData.value.mapCenterLng = center.lng + // 可选:逆地理编码获取地名 + formData.value.locationName = `(${tempLocation.value.lat}, ${tempLocation.value.lng})` mapDialogVisible.value = false } -const initTencentLbsMap = () => { - // @ts-ignore - window.selectAddress = selectAddress - window.addEventListener( - 'message', - function (event) { - let loc = event.data - if (loc && loc.module === 'locationPicker') { - // @ts-ignore - window.selectAddress(loc) - } - }, - false - ) +const openMapDialog = () => { + // 优先用已选点,否则用默认中心 + const lat = formData.value.locationLat || 39.984104 + const lng = formData.value.locationLng || 116.307503 + tempLocation.value = { lat, lng } + mapDialogVisible.value = true } onMounted(() => { AreaApi.getAreaTree().then(res => { areaList.value = res }) - initTencentLbsMap() }) watch(mapDialogVisible, (val) => { diff --git a/src/views/crm/stationsite/index.vue b/src/views/crm/stationsite/index.vue index 56069e540..6f34b9701 100644 --- a/src/views/crm/stationsite/index.vue +++ b/src/views/crm/stationsite/index.vue @@ -152,10 +152,6 @@ v-model:limit="queryParams.pageSize" @pagination="getList" /> - - -