Browse Source

fix(地址解析): 改进省市区地址解析逻辑并增加验证

使用更精确的正则表达式分别匹配省、市、区县信息
增加地址结构完整性验证,在信息不完整时提示用户手动选择
优化详细地址提取逻辑,去除多余空格
master
前端-胡立永 1 week ago
parent
commit
89f6eb1fe1
1 changed files with 46 additions and 12 deletions
  1. +46
    -12
      pages/subcomponent/select.vue

+ 46
- 12
pages/subcomponent/select.vue View File

@ -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 {
}
}
</style>
</style>

Loading…
Cancel
Save