[AI Generated]: feat(*): 对接数据页/测量报告真实接口,完善成员管理、蓝牙连接及成员选择抽屉逻辑
This commit is contained in:
@@ -1,3 +1,16 @@
|
||||
import { get, put } from '../../utils/request/index'
|
||||
import { lefuService } from '../../lefu/index'
|
||||
|
||||
/** GET /weighingScale/v3/selectUserBySn/user 返回的单条结构(编辑回填用) */
|
||||
interface SysUserDevice {
|
||||
id: string
|
||||
realname: string
|
||||
idCard: string
|
||||
height: number
|
||||
sex: number
|
||||
birthday: string
|
||||
}
|
||||
|
||||
/** 表单数据模型 */
|
||||
interface MemberForm {
|
||||
/** 姓名 */
|
||||
@@ -10,80 +23,77 @@ interface MemberForm {
|
||||
age: string
|
||||
/** 身高(cm) */
|
||||
height: string
|
||||
/** 体重(kg) */
|
||||
weight: string
|
||||
}
|
||||
|
||||
/** 身份证解析结果 */
|
||||
interface IdCardParsed {
|
||||
/** 性别,空字符串表示解析失败 */
|
||||
gender: string
|
||||
/** 年龄,空字符串表示解析失败 */
|
||||
age: string
|
||||
sex: number
|
||||
birthday: string
|
||||
}
|
||||
|
||||
/** 已有成员数据项 */
|
||||
interface SelectMemberItem {
|
||||
/** 唯一标识 */
|
||||
id: string
|
||||
/** 姓名 */
|
||||
/** 从抽屉组件 select 事件接收的成员数据 */
|
||||
interface ExistingMember {
|
||||
name: string
|
||||
/** 性别 */
|
||||
gender: '男' | '女'
|
||||
/** 年龄 */
|
||||
age: number
|
||||
/** 身高(cm) */
|
||||
height: number
|
||||
/** 头像 URL,无则显示彩色占位圆 */
|
||||
avatar?: string
|
||||
/** 是否已选中 */
|
||||
selected?: boolean
|
||||
weight: number
|
||||
idCard: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加成员页
|
||||
* 收集姓名、身份证号、身高,并根据身份证自动解析性别与年龄
|
||||
* 保存后返回设备成员列表页
|
||||
* 添加/编辑成员页
|
||||
* URL 参数含 userId 时为编辑模式,隐藏「从已有成员中选择」按钮并预填表单
|
||||
*/
|
||||
Page({
|
||||
data: {
|
||||
// 表单数据
|
||||
/** 编辑模式标记 */
|
||||
isEdit: false,
|
||||
/** 编辑时的用户 ID */
|
||||
userId: '',
|
||||
|
||||
form: {
|
||||
name: '',
|
||||
idCard: '',
|
||||
gender: '',
|
||||
age: '',
|
||||
height: ''
|
||||
height: '',
|
||||
weight: ''
|
||||
} as MemberForm,
|
||||
|
||||
// 抽屉显示状态
|
||||
showSelectDrawer: false,
|
||||
|
||||
// TODO: 后续替换为接口数据
|
||||
existingMembers: [
|
||||
{ id: '1', name: '陈小飞', gender: '男', age: 32, height: 172, avatar: '' },
|
||||
{ id: '2', name: '李镇南', gender: '男', age: 35, height: 185, avatar: '' },
|
||||
{ id: '3', name: '张颖', gender: '女', age: 35, height: 185, avatar: '' },
|
||||
{ id: '4', name: '李镇南', gender: '男', age: 35, height: 185, avatar: '' },
|
||||
{ id: '5', name: '赵雪', gender: '女', age: 31, height: 165, avatar: '' }
|
||||
] as SelectMemberItem[]
|
||||
},
|
||||
|
||||
/**
|
||||
* 姓名输入
|
||||
*/
|
||||
onLoad(options: Record<string, string>) {
|
||||
const { userId } = options
|
||||
if (!userId) return
|
||||
this.setData({ isEdit: true, userId })
|
||||
get<SysUserDevice>('weighingScale/v2/getUserInfo', { id: userId }, { loading: false })
|
||||
.then((res: any) => {
|
||||
const u: SysUserDevice = res.result
|
||||
if (!u) return
|
||||
const parsed = this.parseIdCard(u.idCard ?? '')
|
||||
this.setData({
|
||||
'form.name': u.realname ?? '',
|
||||
'form.idCard': u.idCard ?? '',
|
||||
'form.gender': parsed.gender,
|
||||
'form.age': parsed.age,
|
||||
'form.height': String(u.height ?? ''),
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
onNameInput(e: WechatMiniprogram.Input) {
|
||||
this.setData({
|
||||
'form.name': e.detail.value
|
||||
})
|
||||
this.setData({ 'form.name': e.detail.value })
|
||||
},
|
||||
|
||||
/**
|
||||
* 身份证号输入
|
||||
* 长度满 18 位时自动解析性别与年龄,否则清空
|
||||
*/
|
||||
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,
|
||||
@@ -91,102 +101,69 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 身高输入
|
||||
*/
|
||||
onHeightInput(e: WechatMiniprogram.Input) {
|
||||
this.setData({
|
||||
'form.height': e.detail.value
|
||||
})
|
||||
this.setData({ 'form.height': e.detail.value })
|
||||
},
|
||||
|
||||
onWeightInput(e: WechatMiniprogram.Input) {
|
||||
this.setData({ 'form.weight': e.detail.value })
|
||||
},
|
||||
|
||||
/**
|
||||
* 解析身份证号,返回性别与年龄
|
||||
* @param idCard 18 位身份证号
|
||||
* 解析身份证,返回性别、年龄、sex 数值、出生日期
|
||||
*/
|
||||
parseIdCard(idCard: string): IdCardParsed {
|
||||
// 简单校验:18 位且前 17 位为数字
|
||||
const reg = /^\d{17}[\dX]$/
|
||||
if (!reg.test(idCard)) {
|
||||
return { gender: '', age: '' }
|
||||
}
|
||||
const empty: IdCardParsed = { gender: '', age: '', sex: 0, birthday: '' }
|
||||
if (!/^\d{17}[\dX]$/.test(idCard)) return empty
|
||||
|
||||
// 性别:第 17 位奇数为男,偶数为女
|
||||
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 位
|
||||
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
|
||||
|
||||
// 基本合法性校验
|
||||
if (
|
||||
!year || !month || !day ||
|
||||
month < 1 || month > 12 ||
|
||||
day < 1 || day > 31
|
||||
) {
|
||||
return { gender: '', age: '' }
|
||||
}
|
||||
|
||||
// 计算周岁(看是否过了今年生日)
|
||||
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 (now.getMonth() + 1 < month || (now.getMonth() + 1 === month && now.getDate() < day)) age -= 1
|
||||
if (age < 0 || age > 150) return empty
|
||||
|
||||
if (age < 0 || age > 150) {
|
||||
return { gender: '', age: '' }
|
||||
}
|
||||
|
||||
return { gender, age: String(age) }
|
||||
const mm = String(month).padStart(2, '0')
|
||||
const dd = String(day).padStart(2, '0')
|
||||
return { gender, age: String(age), sex, birthday: `${year}-${mm}-${dd}` }
|
||||
},
|
||||
|
||||
/**
|
||||
* 从已有成员中选择 — 打开抽屉
|
||||
*/
|
||||
onTapSelectExisting() {
|
||||
this.setData({ showSelectDrawer: true })
|
||||
},
|
||||
|
||||
/** 关闭选择成员抽屉 */
|
||||
onSelectMemberClose() {
|
||||
this.setData({ showSelectDrawer: false })
|
||||
},
|
||||
|
||||
/**
|
||||
* 选中成员后填充表单
|
||||
* @param e 携带 detail(SelectMemberItem) 的自定义事件
|
||||
*/
|
||||
onSelectMember(e: WechatMiniprogram.CustomEvent<SelectMemberItem>) {
|
||||
const { name, gender, age, height } = e.detail
|
||||
onSelectMember(e: WechatMiniprogram.CustomEvent<ExistingMember>) {
|
||||
const { name, gender, age, height, weight, idCard } = e.detail
|
||||
this.setData({
|
||||
showSelectDrawer: false,
|
||||
'form.name': name,
|
||||
'form.idCard': idCard,
|
||||
'form.gender': gender,
|
||||
'form.age': String(age),
|
||||
'form.height': String(height)
|
||||
'form.height': String(height),
|
||||
'form.weight': String(weight),
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 取消:直接返回上一页
|
||||
*/
|
||||
onCancel() {
|
||||
onCancel() {
|
||||
wx.navigateBack()
|
||||
},
|
||||
|
||||
/**
|
||||
* 保存成员信息
|
||||
* 必填:姓名、身份证号(18 位且解析成功)、身高
|
||||
* 未通过时 toast 提示,通过后返回上一页
|
||||
*/
|
||||
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 weightNum = parseFloat(weight)
|
||||
|
||||
if (!name.trim()) {
|
||||
wx.showToast({ title: '请填写姓名', icon: 'none' })
|
||||
@@ -197,15 +174,55 @@ Page({
|
||||
return
|
||||
}
|
||||
if (!gender || !age) {
|
||||
wx.showToast({ title: '身份证号有误,请检查', icon: 'none' })
|
||||
wx.showToast({ title: '身份证号有误,请检查', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!height || isNaN(heightNum) || heightNum <= 0) {
|
||||
wx.showToast({ title: '请填写身高', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!weight || isNaN(weightNum) || weightNum <= 0) {
|
||||
wx.showToast({ title: '请填写体重', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: 此处后续接入成员保存接口
|
||||
wx.navigateBack()
|
||||
const parsed = this.parseIdCard(idCard)
|
||||
const userInfo = wx.getStorageSync('userInfo')
|
||||
const connectDeviceInfoRaw = wx.getStorageSync('connectDeviceInfo')
|
||||
|
||||
const payload: Record<string, any> = {
|
||||
parentUserId: userInfo?.userId ?? '',
|
||||
scaleDeviceId: lefuService.deviceInfo?.serialNumber ?? '',
|
||||
realname: name.trim(),
|
||||
idCard,
|
||||
sex: parsed.sex,
|
||||
birthday: parsed.birthday,
|
||||
height: heightNum,
|
||||
weight: weightNum,
|
||||
connectDeviceInfo: connectDeviceInfoRaw ? JSON.stringify(connectDeviceInfoRaw) : '',
|
||||
}
|
||||
if (this.data.isEdit) {
|
||||
payload.id = this.data.userId
|
||||
}
|
||||
|
||||
const successMsg = this.data.isEdit ? '保存成功' : '添加成功'
|
||||
const failMsg = this.data.isEdit ? '保存失败,请重试' : '添加失败,请重试'
|
||||
|
||||
put('weighingScale/v3/edit/user', payload)
|
||||
.then(() => {
|
||||
wx.showToast({ title: successMsg, icon: 'success' })
|
||||
setTimeout(() => {
|
||||
// 通知上一页(equipmentMember)刷新成员列表
|
||||
const pages = getCurrentPages()
|
||||
const prevPage = pages[pages.length - 2] as any
|
||||
if (prevPage && prevPage.data?.sn) {
|
||||
prevPage._loadData(prevPage.data.sn)
|
||||
}
|
||||
wx.navigateBack()
|
||||
}, 1500)
|
||||
})
|
||||
.catch(() => {
|
||||
wx.showToast({ title: failMsg, icon: 'none' })
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -98,10 +98,32 @@
|
||||
</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">
|
||||
<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 class="ghost-btn" bind:tap="onTapSelectExisting">从已有成员中选择</view>
|
||||
<!-- 从已有成员中选择(编辑模式隐藏) -->
|
||||
<block wx:if="{{ !isEdit }}">
|
||||
<view class="ghost-btn" bind:tap="onTapSelectExisting">从已有成员中选择</view>
|
||||
</block>
|
||||
</view>
|
||||
|
||||
<!-- 底部双按钮:取消 / 保存成员信息 -->
|
||||
@@ -113,7 +135,6 @@
|
||||
<!-- 选择已有成员抽屉 -->
|
||||
<select-member-drawer
|
||||
show="{{ showSelectDrawer }}"
|
||||
members="{{ existingMembers }}"
|
||||
bind:close="onSelectMemberClose"
|
||||
bind:select="onSelectMember">
|
||||
</select-member-drawer>
|
||||
|
||||
Reference in New Issue
Block a user