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