[AI Generated]: feat(*): 完善个人信息页新增体重字段、身份证服务端校验及用户信息更新接口

This commit is contained in:
17792275749
2026-05-14 20:50:26 +08:00
parent b39bd7cb8d
commit d8621ebce6
2 changed files with 253 additions and 116 deletions
@@ -1,149 +1,266 @@
import { get, put } from '../../utils/request/index'
/** 表单数据模型 */ /** 表单数据模型 */
interface PersonalForm { interface PersonalForm {
/** 姓名 */ /** 姓名 */
name: string name: string
/** 身份证号 */ /** 身份证号 */
idCard: string idCard: string
/** 性别(由身份证解析,只读) */ /** 性别(由身份证解析只读) */
gender: string gender: string
/** 年龄(由身份证解析,只读) */ /** 年龄(由身份证解析只读) */
age: string age: string
/** 身高(cm) */ /** 身高(cm) */
height: string height: string
/** 体重(kg) */
weight: string
} }
/** 身份证解析结果 */ /**
* 身份证解析结果
* sex / birthday 用于接口提交,gender / age 用于页面展示
*/
interface IdCardParsed { interface IdCardParsed {
/** 性别,空字符串表示解析失败 */ gender: string // '男' | '女' | ''
gender: string age: string // 周岁字符串,解析失败为 ''
/** 年龄,空字符串表示解析失败 */ sex: number // 1=男 2=女,0=解析失败
age: string birthday: string // YYYY-MM-DD,解析失败为 ''
}
/** GET /weighingScale/v3/getUserInfoByIdcard 返回的 result 结构 */
interface UserInfoFromServer {
id: string | null
userId: string
parentUserId: string | null
scaleDeviceId: string
deviceId: string | null
deviceType: string
dataType: number
userType: string | null
idCard: string
height: number
weight: number
birthday: string
sex: number
avatar: string | null
phone: string
thirdId: string | null
realname: string
bmi: number
workNo: string
remark: string | null
relationType: string | null
sn: string
delFlag: number
sex_dictText: string
}
/** PUT /weighingScale/v3/update/user 请求体 */
type UpdateUserPayload = UserInfoFromServer & {
/** storage 中取出的 connectDeviceInfo 对象,JSON.stringify 后传入 */
connectDeviceInfo: string
} }
/** /**
* 完善个人信息页 * 完善个人信息页
* 收集姓名、身份证号、身高,并根据身份证自动解析性别年龄 * 收集姓名、身份证号、身高、体重,身份证满 18 位且姓名非空时自动调接口校验并回填性别年龄
* 提交后进入首页(tabBar)
*/ */
Page({ Page({
data: { data: {
// 表单数据
form: { form: {
name: '', // 姓名 name: '',
idCard: '', // 身份证号 idCard: '',
gender: '', // 性别(由身份证解析,只读) gender: '',
age: '', // 年龄(由身份证解析,只读) age: '',
height: '' // 身高(cm) height: '',
} as PersonalForm weight: ''
} as PersonalForm,
/** 正在请求身份证查询接口 */
querying: false,
/** 接口返回的用户信息,提交时透传给更新接口 */
serverResult: null as UserInfoFromServer | null
}, },
/** /**
* 姓名输入 * 姓名输入
* 如果身份证已合法则联动触发服务端查询
*/ */
onNameInput(e: WechatMiniprogram.Input) { onNameInput(e: WechatMiniprogram.Input) {
this.setData({ const name = e.detail.value
'form.name': e.detail.value this.setData({ 'form.name': name })
}); if (!name.trim()) {
this.setData({ serverResult: null })
return
}
if (this.isIdCardValid(this.data.form.idCard)) {
this.fetchUserInfoByIdCard(name.trim(), this.data.form.idCard)
}
}, },
/** /**
* 身份证号输入 * 身份证号输入
* 长度满 18 位时自动解析性别年龄,否则清空 * 本地解析性别年龄;满足合法格式且姓名非空时触发服务端查询
*/ */
onIdCardInput(e: WechatMiniprogram.Input) { onIdCardInput(e: WechatMiniprogram.Input) {
const idCard = (e.detail.value || '').trim().toUpperCase(); const idCard = (e.detail.value || '').trim().toUpperCase()
const parsed = this.parseIdCard(idCard); const parsed = this.parseIdCard(idCard)
this.setData({ this.setData({
'form.idCard': idCard, 'form.idCard': idCard,
'form.gender': parsed.gender, 'form.gender': parsed.gender,
'form.age': parsed.age 'form.age': parsed.age
}); })
if (!parsed.gender) {
// 身份证不合法,清除上次服务端结果
this.setData({ serverResult: null })
return
}
if (this.data.form.name.trim()) {
this.fetchUserInfoByIdCard(this.data.form.name.trim(), idCard)
}
}, },
/** /** 身高输入 */
* 身高输入
*/
onHeightInput(e: WechatMiniprogram.Input) { onHeightInput(e: WechatMiniprogram.Input) {
this.setData({ this.setData({ 'form.height': e.detail.value })
'form.height': e.detail.value },
});
/** 体重输入 */
onWeightInput(e: WechatMiniprogram.Input) {
this.setData({ 'form.weight': e.detail.value })
}, },
/** /**
* 解析身份证号,返回性别与年龄 * 校验身份证格式:18 位,前 17 位数字,末位数字或 X
* @param idCard 待校验的身份证号
*/
isIdCardValid(idCard: string): boolean {
return /^\d{17}[\dX]$/.test(idCard)
},
/**
* 解析身份证,返回性别、年龄、sex 数值、出生日期
* @param idCard 18 位身份证号 * @param idCard 18 位身份证号
*/ */
parseIdCard(idCard: string): IdCardParsed { parseIdCard(idCard: string): IdCardParsed {
// 简单校验:18 位且前 17 位为数字 const empty: IdCardParsed = { gender: '', age: '', sex: 0, birthday: '' }
const reg = /^\d{17}[\dX]$/; if (!this.isIdCardValid(idCard)) return empty
if (!reg.test(idCard)) {
return { gender: '', age: '' };
}
// 性别:第 17 位奇数为男,偶数为女 // 性别第 17 位奇数为男偶数为女
const genderCode = parseInt(idCard.charAt(16), 10); const genderCode = parseInt(idCard.charAt(16), 10)
const gender = genderCode % 2 === 1 ? '男' : '女'; const sex = genderCode % 2 === 1 ? 1 : 2
const gender = sex === 1 ? '男' : '女'
// 出生日期:第 7-14 位 // 出生日期第 7-14 位
const year = parseInt(idCard.substr(6, 4), 10); const year = parseInt(idCard.substr(6, 4), 10)
const month = parseInt(idCard.substr(10, 2), 10); const month = parseInt(idCard.substr(10, 2), 10)
const day = parseInt(idCard.substr(12, 2), 10); const day = parseInt(idCard.substr(12, 2), 10)
// 基本合法性校验 if (!year || month < 1 || month > 12 || day < 1 || day > 31) return empty
if (
!year || !month || !day ||
month < 1 || month > 12 ||
day < 1 || day > 31
) {
return { gender: '', age: '' };
}
// 计算周岁(看是否过了今年生日) // 计算周岁
const now = new Date(); const now = new Date()
let age = now.getFullYear() - year; let age = now.getFullYear() - year
const nowMonth = now.getMonth() + 1; const nowMonth = now.getMonth() + 1
const nowDay = now.getDate(); const nowDay = now.getDate()
if (nowMonth < month || (nowMonth === month && nowDay < day)) { if (nowMonth < month || (nowMonth === month && nowDay < day)) age -= 1
age -= 1; if (age < 0 || age > 150) return empty
}
if (age < 0 || age > 150) { const mm = String(month).padStart(2, '0')
return { gender: '', age: '' }; const dd = String(day).padStart(2, '0')
} const birthday = `${year}-${mm}-${dd}`
return { gender, age: String(age) }; return { gender, age: String(age), sex, birthday }
},
/**
* 调用服务端接口校验身份证与姓名,成功后用接口返回的身份证重新计算性别年龄
* @param name 用户填写的姓名
* @param idCard 用户填写的身份证号
*/
fetchUserInfoByIdCard(name: string, idCard: string) {
if (this.data.querying) return
this.setData({ querying: true, serverResult: null })
get<UserInfoFromServer>('weighingScale/v3/getUserInfoByIdcard', {
sn: '',
idCard,
realname: name
}, { loading: false })
.then(res => {
const serverResult = res.result
// 用接口返回的身份证重新计算性别和年龄(以服务端为准)
const parsed = this.parseIdCard(serverResult.idCard)
this.setData({
serverResult,
'form.gender': parsed.gender,
'form.age': parsed.age
})
})
.catch(() => {
// 查询失败静默处理,保留本地解析结果
})
.finally(() => {
this.setData({ querying: false })
})
}, },
/** /**
* 提交表单 * 提交表单
* 必填:姓名、身份证号(18 位且解析成功)、身高 * 校验全部必填项 → 构建 payload(透传 serverResult + 用户手动输入)→ PUT 更新接口
* 未通过时 toast 提示,通过后进入首页(tabBar)
*/ */
onSubmit() { onSubmit() {
const { name, idCard, gender, age, height } = this.data.form; const { name, idCard, gender, age, height, weight } = this.data.form
const heightNum = parseFloat(height); const heightNum = parseFloat(height)
const weightNum = parseFloat(weight)
if (!name.trim()) { if (!name.trim()) {
wx.showToast({ title: '请填写姓名', icon: 'none' }); wx.showToast({ title: '请填写姓名', icon: 'none' })
return; return
} }
if (idCard.length !== 18) { if (idCard.length !== 18) {
wx.showToast({ title: '请填写正确的身份证号', icon: 'none' }); wx.showToast({ title: '请填写正确的身份证号', icon: 'none' })
return; return
} }
if (!gender || !age) { if (!gender || !age) {
wx.showToast({ title: '身份证号有误,请检查', icon: 'none' }); wx.showToast({ title: '身份证号有误请检查', icon: 'none' })
return; return
} }
if (!height || isNaN(heightNum) || heightNum <= 0) { if (!height || isNaN(heightNum) || heightNum <= 0) {
wx.showToast({ title: '请填写身高', icon: 'none' }); wx.showToast({ title: '请填写身高', icon: 'none' })
return; return
}
if (!weight || isNaN(weightNum) || weightNum <= 0) {
wx.showToast({ title: '请填写体重', icon: 'none' })
return
}
if (!this.data.serverResult) {
wx.showToast({ title: '身份信息未验证,请检查姓名与身份证号', icon: 'none' })
return
} }
// TODO: 此处后续接入用户信息保存接口 const parsed = this.parseIdCard(idCard)
wx.switchTab({
url: '/pages/home/home' // connectDeviceInfo 存储时已 JSON.parse 为对象,提交时还原为 JSON 字符串
}); const connectDeviceInfoRaw = wx.getStorageSync('connectDeviceInfo')
const connectDeviceInfo: string = connectDeviceInfoRaw
? JSON.stringify(connectDeviceInfoRaw)
: ''
const serverResult = this.data.serverResult as UserInfoFromServer
const payload: UpdateUserPayload = {
...serverResult,
idCard,
realname: name.trim(),
height: heightNum,
weight: weightNum,
sex: parsed.sex,
birthday: parsed.birthday,
connectDeviceInfo
} }
});
put<unknown>('weighingScale/v3/update/user', payload)
.then(() => {
wx.switchTab({ url: '/pages/home/home' })
})
}
})
@@ -98,6 +98,26 @@
</view> </view>
</view> </view>
</view> </view>
<!-- 体重 -->
<view class="form-row">
<view class="form-label">
<text class="label-star">*</text>
<text class="label-text">体重</text>
</view>
<view class="form-control">
<view class="form-input-wrap">
<input class="form-input form-input-with-suffix"
type="digit"
maxlength="6"
placeholder="请填写体重"
placeholder-class="form-placeholder"
value="{{ form.weight }}"
bind:input="onWeightInput" />
<text class="form-suffix form-suffix-inside">kg</text>
</view>
</view>
</view>
</view> </view>
</view> </view>