Files
bodyWeight/miniprogram/pages/supplementPersonal/supplementPersonal.ts
T

180 lines
6.2 KiB
TypeScript

import { getUserInfoByIdCard, updateUser, type UserInfo } from '../../api/index'
import { parseIdCard, isIdCardValid } from '../../utils/idCard/index'
Page({
data: {
name: '',
idCard: '',
gender: '',
age: '',
sex: 0,
birthday: '',
height: '',
weight: '',
/** 查询接口返回的原始用户信息 */
serverResult: null as UserInfo | null,
/** 身份证已存在(flag=101)时表单只读 */
readonly: false,
/** 是否展示「复用信息」确认弹框 */
showConfirmModal: false,
/** 查询接口是否报错(报错时提交前需重新查询) */
queryError: false,
},
/** 姓名/身份证变动后,查询回填的信息失效,统一清空 */
_clearQuery() {
this.setData({
serverResult: null,
gender: '',
age: '',
sex: 0,
birthday: '',
height: '',
weight: '',
readonly: false,
queryError: false,
})
},
onNameInput(e: WechatMiniprogram.Input) {
this.setData({ name: e.detail.value.trim() })
this._clearQuery()
},
// 失焦重新校验:姓名改完、身份证仍合法时重新发起查询
onNameBlur() {
if (!this.data.name) return
this.validateIdCard()
},
onIdCardInput(e: WechatMiniprogram.Input) {
const idCard = (e.detail.value || '').trim().toUpperCase()
this.setData({ idCard })
this._clearQuery()
if (idCard.length === 18) this.validateIdCard()
},
/* 独立的身份证合法性校验,合法且姓名已填则请求接口 */
validateIdCard() {
const { idCard, name } = this.data
if (isIdCardValid(idCard) && name) this.fetchUser()
},
fetchUser() {
getUserInfoByIdCard({
idCard: this.data.idCard,
realname: this.data.name,
}).then((res) => {
const serverResult = res.result
// 优先按服务器返回的身份证解析,未查到则回退用户输入
const { gender, age, sex, birthday } = parseIdCard(serverResult?.idCard || this.data.idCard)
this.setData({ gender, age, sex, birthday, queryError: false })
if (!serverResult) {
// wx.showToast({ title: res.msg || '未查询到用户信息', icon: 'none' })
return
}
// flag=101:身份证已存在,弹框确认是否复用
if (serverResult.flag === 101) {
this.setData({
serverResult,
name: serverResult.realname ?? '',
idCard: serverResult.idCard ?? '',
height: serverResult.height ? String(serverResult.height) : '',
weight: serverResult.weight ? String(serverResult.weight) : '',
readonly: true,
showConfirmModal: true,
})
return
}
// flag=null:查到但未建档,回填已有身高体重
if (serverResult.flag === null) {
this.setData({
serverResult,
height: serverResult.height ? String(serverResult.height) : '',
weight: serverResult.weight ? String(serverResult.weight) : '',
})
return
}
}).catch(() => {
// 查询报错:仍按身份证解析性别/年龄,标记需重新查询
const { gender, age, sex, birthday } = parseIdCard(this.data.idCard)
this.setData({ gender, age, sex, birthday, queryError: true })
})
},
// 复用已有信息:关闭弹框直接提交
onConfirmModalConfirm() {
this.setData({ showConfirmModal: false })
this.onSubmit()
},
// 不复用:清空整个表单重新填写
onConfirmModalCancel() {
this.setData({ showConfirmModal: false, name: '', idCard: '' })
this._clearQuery()
},
onHeightInput(e: WechatMiniprogram.Input) {
this.setData({ height: e.detail.value })
},
onWeightInput(e: WechatMiniprogram.Input) {
this.setData({ weight: e.detail.value })
},
async onSubmit() {
const { name, idCard, height, weight } = this.data
const heightNum = parseFloat(height)
const weightNum = parseFloat(weight)
if (!name) {
wx.showToast({ title: '请填写姓名', icon: 'none' })
return
}
if (!this.data.gender || !this.data.age || !this.data.birthday) {
wx.showToast({ title: '请填写正确的身份证号', icon: 'none' })
return
}
if (!height || isNaN(heightNum) || heightNum <= 0) {
wx.showToast({ title: '请填写身高', icon: 'none' })
return
}
let sex = this.data.sex
let birthday = this.data.birthday
// 查询接口报错 → 重新查询,成功后再提交
if (this.data.queryError) {
try {
const res = await getUserInfoByIdCard({ idCard, realname: name })
const result = res.result
const parsed = parseIdCard(result?.idCard || idCard)
this.setData({ serverResult: result, ...parsed, queryError: false })
sex = parsed.sex
birthday = parsed.birthday
} catch (err: any) {
console.error('[supplementPersonal] 重新查询失败:', err)
wx.showToast({ title: err?.msg || err?.message || '查询失败,请重试', icon: 'none' })
return
}
}
updateUser({
...(this.data.serverResult || {}),
idCard,
realname: name,
height: heightNum,
weight: weightNum || 0,
sex,
birthday,
}).then((res) => {
wx.setStorageSync('userInfo', res.result)
wx.switchTab({ url: '/pages/home/home' })
}).catch((err: any) => {
console.error('[supplementPersonal] 保存失败:', err)
wx.showToast({ title: err?.msg || err?.message || '保存失败,请重试', icon: 'none' })
})
},
})