[AI Generated]: refactor(*): 保活定时器与连接清理逻辑收口至 app.js,修复 null 安全与注释错误

- 新增 app.startKeepAlive/stopKeepAlive/cleanupConnection,bus 监听自动控制保活与断联清理
- Blue.stop() 收入 cleanupConnection,页面 onHide/onUnload 统一调用
- 修复 replaceNetwork.js seviceList 可能为 undefined 导致崩溃
- 修复 configureDevice_2.js device.mac 为 null 时崩溃
- 修复 replaceNetwork_1.js activeProtocol 无 null 检查及 wifiList 容错
- 修正 configureDevice_4.js 超时注释(3分钟→30秒)
This commit is contained in:
17792275749
2026-04-08 14:56:48 +08:00
parent 5096ceaa53
commit de8fa1fca8
7 changed files with 67 additions and 69 deletions
+3 -31
View File
@@ -49,7 +49,6 @@ Page({
ppScale.device.name = this.data.device.name;
ppScale.device.connection = this.data.device;
wx.hideLoading();
this._startKeepAlive();
if (currentStep === 2) {
const comp = this.selectComponent('#configureDevice3');
if (comp) comp.reInitWifi();
@@ -64,7 +63,6 @@ Page({
ppScale.activeProtocol.codeSyncTime((codeSyncTime) => {
console.log("===》codeSyncTime", codeSyncTime);
wx.hideLoading();
this._startKeepAlive();
ppScale.device.name = this.data.device.name;
ppScale.device.connection = this.data.device;
const nameArr = this.data.device.name.split("-");
@@ -148,8 +146,7 @@ Page({
},
setConfigSuccessful() {
this._stopKeepAlive();
ppScale.plugin.Blue.stop();
app.cleanupConnection();
wx.switchTab({
url: "/pages/home/home"
})
@@ -186,25 +183,6 @@ Page({
}
},
// 启动保活定时器,每10秒发送一次保活指令
_keepAliveTimer: null,
_startKeepAlive() {
this._stopKeepAlive();
this._keepAliveTimer = setInterval(() => {
if (ppScale.activeProtocol) {
ppScale.activeProtocol.sendKeepAliveCode();
}
}, 15000);
},
// 清除保活定时器
_stopKeepAlive() {
if (this._keepAliveTimer) {
clearInterval(this._keepAliveTimer);
this._keepAliveTimer = null;
}
},
checkBluetoothPermissionAndInit() {
wx.getSetting({
success: (res) => {
@@ -263,7 +241,7 @@ Page({
onHide() {
console.log("configureDevice onHide");
this._stopKeepAlive();
app.cleanupConnection();
// 配网步骤:立即清空 WiFi 列表(stop 后数据已失效)
if (this.data.progress.index === 2) {
const comp = this.selectComponent('#configureDevice3');
@@ -271,22 +249,16 @@ Page({
comp.setData({ progress: 0, wifiList: [], selectWifi: null, selectWifiPWD: "" });
}
}
app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null);
this._connectReady = false;
this._macReady = false;
this._stopTime = Date.now();
ppScale.plugin.Blue.stop();
},
onUnload() {
console.log("configureDevice onUnload");
this._stopKeepAlive();
app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null);
app.cleanupConnection();
this._connectReady = false;
this._macReady = false;
this._stopTime = Date.now();
ppScale.plugin.Blue.stop();
}
})
+7 -28
View File
@@ -59,7 +59,6 @@ Page({
const nameArr = this.data.device.name.split("-");
app.globalData.ppScale.version = nameArr.length === 5 ? "domain2" : "domain1";
wx.hideLoading();
this._startKeepAlive();
if (this.data.progress === 0) {
const comp = this.selectComponent('#replaceNetwork1Component');
if (comp) comp.getWiFiList();
@@ -193,6 +192,11 @@ Page({
let seviceList = app.globalData.ppScale.device.setting.find((item) => {
return item.deviceName === res.result[0].type
});
if (!seviceList) {
wx.hideLoading();
wx.showToast({ title: '未找到匹配的设备类型', icon: 'none' });
return;
}
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_) => {
@@ -262,39 +266,14 @@ Page({
}
},
// 启动保活定时器,每10秒发送一次保活指令
_keepAliveTimer: null,
_startKeepAlive() {
this._stopKeepAlive();
this._keepAliveTimer = setInterval(() => {
if (app.globalData.ppScale.activeProtocol) {
app.globalData.ppScale.activeProtocol.sendKeepAliveCode();
}
}, 15000);
},
// 清除保活定时器
_stopKeepAlive() {
if (this._keepAliveTimer) {
clearInterval(this._keepAliveTimer);
this._keepAliveTimer = null;
}
},
onHide() {
console.log("replaceNetwork onHide");
this._stopKeepAlive();
app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop();
app.cleanupConnection();
},
onUnload() {
console.log("replaceNetwork onUnload");
this._stopKeepAlive();
app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop();
app.cleanupConnection();
if (this._connectStateWatcher) {
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
}