[AI Generated]: refactor(*): 重构用户初始化流程,接口化用户管理并支持增量下发设备

- 重构 configureDevice 步骤0:新增双条件控制(deviceInfo + codeSyncTime),确保连接完成且 mac 就绪后才进入下一步
- 重构 configureDevice_2:移除 userInfo 缓存读取,改为接口获取用户列表,支持动态渲染、删除用户
- 新增 configureDevice_2_addUser 组件:支持工号查询用户信息并添加到设备
- 优化用户下发逻辑:先通过 dataFetchUserID 获取设备端已有用户,仅下发新增用户
- 删除废弃页面 deviceInfo、userInfo

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-03-10 14:32:47 +08:00
co-authored by Claude Opus 4.6
parent 1e3f1934b1
commit e20638030b
27 changed files with 775 additions and 1646 deletions
+109 -225
View File
@@ -6,31 +6,7 @@ Component({
BLUE_STATE: {},
device: null,
connectState: "",
userId: "",
realname: "",
sex: {
index: "",
data: [{
id: 1,
name: '男'
}, {
id: 2,
name: '女'
}]
},
birthday: {
label: "1980年01月01日",
value: "1980-01-01"
},
height: {
index: 50,
data: Array.from({ length: 200 - 120 + 1 }, (_, i) => i + 120)
},
weight: {
index: [60, 0],
data: [Array.from({ length: 120 + 1 }, (_, i) => i + 10), Array.from({ length: 10 }, (_, i) => i)]
},
userList: [],
},
// 存储回调函数的引用,以便在 detached 时取消监听
@@ -52,31 +28,7 @@ Component({
};
app.watch('ppScale.device.connectState', this._connectStateWatcher);
let userInfo = wx.getStorageSync("userInfo") || null;
if(userInfo) {
let [year, month, day] = userInfo.birthday.split("-");
this.setData({
userId: userInfo.userId,
realname: userInfo.realname,
["sex.index"]: this.data.sex.data.findIndex(item => {return item.id === userInfo.sex}),
birthday: {
label: year + "年" + month + "月" + day + "日",
value: userInfo.birthday
},
["height.index"]: this.data.height.data.findIndex(item => {return item === userInfo.height}),
})
if(userInfo.weight) {
const integerPart = Math.floor(userInfo.weight);
const decimalPart = Math.round((userInfo.weight - integerPart) * 10);
const integerIndex = this.data.weight.data[0].indexOf(integerPart);
const decimalIndex = this.data.weight.data[1].indexOf(decimalPart);
this.setData({
["weight.index"]: [integerIndex !== -1 ? integerIndex : null, decimalIndex !== -1 ? decimalIndex : null],
})
}
}
this.fetchUserList();
},
detached() {
// 4. 在组件销毁时取消监听,防止内存泄漏
@@ -96,204 +48,136 @@ Component({
});
},
// 姓名输入
realnameInput(e) {
let realname = e.detail.value.replace(/\s+/g, '');
this.setData({
realname: realname
addUser() {
this.selectComponent("#configureDevice2AddUser").showModal();
},
getAddUserInfo() {
this.fetchUserList();
},
// 获取设备绑定的用户列表
fetchUserList() {
let sn = app.globalData.ppScale.device.mac.replace(/:/g, '');
$.ajax("weighingScale/v2/select/user", { sn }, "GET", true, "正在获取用户列表...").then(res => {
if (res.result && res.result.length) {
this.setData({ userList: res.result });
} else {
this.setData({ userList: [] });
}
}).catch(() => {
wx.showToast({
title: "用户列表获取失败",
icon: "none"
})
})
},
// 性别选择
sexChange(e) {
let sex = e.detail.value;
console.log(sex);
this.setData({
['sex.index']: e.detail.value
})
if(this.data.height.index === null) {
this.setData({
['height.index']: sex == 0 ? 50 : 40
})
}
if(this.data.weight.index === null) {
this.setData({
['weight.index']: sex == 0 ? 20 : 10
})
}
},
// 生日选择
birthdayChange(e) {
let v = e.detail.value;
let [year, month, day] = v.split("-");
this.setData({
birthday: {
label: year + "年" + month + "月" + day + "日",
value: v
// 删除用户
delUserClick(e) {
let id = e.currentTarget.dataset.id;
wx.showModal({
title: "提示",
content: "确定要删除该用户吗?",
success: (res) => {
if (res.confirm) {
$.ajax("weighingScale/v2/del/user", { id }, "DELETE", true, "正在删除...").then(() => {
wx.showToast({
title: "删除成功",
icon: "success"
})
this.fetchUserList();
}).catch(() => {
wx.showToast({
title: "删除失败,请重试",
icon: "none"
})
})
}
}
})
},
// 身高选择
heightChange(e) {
this.setData({
['height.index']: e.detail.value
})
},
// 体重选择
weightChange(e) {
console.log(e.detail.value)
this.setData({
['weight.index']: e.detail.value
})
},
setUserInfo() {
// app.globalData.ppScale.activeProtocol.codeClearDeviceData("00", (res) => {
// console.log("deviceInfo === codeClearDeviceData", res);
// })
if (this.data.connectState != this.data.BLUE_STATE.CONNECTSUCCESS && this.data.connectState != this.data.BLUE_STATE.WIFISUCCESS) {
wx.showToast({
title: "设备连接失败,请重试。",
icon: "none"
})
return;
}
if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS || this.data.connectState == this.data.BLUE_STATE.WIFISUCCESS) {
if(this.data.userId) {
let userList = this.data.userList;
if (!userList.length) {
wx.showToast({
title: "暂无用户信息,请先添加用户",
icon: "none"
})
return;
}
// 先获取设备端已有用户,只下发新用户
wx.showLoading({ title: "正在下发用户信息...", mask: true });
app.globalData.ppScale.activeProtocol.dataFetchUserID((deviceUserIds) => {
console.log("设备端已有用户ID列表", deviceUserIds);
let existIds = Array.isArray(deviceUserIds) ? deviceUserIds : [];
let needSyncList = userList.filter(user => !existIds.includes(user.userId));
if (!needSyncList.length) {
wx.hideLoading();
wx.showToast({
title: "用户信息已同步",
icon: "success"
})
setTimeout(() => {
this.triggerEvent('deviceEvent', { status: true });
}, 1500)
return;
}
let index = 0;
const syncNext = () => {
if (index >= needSyncList.length) {
wx.hideLoading();
wx.showToast({
title: "用户信息下发成功",
icon: "success"
})
setTimeout(() => {
this.triggerEvent('deviceEvent', { status: true });
}, 1500)
return;
}
let user = needSyncList[index];
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: this.data.userId,
userName: this.data.realname,
userID: user.userId,
userName: user.realname,
memberID: "",
age: this.getAge(this.data.birthday.value),
gender: this.data.sex.data[this.data.sex.index].id,
height: this.data.height.data[this.data.height.index],
age: user.age,
gender: user.sex,
height: user.height,
isAthleteMode: 0,
currentWeight: this.data.weight.data[0][this.data.weight.index[0]] + '.' + this.data.weight.data[1][this.data.weight.index[1]],
deviceHeaderIndex: 0,
currentWeight: String(user.weight),
deviceHeaderIndex: index,
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
if(res == 0) {
this.triggerEvent('deviceEvent', {
status: true
});
if (res == 0) {
index++;
syncNext();
} else {
console.log("dataSyncUserInfo", res)
wx.hideLoading();
console.log("dataSyncUserInfo", res);
wx.showToast({
title: "用户信息下发失败,请重试。",
icon: "none"
})
}
})
} else {
let realname = this.data.realname;
let sex = this.data.sex;
let birthday = this.data.birthday;
let height = this.data.height;
let weight = this.data.weight;
if(!realname) {
wx.showToast({
icon: "none",
title: "姓名不能为空"
})
return false;
}
if(sex.index === "") {
wx.showToast({
icon: "none",
title: "性别不能为空"
})
return false;
}
if(!birthday.value) {
wx.showToast({
icon: "none",
title: "生日不能为空"
})
return false;
}
if(height.index === "") {
wx.showToast({
icon: "none",
title: "身高不能为空"
})
return false;
}
if(weight.index[0] === null && weight.index[1] === null) {
console.log(weight)
wx.showToast({
icon: "none",
title: "体重不能为空"
})
return false;
}
let sn = app.globalData.ppScale.device.mac.replace(/:/g, '');
$.ajax("weighingScale/edit/user", {
sn: sn,
realname: realname,
sex: sex.data[sex.index].id,
birthday: birthday.value,
height: height.data[height.index],
weight: weight.data[0][weight.index[0]] + '.' + weight.data[1][weight.index[1]]
}, "POST", true, "保存中...").then(res => {
$.ajax("weighingScale/select/user", {
sn: sn
}, "GET", true, "更新用户信息中...").then(res_ => {
wx.setStorageSync("userInfo", res_.result);
wx.showToast({
icon: "none",
title: res.message
})
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: res_.result.userId,
userName: res_.result.realname,
memberID: "",
age: this.getAge(res_.result.birthday), //
gender: res_.result.sex, //
height: res_.result.height, //
isAthleteMode: 0,
currentWeight: res_.result.weight, //
deviceHeaderIndex: 0,
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
if(res == 0) {
this.triggerEvent('deviceEvent', {
status: true
});
} else {
console.log("dataSyncUserInfo", res)
wx.showToast({
title: "用户信息下发失败,请重试。",
icon: "none"
})
}
})
})
})
}
} else {
wx.showToast({
title: "设备连接失败,请重试。",
icon: "none"
})
}
},
// 根据生日获取年龄
getAge(birthDateString) {
const birthDate = new Date(birthDateString);
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
const dayDiff = today.getDate() - birthDate.getDate();
// 如果当前月份小于出生月份,或者同月但当前日期小于出生日期,年龄需要减 1
if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
age--;
}
return age;
};
syncNext();
});
},
}
});
@@ -1,4 +1,6 @@
{
"component": true,
"usingComponents": {}
"usingComponents": {
"configureDevice_2_addUser": "/components/configureDevice_2_addUser/configureDevice_2_addUser"
}
}
@@ -33,111 +33,38 @@
</view>
<view class="section">
<view class="from">
<view>
<view>姓名</view>
<block wx:if="{{userId}}">
<view class="valueClass">{{realname}}</view>
</block>
<block wx:else>
<view>
<view>
<input
type="text"
value="{{realname}}"
bindinput="realnameInput"
placeholder="请输入您的姓名"
class="valueClass"
placeholder-class="placeholderClass" />
</view>
</view>
</block>
</view>
<view>
<view>性别</view>
<block wx:if="{{userId}}">
<view class="valueClass">{{sex.data[sex.index].name}}</view>
</block>
<block wx:else>
<view class="~arrowAfter">
<view>
<picker mode="selector" bindchange="sexChange" value="{{sex.index}}" range="{{sex.data}}" range-key="name">
<block wx:if="{{sex.index === ''}}">
<view class="placeholderClass">请选择您的性别</view>
</block>
<block wx:else>
<view class="valueClass">{{sex.data[sex.index].name}}</view>
</block>
</picker>
</view>
</view>
</block>
</view>
<view>
<view>生日</view>
<block wx:if="{{userId}}">
<view class="valueClass">{{birthday.label}}</view>
</block>
<block wx:else>
<view class="~arrowAfter">
<view>
<picker mode="date" bindchange="birthdayChange" value="{{birthday.value}}">
<block wx:if="{{birthday.value === ''}}">
<view class="placeholderClass">请选择您的生日</view>
</block>
<block wx:else>
<view class="valueClass">{{birthday.label}}</view>
</block>
</picker>
</view>
</view>
</block>
</view>
<view>
<view>身高</view>
<block wx:if="{{userId}}">
<view class="valueClass">{{height.data[height.index]}}cm</view>
</block>
<block wx:else>
<view class="~arrowAfter">
<view>
<picker mode="selector" bindchange="heightChange" value="{{height.index}}" range="{{height.data}}">
<block wx:if="{{height.index === ''}}">
<view class="placeholderClass">请选择您的身高</view>
</block>
<block wx:else>
<view class="valueClass">{{height.data[height.index]}}</view>
</block>
</picker>
</view>
<view>cm</view>
</view>
</block>
</view>
<view>
<view>体重</view>
<block wx:if="{{userId}}">
<view class="valueClass">{{weight.data[0][weight.index[0]]}}.{{weight.data[1][weight.index[1]]}}kg</view>
</block>
<block wx:else>
<view class="~arrowAfter">
<view>
<picker mode="multiSelector" bindchange="weightChange" value="{{weight.index}}" range="{{weight.data}}">
<block wx:if="{{weight.index && weight.index.length && weight.index[0] !== null && weight.index[1] !== null}}">
<view class="valueClass">{{weight.data[0][weight.index[0]]}}.{{weight.data[1][weight.index[1]]}}</view>
</block>
<block wx:else>
<view class="placeholderClass">请选择您的体重</view>
</block>
</picker>
</view>
<view>kg</view>
</view>
</block>
</view>
<scroll-view class="~scrollView" scroll-y>
<view class="scrollViewContent">
<block wx:for="{{userList}}" wx:key="userId">
<view class="userList">
<view class="userAvatar"></view>
<view class="userInfo">
<view class="userInfo_1">
<view>{{item.realname}}</view>
<view class="{{item.sex === 1 ? 'man' : 'woman'}}">
<view></view>
<view>{{item.age}}</view>
</view>
</view>
<view class="userInfo_2">
<view>身高:{{item.height}}cm</view>
<view></view>
<view>体重:{{item.weight}}kg</view>
</view>
</view>
<view class="delUser" bind:tap="delUserClick" data-id="{{item.id}}">-</view>
</view>
</block>
</view>
</scroll-view>
</view>
<view class="btn">
<view bind:tap="addUser">添加用户</view>
<view bind:tap="setUserInfo">下一步</view>
</view>
<view class="describe">请认真填写和完善您家人的健康信息,用于计算身体数据及运动卡路里消耗等,以便准确的分析数据。<text>(填写数据同时,踩亮蓝牙秤并与其保持连接)。</text></view>
<view class="describe">
请认真填写和完善员工健康信息,用于计算身体数据及运动卡路里消耗等,以便准确的分析数据。<text>(填写数据同时,踩亮蓝牙称并与其保持连接)。</text></view>
</view>
</view>
</view>
<configureDevice_2_addUser id="configureDevice2AddUser" bind:returnDate="getAddUserInfo" />
@@ -24,6 +24,8 @@ input {
.configureDevice2 {
width: 100%;
height: 100%;
display: flex;
flex-flow: column;
}
.header {
@@ -78,72 +80,168 @@ input {
}
.section {
flex: 1;
overflow: hidden;
width: 100%;
padding: 0 30rpx;
box-sizing: border-box;
display: flex;
flex-flow: column;
}
.from {
width: 100%;
margin-bottom: 66rpx;
}
.from>view {
width: 100%;
padding: 48rpx 10rpx 0;
box-sizing: border-box;
border-bottom: 1rpx solid #EAECF1;
}
.from>view>view:nth-of-type(1) {
color: #252535;
height: 32rpx;
font-size: 32rpx;
font-weight: bold;
line-height: 32rpx;
margin-bottom: 18rpx;
}
.from>view>view:nth-of-type(2) {
width: 100%;
height: 58rpx;
display: flex;
flex-wrap: nowrap;
padding-right: 26rpx;
box-sizing: border-box;
}
.from>view>view:nth-of-type(2)>view:nth-of-type(1) {
padding: 24rpx 0;
flex: 1;
width: 0;
overflow: hidden;
}
.from>view>view:nth-of-type(2)>view:nth-of-type(2) {
color: #77849E;
height: 58rpx;
font-size: 28rpx;
font-weight: bold;
line-height: 58rpx;
.scrollViewContent {
width: 100%;
padding: 0 30rpx;
box-sizing: border-box;
}
.userList {
width: 100%;
height: 162rpx;
border-radius: 16px;
padding: 0 30rpx;
display: flex;
flex-wrap: nowrap;
align-items: center;
box-sizing: border-box;
margin-bottom: 24rpx;
border: 1rpx solid #E6E6EA;
}
.userAvatar {
width: 90rpx;
height: 90rpx;
border-radius: 50%;
margin-right: 24rpx;
border: 2rpx solid #FF8FB5;
}
.userInfo {
flex: 1;
}
.userInfo_1 {
width: 100%;
height: 32rpx;
display: flex;
flex-wrap: nowrap;
align-items: center;
margin-bottom: 20rpx;
}
.userInfo_1>view:nth-of-type(1) {
color: #333333;
font-size: 30rpx;
height: 32rpx;
font-weight: bold;
line-height: 32rpx;
margin-right: 20rpx;
}
.userInfo_1>view:nth-of-type(2) {
height: 32rpx;
display: flex;
flex-wrap: nowrap;
align-items: center;
padding: 0 8rpx;
border-radius: 8rpx;
}
.userInfo_1 .man {
background: linear-gradient(90deg, #54D9FC 0%, #6FC1FB 100%);
}
.userInfo_1 .woman {
background: linear-gradient(90deg, #FF91B6 0%, #FD6C9D 100%);
}
.userInfo_1>view:nth-of-type(2)>view:nth-of-type(1) {
width: 20rpx;
height: 20rpx;
margin-right: 4rpx;
}
.userInfo_1>view:nth-of-type(2)>view:nth-of-type(2) {
color: #FFFFFF;
height: 32rpx;
font-weight: bold;
font-size: 22rpx;
line-height: 32rpx;
}
.userInfo_2 {
width: 100%;
height: 26rpx;
display: flex;
align-items: center;
flex-wrap: nowrap;
}
.userInfo_2>view {
color: #77849E;
height: 26rpx;
font-size: 26rpx;
line-height: 26rpx;
}
.userInfo_2>view:nth-of-type(2) {
margin: 0 20rpx;
width: 4rpx;
height: 24rpx;
border-radius: 1rpx;
background-color: #EAECF1;
}
.delUser {
color: #FFFFFF;
width: 40rpx;
height: 40rpx;
font-size: 36rpx;
font-weight: bold;
line-height: 34rpx;
text-align: center;
border-radius: 50%;
background-color: #F24439;
}
.btn {
width: 100%;
padding: 0 26rpx;
padding: 0 55rpx;
box-sizing: border-box;
}
.btn>view {
.btn>view:nth-of-type(1) {
color: #1385FA;
width: 100%;
height: 88rpx;
font-size: 32rpx;
font-weight: bold;
text-align: center;
line-height: 86rpx;
border-radius: 44rpx;
margin-bottom: 40rpx;
box-sizing: border-box;
border: 1px solid #1385FA;
}
.btn>view:nth-of-type(2) {
color: #FFFFFF;
width: 100%;
height: 88rpx;
font-size: 32rpx;
font-weight: bold;
text-align: center;
line-height: 88rpx;
line-height: 86rpx;
border-radius: 44rpx;
margin-bottom: 40rpx;
box-sizing: border-box;
background-color: #1385FA;
border: 1px solid #1385FA;
}
.describe {
@@ -151,6 +249,8 @@ input {
width: 100%;
font-size: 24rpx;
line-height: 40rpx;
padding: 0 30rpx 20rpx;
box-sizing: border-box;
}
.describe text {