Files
bodyWeight/components/configureDevice_2/configureDevice_2.js
T
17792275749andClaude Opus 4.6 e20638030b [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>
2026-03-10 14:32:47 +08:00

183 lines
6.6 KiB
JavaScript

const app = getApp();
import $ from "../../utils/request";
Component({
data: {
BLUE_STATE: {},
device: null,
connectState: "",
userList: [],
},
// 存储回调函数的引用,以便在 detached 时取消监听
_connectStateWatcher: null,
lifetimes: {
attached() {
console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
this.setData({
BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
connectState: app.globalData.ppScale.device.connectState,
device: app.globalData.ppScale.device.connection
})
this._connectStateWatcher = (newValue, oldValue) => {
console.log(`[Component] connectState 变化: ${oldValue} -> ${newValue}`);
this.setData({
connectState: newValue || ''
});
};
app.watch('ppScale.device.connectState', this._connectStateWatcher);
this.fetchUserList();
},
detached() {
// 4. 在组件销毁时取消监听,防止内存泄漏
if (this._connectStateWatcher) {
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
}
}
},
// 组件的方法
methods: {
// 手动重连设备
connectedDevice() {
app.resetReconnectCount();
this.triggerEvent('connectedDeviceEvent', {
device: this.data.device
});
},
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"
})
})
},
// 删除用户
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"
})
})
}
}
})
},
setUserInfo() {
if (this.data.connectState != this.data.BLUE_STATE.CONNECTSUCCESS && this.data.connectState != this.data.BLUE_STATE.WIFISUCCESS) {
wx.showToast({
title: "设备连接失败,请重试。",
icon: "none"
})
return;
}
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: user.userId,
userName: user.realname,
memberID: "",
age: user.age,
gender: user.sex,
height: user.height,
isAthleteMode: 0,
currentWeight: String(user.weight),
deviceHeaderIndex: index,
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
if (res == 0) {
index++;
syncNext();
} else {
wx.hideLoading();
console.log("dataSyncUserInfo", res);
wx.showToast({
title: "用户信息下发失败,请重试。",
icon: "none"
})
}
})
};
syncNext();
});
},
}
});