diff --git a/miniprogram/pages/addMember/addMember.ts b/miniprogram/pages/addMember/addMember.ts index f2cd7ba..e87ea4d 100644 --- a/miniprogram/pages/addMember/addMember.ts +++ b/miniprogram/pages/addMember/addMember.ts @@ -12,6 +12,34 @@ interface SysUserDevice { birthday: string } +/** 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 +} + /** 表单数据模型 */ interface MemberForm { /** 姓名 */ @@ -72,6 +100,12 @@ Page({ /** 从抽屉选中的完整成员数据,提交时作为 base spread */ selectedMember: null as ExistingMember | null, showSelectDrawer: false, + /** 正在请求身份证查询接口 */ + querying: false, + /** 是否已完成一次身份证查询(成功 200 即视为完成,无论 result 是否为 null) */ + hasQueried: false, + /** 接口返回的用户信息,姓名 + 身份证联动校正性别年龄 */ + serverResult: null as UserInfoFromServer | null, }, onLoad(options: Record) { @@ -91,11 +125,22 @@ Page({ 'form.height': String(u.height ?? ''), 'form.weight': String(u.weight ?? ''), }) + if ((u.realname ?? '').trim() && this.isIdCardValid(u.idCard ?? '')) { + this.fetchUserInfoByIdCard(u.realname.trim(), u.idCard) + } }) }, onNameInput(e: WechatMiniprogram.Input) { - this.setData({ 'form.name': e.detail.value }) + const name = e.detail.value + this.setData({ 'form.name': name }) + if (!name.trim()) { + this.setData({ serverResult: null, hasQueried: false }) + return + } + if (this.isIdCardValid(this.data.form.idCard)) { + this.fetchUserInfoByIdCard(name.trim(), this.data.form.idCard) + } }, onIdCardInput(e: WechatMiniprogram.Input) { @@ -106,6 +151,13 @@ Page({ 'form.gender': parsed.gender, 'form.age': parsed.age }) + if (!parsed.gender) { + this.setData({ serverResult: null, hasQueried: false }) + return + } + if (this.data.form.name.trim()) { + this.fetchUserInfoByIdCard(this.data.form.name.trim(), idCard) + } }, onHeightInput(e: WechatMiniprogram.Input) { @@ -116,12 +168,19 @@ Page({ this.setData({ 'form.weight': e.detail.value }) }, + /** + * 校验身份证格式:18 位,前 17 位数字,末位数字或 X + */ + isIdCardValid(idCard: string): boolean { + return /^\d{17}[\dX]$/.test(idCard) + }, + /** * 解析身份证,返回性别、年龄、sex 数值、出生日期 */ parseIdCard(idCard: string): IdCardParsed { const empty: IdCardParsed = { gender: '', age: '', sex: 0, birthday: '' } - if (!/^\d{17}[\dX]$/.test(idCard)) return empty + if (!this.isIdCardValid(idCard)) return empty const genderCode = parseInt(idCard.charAt(16), 10) const sex = genderCode % 2 === 1 ? 1 : 2 @@ -142,6 +201,41 @@ Page({ return { gender, age: String(age), sex, birthday: `${year}-${mm}-${dd}` } }, + /** + * 调用服务端接口校验身份证与姓名,成功后用接口返回的身份证重新计算性别年龄 + * result 为 null 视为「查无此人」,标记 hasQueried 允许新增提交 + */ + fetchUserInfoByIdCard(name: string, idCard: string) { + if (this.data.querying) return + this.setData({ querying: true, serverResult: null, hasQueried: false }) + + get('weighingScale/v3/getUserInfoByIdcard', { + sn: lefuService.deviceInfo?.serialNumber ?? '', + idCard, + realname: name + }, { loading: false }) + .then(res => { + const serverResult = res.result + if (!serverResult) { + this.setData({ hasQueried: true }) + return + } + const parsed = this.parseIdCard(serverResult.idCard) + this.setData({ + serverResult, + hasQueried: true, + 'form.gender': parsed.gender, + 'form.age': parsed.age + }) + }) + .catch(() => { + // 查询失败保留本地解析结果,hasQueried 维持 false 以便用户重试时再次触发 + }) + .finally(() => { + this.setData({ querying: false }) + }) + }, + onTapSelectExisting() { this.setData({ showSelectDrawer: true }) }, @@ -194,13 +288,20 @@ Page({ wx.showToast({ title: '请填写体重', icon: 'none' }) return } + if (!this.data.hasQueried) { + wx.showToast({ title: this.data.querying ? '身份信息验证中,请稍候' : '请检查姓名与身份证号', icon: 'none' }) + return + } const parsed = this.parseIdCard(idCard) const userInfo = wx.getStorageSync('userInfo') const connectDeviceInfoRaw = wx.getStorageSync('connectDeviceInfo') + const serverResult = this.data.serverResult + // selectedMember(抽屉选中) → serverResult(接口校验,可能为 null) → 表单/本地解析(用户输入) 依次覆盖 const payload: Record = { ...(this.data.selectedMember ?? {}), + ...(serverResult ?? {}), parentUserId: userInfo?.userId ?? '', scaleDeviceId: lefuService.deviceInfo?.serialNumber ?? '', realname: name.trim(), diff --git a/miniprogram/pages/home/home.ts b/miniprogram/pages/home/home.ts index 93bfad9..997604b 100644 --- a/miniprogram/pages/home/home.ts +++ b/miniprogram/pages/home/home.ts @@ -152,9 +152,12 @@ Page({ lefuService.checkAndUpgrade() .then(() => { wx.showLoading({ title: '固件升级中...', mask: true }) - setTimeout(() => { - wx.hideLoading() - }, 15000) + setTimeout(() => { + wx.hideLoading() + // 升级完成后设备重启,旧连接与订阅失效,重新扫描连接 + this.setData({ 'device.connected': false }) + lefuService.autoConnect() + }, 15000) }) .catch(() => {}) },