Files
bodyWeight/miniprogram/pages/supplementPersonal/supplementPersonal.ts
T
17792275749andClaude Sonnet 4.6 83d909b95b refactor: 项目重构,仅保留 login 和 supplementPersonal 页面
删除 11 个旧页面、组件、lefu SDK、旧资源文件。
新增 request 封装(GET/POST/PUT)、api 层、utils/common 身份证工具。
重写 login(登录流程提取到 app.ts) 和 supplementPersonal(简化表单逻辑)。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-08-10 14:21:00 +08:00

89 lines
2.9 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({
sn: '',
idCard: this.data.idCard,
realname: this.data.name,
}).then((res) => {
if (res.data) {
const { gender, age } = parseIdCard(res.data.idCard)
this.setData({ serverResult: res.data, 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) return wx.showToast({ title: '请填写姓名', icon: 'none' })
if (!gender) return wx.showToast({ title: '请填写正确的身份证号', icon: 'none' })
if (!height || isNaN(heightNum) || heightNum <= 0) return wx.showToast({ title: '请填写身高', icon: 'none' })
if (!serverResult) return wx.showToast({ title: '请先验证姓名与身份证号', icon: 'none' })
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.data)
wx.showToast({ title: '保存成功', icon: 'success' })
}).catch(() => {
wx.showToast({ title: '保存失败,请重试', icon: 'none' })
})
},
})