From af7a920683ca71518a5a9a62e678cf164d3f1110 Mon Sep 17 00:00:00 2001 From: Lj <1095098147@qq.com> Date: Sat, 28 Jun 2025 01:12:11 +0800 Subject: [PATCH] =?UTF-8?q?'=E8=B0=83=E7=94=A8=E5=9C=B0=E5=9B=BE=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=9C=B0=E5=9D=80'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/subcomponent/select.vue | 193 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 186 insertions(+), 7 deletions(-) diff --git a/pages/subcomponent/select.vue b/pages/subcomponent/select.vue index a7f5c1a..4f152be 100644 --- a/pages/subcomponent/select.vue +++ b/pages/subcomponent/select.vue @@ -64,7 +64,7 @@ 手机号 - + 所在地区 @@ -85,7 +85,10 @@ 关闭 - 所在地区 + 手动选择地区 + + + 请手动选择准确的省市区信息 @@ -161,7 +164,9 @@ export default { phone: '', region: [], address: '', - addressDetails: '' + addressDetails: '', + latitude: '', + longitude: '' }, provinces: regionData, cities: [], @@ -256,7 +261,9 @@ export default { region: [], address: item.address, addressDetails: item.addressDetails, - defaultFlag: item.defaultFlag + defaultFlag: item.defaultFlag, + latitude: item.latitude || '', + longitude: item.longitude || '' } this.regionIndex = [0, 0, 0] this.cities = this.provinces[0]?.children || [] @@ -270,7 +277,9 @@ export default { phone: '', region: [], address: '', - addressDetails: '' + addressDetails: '', + latitude: '', + longitude: '' } this.regionIndex = [0, 0, 0] this.cities = this.provinces[0]?.children || [] @@ -303,7 +312,9 @@ export default { phone: this.form.phone, address: this.form.address, addressDetails: this.form.addressDetails, - defaultFlag: this.form.defaultFlag + defaultFlag: this.form.defaultFlag, + latitude: this.form.latitude, + longitude: this.form.longitude } if (this.form.id) params.id = this.form.id this.$api('saveOrUpdateAddress', params, (res) => { @@ -325,6 +336,164 @@ export default { } this.regionIndex = [pIdx, cIdx, dIdx] }, + // 选择地区 + selectAddress() { + // 首先检查位置权限 + uni.getSetting({ + success: (res) => { + if (res.authSetting['scope.userLocation'] === false) { + // 用户之前拒绝了位置权限,引导用户去设置 + uni.showModal({ + title: '位置权限', + content: '需要获取您的位置信息来选择地址,请在设置中开启位置权限', + confirmText: '去设置', + success: (modalRes) => { + if (modalRes.confirm) { + uni.openSetting() + } else { + // 用户拒绝去设置,直接打开区域选择器 + this.showRegionPicker = true + } + }, + fail: () => { + this.showRegionPicker = true + } + }) + return + } + + // 调用位置选择 + uni.chooseLocation({ + success: res => { + console.log(res); + + this.form.latitude = res.latitude + this.form.longitude = res.longitude + + var reg = /.+?(省|市|自治区|自治州|县|区)/g; + + let address = '' + + if (!res.address && res.name) { //用户直接选择城市的逻辑 + address = res.name + } + if (res.address || res.name) { + address = res.address + res.name + } + + if(!address){ + return + } + + let arr = address.match(reg) + + // 判断是否提取到了省市县信息 + if (!arr || arr.length < 2) { + // 提取不到完整的省市县信息,打开区域选择器 + uni.showToast({ + title: '地址信息不完整,请手动选择', + icon: 'none' + }) + this.showRegionPicker = true + return + } + + const province = arr[0] || '' + const city = arr[1] || '' + const district = arr[2] || '' + + let detail = district || city || province || '' + + this.form.addressDetails = address.substring(address.indexOf(detail) + detail.length) + this.form.address = `${province}${city}${district}` + + // 尝试匹配到region数据中对应的省市区 + this.matchRegionData(province, city, district) + }, + fail(e) { + console.log("获取位置信息失败!", e) + + // 根据错误类型给出不同提示 + if (e.errMsg && e.errMsg.includes('auth deny')) { + uni.showModal({ + title: '位置权限', + content: '需要获取您的位置信息来选择地址,请允许位置权限', + confirmText: '重新授权', + success: (modalRes) => { + if (modalRes.confirm) { + uni.openSetting() + } else { + this.showRegionPicker = true + } + } + }) + } else if (e.errMsg && e.errMsg.includes('cancel')) { + // 用户取消选择位置,直接打开区域选择器 + this.showRegionPicker = true + } else { + // 其他错误,提示并打开区域选择器 + uni.showToast({ + title: '获取位置失败,请手动选择', + icon: 'none' + }) + this.showRegionPicker = true + } + } + }) + }, + fail: () => { + // 获取设置失败,直接打开区域选择器 + this.showRegionPicker = true + } + }) + }, + + // 匹配region数据 + matchRegionData(provinceName, cityName, districtName) { + // 查找省份 + const provinceIndex = this.provinces.findIndex(p => + provinceName.includes(p.name) || p.name.includes(provinceName.replace(/省|市|自治区|自治州/g, '')) + ) + + if (provinceIndex === -1) { + this.regionIndex = [0, 0, 0] + return + } + + this.cities = this.provinces[provinceIndex]?.children || [] + + // 查找城市 + const cityIndex = this.cities.findIndex(c => + cityName.includes(c.name) || c.name.includes(cityName.replace(/市|县|区/g, '')) + ) + + if (cityIndex === -1) { + this.regionIndex = [provinceIndex, 0, 0] + this.districts = this.cities[0]?.children || [] + return + } + + this.districts = this.cities[cityIndex]?.children || [] + + // 查找区县 + const districtIndex = this.districts.findIndex(d => + districtName.includes(d.name) || d.name.includes(districtName.replace(/县|区/g, '')) + ) + + this.regionIndex = [ + provinceIndex, + cityIndex, + districtIndex > -1 ? districtIndex : 0 + ] + + // 更新form.region + this.form.region = [ + this.provinces[provinceIndex]?.code || '', + this.cities[cityIndex]?.code || '', + this.districts[districtIndex > -1 ? districtIndex : 0]?.code || '' + ] + }, + confirmRegion() { const province = this.provinces[this.regionIndex[0]] const city = this.cities[this.regionIndex[1]] @@ -583,8 +752,18 @@ export default { .arrow { color: #ccc; font-size: 28rpx; margin-left: 10rpx; } } } +.region-tip { + padding: 0 32rpx 16rpx 32rpx; + text-align: center; + + text { + font-size: 26rpx; + color: #999; + } +} + .region-picker { - padding: 32rpx 0 0 0; + padding: 16rpx 0 0 0; .picker-view { width: 100%; height: 300rpx; display: flex; justify-content: center; align-items: center; .active { color: #222; font-weight: bold; }