Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
import { getUserInfoByIdCard, updateUser, type UserInfo } from '../../api/index'
|
|
import { parseIdCard } from '../../utils/common'
|
|
|
|
Page({
|
|
data: {
|
|
name: '',
|
|
idCard: '',
|
|
gender: '',
|
|
age: '',
|
|
height: '',
|
|
weight: '',
|
|
serverResult: null as UserInfo | null,
|
|
},
|
|
|
|
onNameInput(e: WechatMiniprogram.Input) {
|
|
this.setData({ name: e.detail.value.trim() })
|
|
},
|
|
|
|
/* 失焦时:清空查询结果 → 重新校验身份证,变更即失效 */
|
|
onNameBlur() {
|
|
if (!this.data.name) return
|
|
this.setData({ serverResult: null })
|
|
this.validateIdCard()
|
|
},
|
|
|
|
onIdCardInput(e: WechatMiniprogram.Input) {
|
|
const idCard = (e.detail.value || '').trim().toUpperCase()
|
|
this.setData({ idCard, serverResult: null })
|
|
if (idCard.length === 18) this.validateIdCard()
|
|
},
|
|
|
|
/* 独立的身份证合法性校验,合法且姓名已填则请求接口 */
|
|
validateIdCard() {
|
|
const { idCard, name } = this.data
|
|
const { gender, age } = parseIdCard(idCard)
|
|
this.setData({ gender, age })
|
|
if (gender && name) this.fetchUser()
|
|
},
|
|
|
|
fetchUser() {
|
|
getUserInfoByIdCard({
|
|
idCard: this.data.idCard,
|
|
realname: this.data.name,
|
|
}).then((res) => {
|
|
if (res.result) {
|
|
const { gender, age } = parseIdCard(res.result.idCard)
|
|
this.setData({ serverResult: res.result, gender, age })
|
|
}
|
|
}).catch(() => {})
|
|
},
|
|
|
|
onHeightInput(e: WechatMiniprogram.Input) {
|
|
this.setData({ height: e.detail.value })
|
|
},
|
|
|
|
onWeightInput(e: WechatMiniprogram.Input) {
|
|
this.setData({ weight: e.detail.value })
|
|
},
|
|
|
|
onSubmit() {
|
|
const { name, idCard, gender, height, weight, serverResult } = this.data
|
|
const heightNum = parseFloat(height)
|
|
const weightNum = parseFloat(weight)
|
|
|
|
if (!name) {
|
|
wx.showToast({ title: '请填写姓名', icon: 'none' })
|
|
return
|
|
}
|
|
if (!gender) {
|
|
wx.showToast({ title: '请填写正确的身份证号', icon: 'none' })
|
|
return
|
|
}
|
|
if (!height || isNaN(heightNum) || heightNum <= 0) {
|
|
wx.showToast({ title: '请填写身高', icon: 'none' })
|
|
return
|
|
}
|
|
if (!serverResult) {
|
|
wx.showToast({ title: '请先验证姓名与身份证号', icon: 'none' })
|
|
return
|
|
}
|
|
|
|
const parsed = parseIdCard(idCard)
|
|
|
|
updateUser({
|
|
...serverResult,
|
|
idCard,
|
|
realname: name,
|
|
height: heightNum,
|
|
weight: weightNum || 0,
|
|
sex: parsed.sex,
|
|
birthday: parsed.birthday,
|
|
}).then((res) => {
|
|
wx.setStorageSync('userInfo', res.result)
|
|
wx.switchTab({ url: '/pages/home/home' })
|
|
}).catch(() => {
|
|
wx.showToast({ title: '保存失败,请重试', icon: 'none' })
|
|
})
|
|
},
|
|
})
|