[AI Generated]: feat(Comp): 重写用户同步逻辑支持主子用户、移除callWithTimeout、新增用户上限10人及主用户删除保护

This commit is contained in:
17792275749
2026-04-13 13:21:59 +08:00
parent 4e2e8fb899
commit 4fc829a81f
9 changed files with 118 additions and 264 deletions
+69 -136
View File
@@ -1,9 +1,6 @@
const app = getApp();
import $ from "../../utils/request";
// 超时时间:15 秒
const SDK_TIMEOUT = 15000;
/**
* 根据生日计算年龄
* @param {string} birthday 生日字符串,如 "1990-05-20"
@@ -56,7 +53,6 @@ Component({
if (this._connectStateWatcher) {
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
}
if (this._sdkCancel) { this._sdkCancel(); this._sdkCancel = null; }
}
},
@@ -101,14 +97,16 @@ Component({
},
addUser() {
if (this.data.userList.length >= 5) {
wx.showToast({
title: "超过最大用户数",
if (this.data.userList.length >= 10) {
wx.showToast({
title: "超过最大用户数",
icon: "none"
});
return;
}
this.selectComponent("#configureDevice2AddUser").showModal();
// 列表为空时,第一个用户只能是员工(主用户)
const isFirstUser = this.data.userList.length === 0;
this.selectComponent("#configureDevice2AddUser").showModal(isFirstUser);
},
getAddUserInfo() {
@@ -146,6 +144,17 @@ Component({
// 删除用户
delUserClick(e) {
let id = e.currentTarget.dataset.id;
const userList = this.data.userList;
const target = userList.find(u => u.id === id);
// 主用户(userType=1)有子用户时不允许删除
if (target && target.userType === 1) {
const hasSubUser = userList.some(u => u.userType === 0);
if (hasSubUser) {
wx.showToast({ title: "请先删除所有普通用户", icon: "none" });
return;
}
}
wx.showModal({
title: "提示",
content: "确定要删除该用户吗?",
@@ -188,99 +197,29 @@ Component({
return;
}
// 获取设备端已有用户,与用户列表做双向同步
wx.showLoading({ title: "正在同步用户信息...", mask: true });
this._sdkCancel = app.callWithTimeout(
(done) => app.globalData.ppScale.activeProtocol.dataFetchUserID(done),
SDK_TIMEOUT,
(deviceUserIds) => {
console.log("设备端已有用户ID列表", deviceUserIds);
// 找出主用户,所有用户下发时 userID 统一使用主用户的 userId
const mainUser = userList.find(u => u.userType === 1);
if (!mainUser) {
wx.showToast({ title: "未找到主用户,请先添加员工用户", icon: "none" });
return;
}
const mainUserId = mainUser.userId;
// 回调返回非数组视为获取失败
if (!Array.isArray(deviceUserIds)) {
wx.showLoading({ title: "正在同步用户数据...", mask: true });
// 第一步:清除秤上所有用户信息
app.globalData.ppScale.activeProtocol.codeClearDeviceData("01", (clearRes) => {
console.log("codeClearDeviceData res", clearRes);
if (clearRes !== 0x00) {
wx.hideLoading();
wx.showToast({ title: "获取设备用户列表失败,请重试", icon: "none" });
wx.showToast({ title: "同步用户失败,请重试", icon: "none" });
return;
}
let rawList = deviceUserIds;
// SDK 返回的是 userID 数组,统一转为对象方便后续操作
let deviceIds = rawList.map(item => ({
userID: String(item),
memberID: ""
}));
// 本地列表的 userId 统一转字符串,与 SDK 侧类型对齐
let localIds = userList.map(user => String(user.userId));
// 秤有、列表没有 → 需要从秤删除
let needDeleteList = deviceIds.filter(d => !localIds.includes(d.userID));
// 秤没有、列表有 → 需要下发到秤
let needSyncList = userList.filter(user => !deviceIds.some(d => d.userID === String(user.userId)));
console.log("需删除", needDeleteList, "需下发", needSyncList);
// 无需操作时直接完成
if (!needDeleteList.length && !needSyncList.length) {
wx.hideLoading();
wx.showToast({ title: "用户信息已同步", icon: "success" });
setTimeout(() => {
this._checkWifiAndNext();
}, 1500);
return;
}
// 第一步:逐个下发新用户到秤
// 第二步:全量下发本地用户列表
let syncIndex = 0;
const syncNext = () => {
if (syncIndex >= needSyncList.length) {
// 下发完成,开始删除
if (needDeleteList.length) {
wx.showLoading({ title: "正在删除多余用户...", mask: true });
}
deleteNext();
return;
}
wx.showLoading({ title: "正在下发第" + (syncIndex + 1) + "/" + needSyncList.length + "个用户...", mask: true });
let user = needSyncList[syncIndex];
this._sdkCancel = app.callWithTimeout(
(done) => app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: user.userId,
userName: user.realname,
memberID: "",
age: user.age || '',
gender: user.sex === 2 ? 0 : user.sex,
height: String(user.height || ''),
isAthleteMode: 0,
deviceHeaderIndex: syncIndex,
currentWeight: String(user.weight || ''),
targetWeight: "",
idealWeight: "",
recentData: [],
}, done),
SDK_TIMEOUT,
(res) => {
if (res == 0) {
syncIndex++;
syncNext();
} else {
wx.hideLoading();
console.log("dataSyncUserInfo 失败", res);
wx.showToast({ title: "用户信息下发失败,请重试。", icon: "none" });
}
},
() => {
wx.hideLoading();
wx.showToast({ title: "用户下发超时,请重试", icon: "none" });
}
);
};
// 第二步:逐个删除秤端多余用户
let delIndex = 0;
const deleteNext = () => {
if (delIndex >= needDeleteList.length) {
// 全部完成
if (syncIndex >= userList.length) {
wx.hideLoading();
wx.showToast({ title: "用户信息同步成功", icon: "success" });
setTimeout(() => {
@@ -288,39 +227,36 @@ Component({
}, 1500);
return;
}
wx.showLoading({ title: "正在删除第" + (delIndex + 1) + "/" + needDeleteList.length + "个多余用户...", mask: true });
let item = needDeleteList[delIndex];
this._sdkCancel = app.callWithTimeout(
(done) => app.globalData.ppScale.activeProtocol.dataDeleteUser({
userID: item.userID,
memberID: ""
}, done),
SDK_TIMEOUT,
(status) => {
if (status == 0) {
delIndex++;
deleteNext();
} else {
wx.hideLoading();
console.log("dataDeleteUser 失败", status);
wx.showToast({ title: "删除设备用户失败,请重试。", icon: "none" });
}
},
() => {
const user = userList[syncIndex];
// 主用户 memberID 为空,子用户 memberID 为子用户自己的 userId
const memberID = user.userId === mainUserId ? "" : user.userId;
wx.showLoading({ title: "正在下发第" + (syncIndex + 1) + "/" + userList.length + "个用户...", mask: true });
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: mainUserId,
userName: user.realname,
memberID: memberID,
age: user.age || '',
gender: user.sex === 2 ? 0 : user.sex,
height: user.height || '',
isAthleteMode: 0,
deviceHeaderIndex: syncIndex,
currentWeight: user.weight || '',
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
console.log("dataSyncUserInfo res", res, "user:", user.realname, "userID:", mainUserId, "memberID:", memberID);
if (res == 0) {
syncIndex++;
syncNext();
} else {
wx.hideLoading();
wx.showToast({ title: "删除用户超时,请重试", icon: "none" });
wx.showToast({ title: "用户信息下发失败,请重试", icon: "none" });
}
);
});
};
// 从下发开始执行
syncNext();
},
() => {
wx.hideLoading();
wx.showToast({ title: "获取设备用户超时,请重试", icon: "none" });
}
);
});
},
/**
@@ -330,20 +266,17 @@ Component({
*/
_checkWifiAndNext() {
wx.showLoading({ title: "正在检测配网状态...", mask: true });
this._sdkCancel = app.callWithTimeout(
(done) => app.globalData.ppScale.activeProtocol.codeFetchWifiConfig(done),
SDK_TIMEOUT,
(res) => {
wx.hideLoading();
// 0x01 表示已配网,跳过配网步骤直接进入下一步
const progressIndex = res === 0x01 ? 3 : 2;
this.triggerEvent('deviceEvent', { status: true, progressIndex });
},
() => {
wx.hideLoading();
wx.showToast({ title: "检测配网状态超时,请重试。", icon: "none" });
app.globalData.ppScale.activeProtocol.codeFetchWifiConfig((res) => {
wx.hideLoading();
if (res === undefined || res === null) {
// 获取失败,自动重试
this._checkWifiAndNext();
return;
}
);
// 0x01 表示已配网,跳过配网步骤直接进入下一步
const progressIndex = res === 0x01 ? 3 : 2;
this.triggerEvent('deviceEvent', { status: true, progressIndex });
});
},
}
});