fix(address-edit): 修复地址编辑页粘贴文本智能识别问题
- 优化姓名匹配优先查找标签“收件人”等后面的名字,提升识别准确率 - 清理详细地址中标签文本,去除“收件人”“手机号码”“所在地区”“详细地址”等冗余字段 - 保留街道及详细地址完整信息,避免误删有效内容 - 类型定义中新增 country,调整 isDefault 类型为 boolean,增加 address 字段 - 使用 Python 测试验证解析效果,确保姓名、电话、省市区和详细地址准确提取 - 通过类型检查无误,提升代码健壮性和维护便利性
This commit is contained in:
@@ -101,6 +101,7 @@ const AddressEditPage: React.FC = () => {
|
||||
const [formData, setFormData] = useState<Partial<ShopUserAddress>>({
|
||||
name: '',
|
||||
phone: '',
|
||||
country: '',
|
||||
province: '',
|
||||
city: '',
|
||||
region: '',
|
||||
@@ -148,15 +149,22 @@ const AddressEditPage: React.FC = () => {
|
||||
const parseAddressText = (text: string) => {
|
||||
const result: any = {}
|
||||
|
||||
// 手机号正则
|
||||
// 手机号正则(11 位国内手机号)
|
||||
const phoneRegex = /1[3-9]\d{9}/
|
||||
const phoneMatch = text.match(phoneRegex)
|
||||
if (phoneMatch) result.phone = phoneMatch[0]
|
||||
|
||||
// 姓名正则(2-4个中文字符,通常在开头)
|
||||
const nameRegex = /^[\u4e00-\u9fa5]{2,4}/
|
||||
const nameMatch = text.match(nameRegex)
|
||||
if (nameMatch) result.name = nameMatch[0]
|
||||
// 姓名正则:优先匹配“收件人/收货人/姓名/联系人:”后面的 2-4 个中文字符
|
||||
const nameWithPrefixRegex = /(?:收件人|收货人|姓名|联系人)[::]\s*([\u4e00-\u9fa5]{2,4})/
|
||||
const nameWithPrefixMatch = text.match(nameWithPrefixRegex)
|
||||
if (nameWithPrefixMatch) {
|
||||
result.name = nameWithPrefixMatch[1]
|
||||
} else {
|
||||
// 兜底:文本开头 2-4 个中文字符
|
||||
const nameRegex = /^[\u4e00-\u9fa5]{2,4}/
|
||||
const nameMatch = text.match(nameRegex)
|
||||
if (nameMatch) result.name = nameMatch[0]
|
||||
}
|
||||
|
||||
// 省市区识别
|
||||
const regionResult = parseRegion(text)
|
||||
@@ -166,13 +174,29 @@ const AddressEditPage: React.FC = () => {
|
||||
result.region = regionResult.region
|
||||
}
|
||||
|
||||
// 详细地址提取
|
||||
// 详细地址提取:先把常见的标签字段和已知字段去掉,再清洗标点
|
||||
let addressText = text
|
||||
|
||||
// 去除已提取的姓名、手机号
|
||||
if (result.name) addressText = addressText.replace(result.name, '')
|
||||
if (result.phone) addressText = addressText.replace(result.phone, '')
|
||||
|
||||
// 去除常见标签文案(如“收件人:”、“手机号码:”、“所在地区:”、“详细地址:”)
|
||||
addressText = addressText
|
||||
.replace(/(?:收件人|收货人|姓名|联系人)[::]\s*/g, '')
|
||||
.replace(/(?:手机号码|手机号|电话|联系电话)[::]\s*/g, '')
|
||||
.replace(/(?:所在地区|地区|省市区)[::]\s*/g, '')
|
||||
.replace(/(?:详细地址|地址|街道)[::]\s*/g, '')
|
||||
|
||||
// 去除已识别出的省市区
|
||||
if (result.province) addressText = addressText.replace(result.province, '')
|
||||
if (result.city) addressText = addressText.replace(result.city, '')
|
||||
if (result.region) addressText = addressText.replace(result.region, '')
|
||||
|
||||
// 去除零散的标签关键字(如“手机号码”、“所在地区”、“详细地址”本身)
|
||||
addressText = addressText
|
||||
.replace(/(?:收件人|收货人|手机号码|手机号|所在地区|详细地址)/g, '')
|
||||
|
||||
result.address = addressText.replace(/[,,。\s]+/g, '').trim()
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user