feat(user): 补充个人信息页接入身份证已存在复用弹框与登录引导

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-08-24 11:54:33 +08:00
co-authored by Claude Sonnet 4.6
parent 9911dea32f
commit 90f84cc7df
13 changed files with 353 additions and 72 deletions
+5 -5
View File
@@ -25,12 +25,12 @@ Page({
const { token, user } = res.result
// 持久化登录凭证和用户信息
wx.setStorageSync('token', token)
// if (user) {
// wx.setStorageSync('userInfo', user)
if (user) {
wx.setStorageSync('userInfo', user)
wx.reLaunch({ url: '/pages/home/home' })
// } else {
// wx.reLaunch({ url: '/pages/supplementPersonal/supplementPersonal' })
// }
} else {
wx.reLaunch({ url: '/pages/supplementPersonal/supplementPersonal' })
}
}).catch((err) => {
wx.showToast({ title: err.msg || '登录失败,请重试', icon: 'none' })
}).finally(() => {
@@ -1,5 +1,7 @@
{
"usingComponents": {},
"usingComponents": {
"user-info-confirm-modal": "/components/userInfoConfirmModal/userInfoConfirmModal"
},
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#FFFFFF",
"navigationBarTitleText": "完善个人信息"
@@ -1,5 +1,5 @@
import { getUserInfoByIdCard, updateUser, type UserInfo } from '../../api/index'
import { parseIdCard } from '../../utils/common'
import { parseIdCard, isIdCardValid } from '../../utils/idCard/index'
Page({
data: {
@@ -7,34 +7,55 @@ Page({
idCard: '',
gender: '',
age: '',
sex: 0,
birthday: '',
height: '',
weight: '',
/** 查询接口返回的原始用户信息 */
serverResult: null as UserInfo | null,
/** 身份证已存在(flag=101)时表单只读 */
readonly: false,
/** 是否展示「复用信息」确认弹框 */
showConfirmModal: false,
},
/** 姓名/身份证变动后,查询回填的信息失效,统一清空 */
_clearQuery() {
this.setData({
serverResult: null,
gender: '',
age: '',
sex: 0,
birthday: '',
height: '',
weight: '',
readonly: false,
})
},
onNameInput(e: WechatMiniprogram.Input) {
this.setData({ name: e.detail.value.trim() })
this._clearQuery()
},
/* 失焦时:清空查询结果 → 重新校验身份证,变更即失效 */
// 失焦重新校验:姓名改完、身份证仍合法时重新发起查询
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 })
this.setData({ idCard })
this._clearQuery()
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()
if (isIdCardValid(idCard) && name) this.fetchUser()
},
fetchUser() {
@@ -42,13 +63,51 @@ Page({
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 })
const serverResult = res.result
// 优先按服务器返回的身份证解析,未查到则回退用户输入
const { gender, age, sex, birthday } = parseIdCard(serverResult?.idCard || this.data.idCard)
this.setData({ gender, age, sex, birthday })
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(() => {})
},
// 复用已有信息:关闭弹框直接提交
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 })
},
@@ -58,7 +117,7 @@ Page({
},
onSubmit() {
const { name, idCard, gender, height, weight, serverResult } = this.data
const { name, idCard, gender, sex, birthday, height, weight, serverResult } = this.data
const heightNum = parseFloat(height)
const weightNum = parseFloat(weight)
@@ -79,16 +138,14 @@ Page({
return
}
const parsed = parseIdCard(idCard)
updateUser({
...serverResult,
idCard,
realname: name,
height: heightNum,
weight: weightNum || 0,
sex: parsed.sex,
birthday: parsed.birthday,
sex,
birthday,
}).then((res) => {
wx.setStorageSync('userInfo', res.result)
wx.switchTab({ url: '/pages/home/home' })
@@ -18,6 +18,7 @@
placeholder="请填写真实姓名"
placeholder-class="form-placeholder"
value="{{ name }}"
disabled="{{ readonly }}"
bind:input="onNameInput"
bind:blur="onNameBlur" />
</view>
@@ -35,6 +36,7 @@
placeholder="请填写身份证号"
placeholder-class="form-placeholder"
value="{{ idCard }}"
disabled="{{ readonly }}"
bind:input="onIdCardInput" />
</view>
</view>
@@ -77,6 +79,7 @@
placeholder="请填写身高"
placeholder-class="form-placeholder"
value="{{ height }}"
disabled="{{ readonly }}"
bind:input="onHeightInput" />
<text class="form-suffix form-suffix-inside">cm</text>
</view>
@@ -95,6 +98,7 @@
placeholder="请填写体重"
placeholder-class="form-placeholder"
value="{{ weight }}"
disabled="{{ readonly }}"
bind:input="onWeightInput" />
<text class="form-suffix form-suffix-inside">kg</text>
</view>
@@ -104,4 +108,6 @@
</view>
<view class="submit-btn" bind:tap="onSubmit">完成并进入首页</view>
<user-info-confirm-modal show="{{ showConfirmModal }}" userInfo="{{ serverResult }}" bind:confirm="onConfirmModalConfirm" bind:cancel="onConfirmModalCancel" />
</view>