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>
This commit is contained in:
17792275749
2026-08-10 14:21:00 +08:00
co-authored by Claude Sonnet 4.6
parent 439f560218
commit 83d909b95b
96 changed files with 370 additions and 9040 deletions
@@ -1,4 +1,4 @@
{
"usingComponents": {},
"navigationBarTitleText": "完善个人信息"
"usingComponents": {},
"navigationBarTitleText": "完善个人信息"
}
@@ -1,328 +1,88 @@
import { get, put } from '../../utils/request/index'
import { lefuService } from '../../lefu/index'
import type { DeviceMember } from '../../lefu/index'
import { getUserInfoByIdCard, updateUser, type UserInfo } from '../../api/index'
import { parseIdCard } from '../../utils/common'
/** 表单数据模型 */
interface PersonalForm {
/** 姓名 */
name: string
/** 身份证号 */
idCard: string
/** 性别(由身份证解析,只读) */
gender: string
/** 年龄(由身份证解析,只读) */
age: string
/** 身高(cm) */
height: string
/** 体重(kg) */
weight: string
}
/**
* 身份证解析结果
* sex / birthday 用于接口提交,gender / age 用于页面展示
*/
interface IdCardParsed {
gender: string // '男' | '女' | ''
age: string // 周岁字符串,解析失败为 ''
sex: number // 1=男 2=女,0=解析失败
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 位且姓名非空时自动调接口校验并回填性别年龄
*/
Page({
data: {
form: {
name: '',
idCard: '',
gender: '',
age: '',
height: '',
weight: ''
} as PersonalForm,
/** 正在请求身份证查询接口 */
querying: false,
/** 接口返回的用户信息,提交时透传给更新接口 */
serverResult: null as UserInfoFromServer | null,
/** 表单是否只读(从添加设备流程过来时为 true) */
readonly: false
name: '',
idCard: '',
gender: '',
age: '',
height: '',
weight: '',
serverResult: null as UserInfo | null,
},
onLoad() {
// 已有连接设备 → 从缓存 userInfo 回填表单并禁用编辑
const connectDeviceInfo = wx.getStorageSync('connectDeviceInfo')
if (!connectDeviceInfo || JSON.stringify(connectDeviceInfo) === '{}') return
const userInfo = wx.getStorageSync('userInfo') as Record<string, any> | null
if (!userInfo?.userId) return
const parsed = this.parseIdCard(userInfo.idCard ?? '')
this.setData({
readonly: true,
'form.name': userInfo.realname ?? '',
'form.idCard': userInfo.idCard ?? '',
'form.gender': parsed.gender || userInfo.sex_dictText || '',
'form.age': parsed.age || '',
'form.height': userInfo.height != null ? String(userInfo.height) : '',
'form.weight': userInfo.weight != null ? String(userInfo.weight) : '',
})
},
/**
* 姓名输入
* 如果身份证已合法则联动触发服务端查询
*/
onNameInput(e: WechatMiniprogram.Input) {
const 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)
}
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()
const parsed = this.parseIdCard(idCard)
this.setData({
'form.idCard': idCard,
'form.gender': parsed.gender,
'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)
}
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({ 'form.height': e.detail.value })
this.setData({ height: e.detail.value })
},
/** 体重输入 */
onWeightInput(e: WechatMiniprogram.Input) {
this.setData({ 'form.weight': e.detail.value })
this.setData({ weight: e.detail.value })
},
/**
* 校验身份证格式:18 位,前 17 位数字,末位数字或 X
* @param idCard 待校验的身份证号
*/
isIdCardValid(idCard: string): boolean {
return /^\d{17}[\dX]$/.test(idCard)
},
/**
* 解析身份证,返回性别、年龄、sex 数值、出生日期
* @param idCard 18 位身份证号
*/
parseIdCard(idCard: string): IdCardParsed {
const empty: IdCardParsed = { gender: '', age: '', sex: 0, birthday: '' }
if (!this.isIdCardValid(idCard)) return empty
// 性别:第 17 位奇数为男,偶数为女
const genderCode = parseInt(idCard.charAt(16), 10)
const sex = genderCode % 2 === 1 ? 1 : 2
const gender = sex === 1 ? '男' : '女'
// 出生日期:第 7-14 位
const year = parseInt(idCard.substr(6, 4), 10)
const month = parseInt(idCard.substr(10, 2), 10)
const day = parseInt(idCard.substr(12, 2), 10)
if (!year || month < 1 || month > 12 || day < 1 || day > 31) return empty
// 计算周岁
const now = new Date()
let age = now.getFullYear() - year
const nowMonth = now.getMonth() + 1
const nowDay = now.getDate()
if (nowMonth < month || (nowMonth === month && nowDay < day)) age -= 1
if (age < 0 || age > 150) return empty
const mm = String(month).padStart(2, '0')
const dd = String(day).padStart(2, '0')
const birthday = `${year}-${mm}-${dd}`
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: lefuService.deviceInfo?.serialNumber ?? '',
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 })
})
},
/**
* 提交表单
* 校验全部必填项 → 构建 payload(透传 serverResult + 用户手动输入)→ PUT 更新接口
*/
onSubmit() {
const { name, idCard, gender, age, height, weight } = this.data.form
const { name, idCard, gender, height, weight, serverResult } = this.data
const heightNum = parseFloat(height)
const weightNum = parseFloat(weight)
if (!name.trim()) {
wx.showToast({ title: '请填写姓名', icon: 'none' })
return
}
if (idCard.length !== 18) {
wx.showToast({ title: '请填写正确的身份证号', icon: 'none' })
return
}
if (!gender || !age) {
wx.showToast({ title: '身份证号有误,请检查', icon: 'none' })
return
}
if (!height || isNaN(heightNum) || heightNum <= 0) {
wx.showToast({ title: '请填写身高', icon: 'none' })
return
}
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 = this.parseIdCard(idCard)
const parsed = parseIdCard(idCard)
// connectDeviceInfo 存储时已 JSON.parse 为对象,提交时还原为 JSON 字符串
const connectDeviceInfoRaw = wx.getStorageSync('connectDeviceInfo')
const connectDeviceInfo: string = connectDeviceInfoRaw ? JSON.stringify(connectDeviceInfoRaw) : ''
let payload: UpdateUserPayload
if (this.data.readonly) {
// readonly 模式:从缓存 userInfo 构建 payload
const userInfo = wx.getStorageSync('userInfo') as Record<string, any>
const { connectDeviceInfo: _c, ...rest } = userInfo
payload = {
...rest,
idCard,
realname: name.trim(),
height: heightNum,
weight: weightNum,
sex: parsed.sex,
birthday: parsed.birthday,
sn: lefuService.deviceInfo?.serialNumber ?? '',
scaleDeviceId: lefuService.deviceInfo?.serialNumber ?? '',
connectDeviceInfo,
} as UpdateUserPayload
} else {
// 正常模式:需要 serverResult
if (!this.data.serverResult) {
wx.showToast({ title: '身份信息未验证,请检查姓名与身份证号', icon: 'none' })
return
}
const serverResult = this.data.serverResult as UserInfoFromServer
payload = {
...serverResult,
idCard,
realname: name.trim(),
height: heightNum,
weight: weightNum,
sex: parsed.sex,
birthday: parsed.birthday,
sn: lefuService.deviceInfo?.serialNumber ?? '',
scaleDeviceId: lefuService.deviceInfo?.serialNumber ?? '',
connectDeviceInfo,
}
}
put<UpdateUserPayload>('weighingScale/v3/update/user', payload)
.then(res => {
const { connectDeviceInfo: _, ...userInfo } = res.result
wx.setStorageSync('userInfo', userInfo)
const mainUser: DeviceMember = {
id: payload.userId,
name: payload.realname,
gender: payload.sex === 1 ? 1 : 0,
age: parseInt(parsed.age, 10) || 0,
height: payload.height,
isSelf: true,
}
wx.showLoading({ title: '加载中...', mask: true })
lefuService.syncMembersToDevice([mainUser])
.then(() => {
wx.hideLoading()
wx.switchTab({ url: '/pages/home/home' })
})
.catch((err: Error) => {
wx.hideLoading()
wx.showToast({ title: err.message || '主用户下发失败', icon: 'none' })
})
})
}
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' })
})
},
})
@@ -1,129 +1,106 @@
<view class="supplement-personal">
<view class="tips">
<view class="tips-icon">!</view>
<text class="tips-text">请完整填写信息,用于精准计算体脂数据</text>
</view>
<!-- 顶部蓝色提示条 -->
<view class="tips">
<view class="tips-icon">!</view>
<text class="tips-text">请完整填写信息,用于精准计算体脂数据</text>
</view>
<view class="form">
<view class="form-card">
<view class="form-row">
<view class="form-label">
<text class="label-text">姓名</text>
<text class="label-star">*</text>
</view>
<view class="form-control">
<input class="form-input"
type="text"
maxlength="20"
placeholder="请填写真实姓名"
placeholder-class="form-placeholder"
value="{{ name }}"
bind:input="onNameInput"
bind:blur="onNameBlur" />
</view>
</view>
<view class="form">
<!-- 白色圆角表单卡片 -->
<view class="form-card">
<view class="form-row">
<view class="form-label">
<text class="label-text">身份证号</text>
<text class="label-star">*</text>
</view>
<view class="form-control">
<input class="form-input"
type="idcard"
maxlength="18"
placeholder="请填写身份证号"
placeholder-class="form-placeholder"
value="{{ idCard }}"
bind:input="onIdCardInput" />
</view>
</view>
<!-- 姓名 -->
<view class="form-row">
<view class="form-label">
<text class="label-text">姓名</text>
<text class="label-star">*</text>
</view>
<view class="form-control">
<input class="form-input"
type="text"
maxlength="20"
placeholder="请填写真实姓名"
placeholder-class="form-placeholder"
value="{{ form.name }}"
disabled="{{ readonly }}"
bind:input="onNameInput" />
</view>
</view>
<view class="form-row">
<view class="form-label">
<text class="label-text">性别</text>
</view>
<view class="form-control">
<view class="form-readonly">
<text wx:if="{{ gender }}" class="readonly-text">{{ gender }}</text>
<text wx:else class="readonly-placeholder">自动生成,无需填写</text>
</view>
</view>
</view>
<!-- 身份证号 -->
<view class="form-row">
<view class="form-label">
<text class="label-text">身份证号</text>
<text class="label-star">*</text>
</view>
<view class="form-control">
<input class="form-input"
type="idcard"
maxlength="18"
placeholder="请填写身份证号"
placeholder-class="form-placeholder"
value="{{ form.idCard }}"
disabled="{{ readonly }}"
bind:input="onIdCardInput" />
</view>
</view>
<view class="form-row">
<view class="form-label">
<text class="label-text">年龄</text>
</view>
<view class="form-control">
<view class="form-readonly form-readonly-with-suffix">
<text wx:if="{{ age }}" class="readonly-text">{{ age }}</text>
<text wx:else class="readonly-placeholder">自动生成,无需填写</text>
<text class="form-suffix">岁</text>
</view>
</view>
</view>
<!-- 性别(由身份证自动生成) -->
<view class="form-row">
<view class="form-label">
<text class="label-text">性别</text>
</view>
<view class="form-control">
<view class="form-readonly">
<block wx:if="{{ form.gender }}">
<text class="readonly-text">{{ form.gender }}</text>
</block>
<block wx:else>
<text class="readonly-placeholder">自动生成,无需填写</text>
</block>
</view>
</view>
</view>
<view class="form-row">
<view class="form-label">
<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="5"
placeholder="请填写身高"
placeholder-class="form-placeholder"
value="{{ height }}"
bind:input="onHeightInput" />
<text class="form-suffix form-suffix-inside">cm</text>
</view>
</view>
</view>
<!-- 年龄(由身份证自动生成) -->
<view class="form-row">
<view class="form-label">
<text class="label-text">年龄</text>
</view>
<view class="form-control">
<view class="form-readonly form-readonly-with-suffix">
<block wx:if="{{ form.age }}">
<text class="readonly-text">{{ form.age }}</text>
</block>
<block wx:else>
<text class="readonly-placeholder">自动生成,无需填写</text>
</block>
<text class="form-suffix">岁</text>
</view>
</view>
</view>
<view class="form-row">
<view class="form-label">
<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="{{ weight }}"
bind:input="onWeightInput" />
<text class="form-suffix form-suffix-inside">kg</text>
</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="5"
placeholder="请填写身高"
placeholder-class="form-placeholder"
value="{{ form.height }}"
disabled="{{ readonly }}"
bind:input="onHeightInput" />
<text class="form-suffix form-suffix-inside">cm</text>
</view>
</view>
</view>
<!-- 体重 -->
<view class="form-row">
<view class="form-label">
<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 }}"
disabled="{{ readonly }}"
bind:input="onWeightInput" />
<text class="form-suffix form-suffix-inside">kg</text>
</view>
</view>
</view>
</view>
</view>
<!-- 底部提交按钮 -->
<view class="submit-btn" bind:tap="onSubmit">完成并进入首页</view>
<view class="submit-btn" bind:tap="onSubmit">完成</view>
</view>