[AI Generated]: fix(*): 修复蓝牙权限授权、bus订阅泄漏、OTA重复回调、WiFi列表容错及用户同步字段映射问题
This commit is contained in:
@@ -15,9 +15,25 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
// 存储回调函数的引用,以便在 detached 时取消监听
|
||||
// 存储回调函数的引用,以便在 onUnload 时取消监听
|
||||
_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) {
|
||||
// 一次性数据初始化
|
||||
this.checkBluetoothPermissionAndInit();
|
||||
@@ -43,13 +59,16 @@ Page({
|
||||
// 重新注册 app 级别的全局 bus 订阅
|
||||
app.registerBusListeners();
|
||||
|
||||
// 页面级 bus 订阅
|
||||
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
|
||||
// 页面级 bus 订阅(先清理旧的,防止重复叠加)
|
||||
this._unsubscribePageBus();
|
||||
|
||||
this._pageHandlers.devicesModel = (res) => {
|
||||
console.log("replaceNetwork ===》devicesModel", res);
|
||||
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);
|
||||
|
||||
app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true);
|
||||
@@ -73,7 +92,8 @@ Page({
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", this._pageHandlers.deviceConnect);
|
||||
|
||||
// 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
|
||||
this.checkBluetoothPermissionAndInit();
|
||||
@@ -203,13 +223,13 @@ Page({
|
||||
});
|
||||
app.globalData.ppScale.plugin.Blue.setDeviceSetting([seviceList]);
|
||||
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_);
|
||||
|
||||
|
||||
let fIndex = res_.findIndex(item => item.deviceId === res.result[0].deviceId);
|
||||
if (fIndex >= 0) {
|
||||
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
|
||||
wx.showLoading({
|
||||
wx.showLoading({
|
||||
title: "正在连接设备...",
|
||||
mask: true
|
||||
});
|
||||
@@ -218,7 +238,8 @@ Page({
|
||||
})
|
||||
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]);
|
||||
}
|
||||
});
|
||||
};
|
||||
app.globalData.ppScale.plugin.bus.subscribe("devicesList", this._pageHandlers.devicesList);
|
||||
})
|
||||
},
|
||||
|
||||
@@ -298,6 +319,8 @@ Page({
|
||||
onHide() {
|
||||
console.log("replaceNetwork onHide");
|
||||
this._stopKeepAlive();
|
||||
this._unsubscribePageBus();
|
||||
app.unregisterBusListeners();
|
||||
app.resetReconnectCount();
|
||||
app.setGlobalData('ppScale.device.connectState', null);
|
||||
app.globalData.ppScale.plugin.Blue.stop();
|
||||
@@ -307,6 +330,8 @@ Page({
|
||||
onUnload() {
|
||||
console.log("replaceNetwork onUnload");
|
||||
this._stopKeepAlive();
|
||||
this._unsubscribePageBus();
|
||||
app.unregisterBusListeners();
|
||||
app.resetReconnectCount();
|
||||
app.setGlobalData('ppScale.device.connectState', null);
|
||||
app.globalData.ppScale.plugin.Blue.stop();
|
||||
|
||||
@@ -47,10 +47,11 @@ Page({
|
||||
|
||||
if (scopeToRequest.length > 0) {
|
||||
try {
|
||||
await wx.authorize({
|
||||
scope: scopeToRequest[0]
|
||||
});
|
||||
this.checkBluetoothPermissionAndInit();
|
||||
// 逐个授权所有缺失的权限
|
||||
for (const scope of scopeToRequest) {
|
||||
await wx.authorize({ scope });
|
||||
}
|
||||
this.openBluetoothAdapter();
|
||||
} catch (authErr) {
|
||||
if (this.data.isShowingModal) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user