fix(addUser): 家庭用户身份证查询报错时提交前重新查询并重置状态

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-09-02 19:34:36 +08:00
co-authored by Claude Opus 4.7
parent 4e51432b8a
commit 55cdeae1ba
+39 -6
View File
@@ -54,6 +54,7 @@ Page({
employeeQueryResult: null as Record<string, any> | null,
rawUser: null as Record<string, any> | null,
isEdit: false,
queryError: false,
},
onLoad(options: Record<string, string>) {
@@ -121,6 +122,7 @@ Page({
serverResult: null,
form: { workNo: '', name: '', idCard: '', gender: '', age: '', height: '', weight: '', sex: 0 },
birthday: '',
queryError: false,
})
},
@@ -200,6 +202,7 @@ Page({
'form.weight': '',
birthday: '',
readonly: false,
queryError: false,
})
},
@@ -222,6 +225,7 @@ Page({
'form.weight': '',
birthday: '',
readonly: false,
queryError: false,
})
if (idCard.length === 18) this.validateIdCard()
},
@@ -241,9 +245,8 @@ Page({
}).then((res) => {
const serverResult = res.result
const { gender, age, sex, birthday } = parseIdCard(serverResult?.idCard || this.data.form.idCard)
this.setData({ 'form.gender': gender, 'form.age': age, 'form.sex': sex, birthday })
this.setData({ 'form.gender': gender, 'form.age': age, 'form.sex': sex, birthday, queryError: false })
if (!serverResult) {
wx.showToast({ title: res.msg || '未查询到用户信息', icon: 'none' })
return
}
if (serverResult.flag === 101) {
@@ -266,7 +269,11 @@ Page({
})
return
}
}).catch(() => {})
}).catch(() => {
// 查询报错:仍按身份证解析性别/年龄,标记需重新查询
const { gender, age, sex, birthday } = parseIdCard(this.data.form.idCard)
this.setData({ 'form.gender': gender, 'form.age': age, 'form.sex': sex, birthday, queryError: true })
})
},
/** 家庭用户:性别切换 */
@@ -302,6 +309,7 @@ Page({
serverResult: null,
form: { workNo: '', name: '', idCard: '', gender: '', age: '', height: '', weight: '', sex: 0 },
birthday: '',
queryError: false,
})
},
@@ -310,7 +318,7 @@ Page({
},
/** 提交用户 */
onSubmit() {
async onSubmit() {
const { name, idCard, gender, height, weight, sex } = this.data.form
const { userType } = this.data
const heightNum = parseFloat(height)
@@ -344,6 +352,31 @@ Page({
}
}
// 家庭用户新增:身份证查询报错 → 重新查询,成功后再提交
let sexFinal = sex
let birthdayFinal = this.data.birthday
if (userType === 'family' && !editId && this.data.queryError) {
try {
const res = await getUserInfoByIdCard({ sn: targetSn, idCard, realname: name.trim() })
const result = res.result
const parsed = parseIdCard(result?.idCard || idCard)
this.setData({
serverResult: result,
'form.gender': parsed.gender,
'form.age': parsed.age,
'form.sex': parsed.sex,
birthday: parsed.birthday,
queryError: false,
})
sexFinal = parsed.sex
birthdayFinal = parsed.birthday
} catch (err: any) {
console.error('[addUser] 重新查询失败:', err)
wx.showToast({ title: err?.msg || err?.message || '查询失败,请重试', icon: 'none' })
return
}
}
const userInfo = wx.getStorageSync('userInfo') as Record<string, any> | null
// 编辑:以详情接口返回为 base;新增:员工用查询结果、家庭用身份证查询结果
const baseSpread = editId
@@ -360,8 +393,8 @@ Page({
scaleDeviceId: targetSn,
realname: name.trim(),
idCard: idCard || null,
sex,
birthday: this.data.birthday,
sex: sexFinal,
birthday: birthdayFinal,
height: heightNum,
weight: isNaN(weightNum) ? 0 : weightNum,
mode: getApp<IAppOption>().globalData.operationsEngineer ? 'ops' : 'normal',