From 8d6e51af05eb816f2df4c9b09bd0e9887b43f8cb Mon Sep 17 00:00:00 2001 From: WindowBird <13870814+windows-bird@user.noreply.gitee.com> Date: Tue, 19 Aug 2025 09:27:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=8E=B7=E5=8F=96=E5=9C=B0?= =?UTF-8?q?=E5=9D=800.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/lease/lease.vue | 139 +++++++++++++++++++++++++++++------------- utils/permission.js | 9 ++- 2 files changed, 101 insertions(+), 47 deletions(-) diff --git a/pages/lease/lease.vue b/pages/lease/lease.vue index 3321c6b..0564e58 100644 --- a/pages/lease/lease.vue +++ b/pages/lease/lease.vue @@ -42,8 +42,8 @@ 地址 {{ formData.address || '选择收货地址' }} - - 📍 + + 🗺️ @@ -54,7 +54,6 @@ 当前定位 - {{ currentLocation.company }} {{ currentLocation.address }} @@ -164,61 +163,106 @@ export default { const location = await getLocationWithPermission() console.log('位置信息:', location) - this.reverseGeocode(location.latitude, location.longitude) + + // 直接使用坐标信息,简化处理 + this.currentLocation = { + address: `纬度: ${location.latitude.toFixed(6)}, 经度: ${location.longitude.toFixed(6)}`, + latitude: location.latitude, + longitude: location.longitude, + } + + uni.hideLoading() + uni.showToast({ + title: '位置获取成功,点击地图图标查看', + icon: 'success', + duration: 3000, + }) } catch (err) { uni.hideLoading() handleLocationError(err) } }, - // 处理位置信息 - reverseGeocode(latitude, longitude) { - uni.hideLoading() + selectAddress() { + // 如果有当前定位信息,打开地图 + if (this.currentLocation && this.currentLocation.latitude && this.currentLocation.longitude) { + this.openMap() + } else { + // 没有定位信息,先获取位置 + uni.showModal({ + title: '提示', + content: '需要先获取当前位置才能打开地图,是否现在获取?', + success: res => { + if (res.confirm) { + this.getCurrentLocation() + } + }, + }) + } + }, - // 直接使用坐标信息,避免依赖外部API - this.currentLocation = { - company: '当前位置', - address: `纬度: ${latitude.toFixed(6)}, 经度: ${longitude.toFixed(6)}`, + // 打开地图 + openMap() { + const { latitude, longitude } = this.currentLocation + + // 显示加载提示 + uni.showLoading({ + title: '打开地图中...', + }) + + // 尝试打开系统地图应用 + // #ifdef APP-PLUS + uni.openLocation({ latitude: latitude, longitude: longitude, - } - - // 尝试使用uni-app的地理编码(如果平台支持) - // #ifdef APP-PLUS || MP-WEIXIN - uni.reverseGeocoder({ - location: { - latitude: latitude, - longitude: longitude, - }, - success: res => { - console.log('逆地理编码结果:', res) - if (res.result) { - this.currentLocation = { - company: res.result.addressComponent?.city || '当前位置', - address: - res.result.formatted_addresses?.recommend || res.result.address || '未知地址', - latitude: latitude, - longitude: longitude, - } - } + name: '当前位置', + address: this.currentLocation.address || '未知地址', + scale: 18, + success: () => { + uni.hideLoading() + console.log('地图打开成功') }, fail: err => { - console.log('逆地理编码不可用,使用坐标信息') + uni.hideLoading() + console.error('打开地图失败:', err) + this.showMapInApp() }, }) // #endif - uni.showToast({ - title: '位置获取成功', - icon: 'success', + // #ifdef MP-WEIXIN + uni.openLocation({ + latitude: latitude, + longitude: longitude, + name: '当前位置', + address: this.currentLocation.address || '未知地址', + scale: 18, + success: () => { + uni.hideLoading() + }, + fail: err => { + uni.hideLoading() + console.error('打开地图失败:', err) + this.showMapInApp() + }, }) + // #endif + + // #ifdef H5 + // H5环境下使用在线地图 + uni.hideLoading() + const mapUrl = `https://uri.amap.com/marker?position=${longitude},${latitude}&name=${encodeURIComponent('当前位置')}&address=${encodeURIComponent(this.currentLocation.address || '未知地址')}` + window.open(mapUrl, '_blank') + // #endif }, - selectAddress() { - // 选择地址逻辑 - uni.showToast({ - title: '选择地址功能', - icon: 'none', + // 在应用内显示地图(备用方案) + showMapInApp() { + uni.showModal({ + title: '地图功能', + content: `${this.currentLocation.address}\n\n经纬度:${this.currentLocation.latitude.toFixed(6)}, ${this.currentLocation.longitude.toFixed(6)}`, + showCancel: false, + confirmText: '确定', }) }, @@ -228,7 +272,7 @@ export default { return } - this.formData.address = this.currentLocation.company + ' ' + this.currentLocation.address + this.formData.address = this.currentLocation.address uni.showToast({ title: '已使用当前定位', icon: 'success', @@ -413,13 +457,20 @@ export default { display: flex; align-items: center; justify-content: center; - width: 40rpx; - height: 40rpx; + width: 48rpx; + height: 48rpx; background: #f15a04; border-radius: 50%; + transition: all 0.3s ease; + box-shadow: 0 4rpx 12rpx rgba(241, 90, 4, 0.3); + + &:active { + transform: scale(0.9); + box-shadow: 0 2rpx 8rpx rgba(241, 90, 4, 0.4); + } .map-icon { - font-size: 24rpx; + font-size: 28rpx; color: white; } } diff --git a/utils/permission.js b/utils/permission.js index c34713e..32db68f 100644 --- a/utils/permission.js +++ b/utils/permission.js @@ -95,12 +95,15 @@ export function handleLocationError(err) { let errorMsg = '获取位置失败' let showModal = false - if (err.errMsg.includes('auth deny') || err.errMsg.includes('no permission')) { + // 安全地检查错误信息 + const errMsg = err?.errMsg || err?.message || '' + + if (errMsg.includes('auth deny') || errMsg.includes('no permission')) { errorMsg = '定位权限被拒绝,请在设置中开启' showModal = true - } else if (err.errMsg.includes('timeout')) { + } else if (errMsg.includes('timeout')) { errorMsg = '定位超时,请重试' - } else if (err.errMsg.includes('unsupported')) { + } else if (errMsg.includes('unsupported')) { errorMsg = '当前设备不支持定位功能' }