[AI Generated]: fix(*): 修复蓝牙权限授权、bus订阅泄漏、OTA重复回调、WiFi列表容错及用户同步字段映射问题

This commit is contained in:
17792275749
2026-04-08 11:29:45 +08:00
parent 3b65d2fb0d
commit 228c68abca
6 changed files with 65 additions and 24 deletions
+2 -2
View File
@@ -33,8 +33,8 @@
"style": "v2", "style": "v2",
"plugins": { "plugins": {
"ppScale-plugin": { "ppScale-plugin": {
"version": "1.2.19", "version": "0.0.22",
"provider": "wx0ffb48417ce6345c" "provider": "wx0826af4e4908f524"
} }
}, },
"permission": { "permission": {
@@ -66,6 +66,10 @@ Component({
content: "确定要初始化设备吗?将清除设备中的所有数据(用户信息、历史数据、配网数据、设置信息)。", content: "确定要初始化设备吗?将清除设备中的所有数据(用户信息、历史数据、配网数据、设置信息)。",
success: (res) => { success: (res) => {
if (res.confirm) { if (res.confirm) {
if (!app.globalData.ppScale.activeProtocol) {
wx.showToast({ title: "设备未连接,请重试", icon: "none" });
return;
}
wx.showLoading({ title: "正在初始化设备...", mask: true }); wx.showLoading({ title: "正在初始化设备...", mask: true });
app.globalData.ppScale.activeProtocol.codeClearDeviceData("00", (status) => { app.globalData.ppScale.activeProtocol.codeClearDeviceData("00", (status) => {
wx.hideLoading(); wx.hideLoading();
@@ -190,18 +194,19 @@ Component({
} }
let rawList = deviceUserIds; let rawList = deviceUserIds;
// 兼容对象数组和纯ID数组,统一转为字符串比 // SDK 返回的是 userID 数组,统一转为对象方便后续操作
let deviceIds = rawList.map(item => ({ let deviceIds = rawList.map(item => ({
id: item, userID: String(item),
memberID: "" memberID: ""
})); }));
let localIds = userList.map(user => user.userId); // 本地列表的 userId 统一转字符串,与 SDK 侧类型对齐
let localIds = userList.map(user => String(user.userId));
// 秤有、列表没有 → 需要从秤删除 // 秤有、列表没有 → 需要从秤删除
let needDeleteList = deviceIds.filter(d => !localIds.includes(d.userId)); let needDeleteList = deviceIds.filter(d => !localIds.includes(d.userID));
// 秤没有、列表有 → 需要下发到秤 // 秤没有、列表有 → 需要下发到秤
let needSyncList = userList.filter(user => !deviceIds.some(d => d.userID == user.userId)); let needSyncList = userList.filter(user => !deviceIds.some(d => d.userID === String(user.userId)));
console.log("需删除", needDeleteList, "需下发", needSyncList); console.log("需删除", needDeleteList, "需下发", needSyncList);
@@ -268,7 +273,7 @@ Component({
wx.showLoading({ title: "正在删除第" + (delIndex + 1) + "/" + needDeleteList.length + "个多余用户...", mask: true }); wx.showLoading({ title: "正在删除第" + (delIndex + 1) + "/" + needDeleteList.length + "个多余用户...", mask: true });
let item = needDeleteList[delIndex]; let item = needDeleteList[delIndex];
app.globalData.ppScale.activeProtocol.dataDeleteUser({ app.globalData.ppScale.activeProtocol.dataDeleteUser({
userID: item.userId, userID: item.userID,
memberID: "" memberID: ""
}, (status) => { }, (status) => {
if (status == 0) { if (status == 0) {
@@ -34,7 +34,7 @@ Component({
} }
activeProtocol.dataFindSurroundDevice((res) => { activeProtocol.dataFindSurroundDevice((res) => {
wx.hideLoading(); wx.hideLoading();
this.setData({ wifiList: res }); this.setData({ wifiList: Array.isArray(res) ? res : [] });
}); });
}, },
@@ -45,6 +45,9 @@ Component({
wx.showLoading({ title: '正在升级...', mask: true }); wx.showLoading({ title: '正在升级...', mask: true });
// 重置完成标志位,防止超时与回调重复触发
this._otaCompleted = false;
this.setData({ this.setData({
otaProgress: 0, otaProgress: 0,
otaStatus: 'upgrading', otaStatus: 'upgrading',
@@ -62,6 +65,9 @@ Component({
activeProtocol.codeOtaUpdate((res) => { activeProtocol.codeOtaUpdate((res) => {
console.log('OTA 升级回调:', res); console.log('OTA 升级回调:', res);
// 已完成(成功或失败)则忽略后续回调
if (this._otaCompleted) return;
if (res && res.progress !== undefined) { if (res && res.progress !== undefined) {
this.setData({ this.setData({
otaProgress: res.progress, otaProgress: res.progress,
@@ -81,6 +87,8 @@ Component({
// OTA 升级成功处理 // OTA 升级成功处理
handleOtaSuccess() { handleOtaSuccess() {
if (this._otaCompleted) return;
this._otaCompleted = true;
wx.hideLoading(); wx.hideLoading();
this.setData({ this.setData({
otaProgress: 100, otaProgress: 100,
@@ -95,6 +103,8 @@ Component({
// OTA 升级失败处理 // OTA 升级失败处理
handleOtaFailed(msg) { handleOtaFailed(msg) {
if (this._otaCompleted) return;
this._otaCompleted = true;
wx.hideLoading(); wx.hideLoading();
const message = msg || '升级失败,设备即将重启'; const message = msg || '升级失败,设备即将重启';
this.setData({ this.setData({
+33 -8
View File
@@ -15,9 +15,25 @@ Page({
} }
}, },
// 存储回调函数的引用,以便在 detached 时取消监听 // 存储回调函数的引用,以便在 onUnload 时取消监听
_connectStateWatcher: null, _connectStateWatcher: null,
// 存储页面级 bus 回调引用,用于精准 unsubscribe
_pageHandlers: {
devicesModel: null,
deviceConnect: null,
devicesList: null,
},
// 注销页面级 bus 订阅
_unsubscribePageBus() {
const bus = app.globalData.ppScale.plugin.bus;
const h = this._pageHandlers;
if (h.devicesModel) { bus.unsubscribe('devicesModel', h.devicesModel); h.devicesModel = null; }
if (h.deviceConnect) { bus.unsubscribe('deviceConnect', h.deviceConnect); h.deviceConnect = null; }
if (h.devicesList) { bus.unsubscribe('devicesList', h.devicesList); h.devicesList = null; }
},
onLoad(options) { onLoad(options) {
// 一次性数据初始化 // 一次性数据初始化
this.checkBluetoothPermissionAndInit(); this.checkBluetoothPermissionAndInit();
@@ -43,13 +59,16 @@ Page({
// 重新注册 app 级别的全局 bus 订阅 // 重新注册 app 级别的全局 bus 订阅
app.registerBusListeners(); app.registerBusListeners();
// 页面级 bus 订阅 // 页面级 bus 订阅(先清理旧的,防止重复叠加)
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => { this._unsubscribePageBus();
this._pageHandlers.devicesModel = (res) => {
console.log("replaceNetwork ===》devicesModel", res); console.log("replaceNetwork ===》devicesModel", res);
app.globalData.ppScale.device.mac = res.deviceMac; app.globalData.ppScale.device.mac = res.deviceMac;
}); };
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", this._pageHandlers.devicesModel);
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => { this._pageHandlers.deviceConnect = (res) => {
console.log('replaceNetwork ===》deviceConnect', res); console.log('replaceNetwork ===》deviceConnect', res);
app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true); app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true);
@@ -73,7 +92,8 @@ Page({
} }
} }
}); });
}); };
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", this._pageHandlers.deviceConnect);
// 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start) // 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
this.checkBluetoothPermissionAndInit(); this.checkBluetoothPermissionAndInit();
@@ -203,7 +223,7 @@ Page({
}); });
app.globalData.ppScale.plugin.Blue.setDeviceSetting([seviceList]); app.globalData.ppScale.plugin.Blue.setDeviceSetting([seviceList]);
app.globalData.ppScale.plugin.Blue.start(res.result[0].type, false); app.globalData.ppScale.plugin.Blue.start(res.result[0].type, false);
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => { this._pageHandlers.devicesList = (res_) => {
console.log("searchDevice ===> devicesList", res_); console.log("searchDevice ===> devicesList", res_);
let fIndex = res_.findIndex(item => item.deviceId === res.result[0].deviceId); let fIndex = res_.findIndex(item => item.deviceId === res.result[0].deviceId);
@@ -218,7 +238,8 @@ Page({
}) })
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]); app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]);
} }
}); };
app.globalData.ppScale.plugin.bus.subscribe("devicesList", this._pageHandlers.devicesList);
}) })
}, },
@@ -298,6 +319,8 @@ Page({
onHide() { onHide() {
console.log("replaceNetwork onHide"); console.log("replaceNetwork onHide");
this._stopKeepAlive(); this._stopKeepAlive();
this._unsubscribePageBus();
app.unregisterBusListeners();
app.resetReconnectCount(); app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null); app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop(); app.globalData.ppScale.plugin.Blue.stop();
@@ -307,6 +330,8 @@ Page({
onUnload() { onUnload() {
console.log("replaceNetwork onUnload"); console.log("replaceNetwork onUnload");
this._stopKeepAlive(); this._stopKeepAlive();
this._unsubscribePageBus();
app.unregisterBusListeners();
app.resetReconnectCount(); app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null); app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop(); app.globalData.ppScale.plugin.Blue.stop();
+5 -4
View File
@@ -47,10 +47,11 @@ Page({
if (scopeToRequest.length > 0) { if (scopeToRequest.length > 0) {
try { try {
await wx.authorize({ // 逐个授权所有缺失的权限
scope: scopeToRequest[0] for (const scope of scopeToRequest) {
}); await wx.authorize({ scope });
this.checkBluetoothPermissionAndInit(); }
this.openBluetoothAdapter();
} catch (authErr) { } catch (authErr) {
if (this.data.isShowingModal) { if (this.data.isShowingModal) {
return; return;