From 89f6eb1fe10da9f49841b19fcc4a72d7668ca16e Mon Sep 17 00:00:00 2001 From: hly <2783385703@qq.com> Date: Thu, 28 Aug 2025 11:50:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=9C=B0=E5=9D=80=E8=A7=A3=E6=9E=90):=20?= =?UTF-8?q?=E6=94=B9=E8=BF=9B=E7=9C=81=E5=B8=82=E5=8C=BA=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91=E5=B9=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用更精确的正则表达式分别匹配省、市、区县信息 增加地址结构完整性验证,在信息不完整时提示用户手动选择 优化详细地址提取逻辑,去除多余空格 --- pages/subcomponent/select.vue | 58 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/pages/subcomponent/select.vue b/pages/subcomponent/select.vue index 19d60ff..662380c 100644 --- a/pages/subcomponent/select.vue +++ b/pages/subcomponent/select.vue @@ -335,7 +335,10 @@ export default { this.form.latitude = res.latitude this.form.longitude = res.longitude - var reg = /.+?(省|市|自治区|自治州|县|区)/g; + // 改进的正则表达式,更精确地匹配省市区结构 + var provinceReg = /(.+?(省|自治区|特别行政区))/; + var cityReg = /(.+?(市|自治州|地区|盟))/; + var districtReg = /(.+?(区|县|市|旗))/; let address = '' @@ -347,14 +350,40 @@ export default { } if(!address){ + this.showRegionPicker = true return } - let arr = address.match(reg) + // 使用更严格的省市区结构验证 + let province = '' + let city = '' + let district = '' + + // 提取省份 + let provinceMatch = address.match(provinceReg) + if (provinceMatch) { + province = provinceMatch[1] + address = address.replace(province, '') + } + + // 提取城市 + let cityMatch = address.match(cityReg) + if (cityMatch) { + city = cityMatch[1] + address = address.replace(city, '') + } + + // 提取区县 + let districtMatch = address.match(districtReg) + if (districtMatch) { + district = districtMatch[1] + address = address.replace(district, '') + } - // 判断是否提取到了省市县信息 - if (!arr || arr.length < 2) { - // 提取不到完整的省市县信息,打开区域选择器 + // 验证是否符合省市区/县的基本结构 + // 至少需要有省份和市/区县其中之一 + if (!province || (!city && !district)) { + // 地址结构不完整,直接打开区域选择器 uni.showToast({ title: '地址信息不完整,请手动选择', icon: 'none' @@ -363,13 +392,18 @@ export default { return } - const province = arr[0] || '' - const city = arr[1] || '' - const district = arr[2] || '' - - let detail = district || city || province || '' + // 如果只有省市没有区县,也提示手动选择 + if (province && city && !district) { + uni.showToast({ + title: '请选择具体的区县', + icon: 'none' + }) + this.showRegionPicker = true + return + } - this.form.addressDetails = address.substring(address.indexOf(detail) + detail.length) + // 设置详细地址(剩余部分) + this.form.addressDetails = address.trim() this.form.address = `${province}${city}${district}` // 尝试匹配到region数据中对应的省市区 @@ -749,4 +783,4 @@ export default { } } - \ No newline at end of file + \ No newline at end of file