[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
+1
View File
@@ -0,0 +1 @@
.idea/
+39
View File
@@ -151,6 +151,42 @@ App({
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
},
// 保活定时器引用
_keepAliveTimer: null,
/**
* 启动保活定时器,每 15 秒发送一次保活指令
* 内部已做去重处理,重复调用安全
*/
startKeepAlive() {
this.stopKeepAlive();
this._keepAliveTimer = setInterval(() => {
if (this.globalData.ppScale.activeProtocol) {
this.globalData.ppScale.activeProtocol.sendKeepAliveCode();
}
}, 15000);
},
/**
* 清除保活定时器
*/
stopKeepAlive() {
if (this._keepAliveTimer) {
clearInterval(this._keepAliveTimer);
this._keepAliveTimer = null;
}
},
/**
* 页面离开时的统一清理:停止保活 + 重置重连计数 + connectState 置空 + 停止蓝牙
*/
cleanupConnection() {
this.stopKeepAlive();
this.resetReconnectCount();
this.setGlobalData('ppScale.device.connectState', null);
this.globalData.ppScale.plugin.Blue.stop();
},
/**
* 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect
* 依赖 Blue.stop() 清空订阅,onShow 时直接调用,无需标志位
@@ -164,8 +200,10 @@ App({
this.setGlobalData('ppScale.device.connectState', res);
if (res == plugin.BLUE_STATE.CONNECTSUCCESS) {
this.globalData.ppScale.reconnect.count = 0;
this.startKeepAlive();
}
if (res == plugin.BLUE_STATE.CONNECTFAILED) {
this.stopKeepAlive();
this.reconnectDevice();
}
wx.hideLoading();
@@ -179,6 +217,7 @@ App({
// 设备自动断开监听
plugin.bus.subscribe('deviceWillDisconnect', (res) => {
console.log("app.js ===> deviceWillDisconnect", res);
this.stopKeepAlive();
this.setGlobalData('ppScale.device.connectState', plugin.BLUE_STATE.CONNECTFAILED);
});
},
@@ -114,6 +114,10 @@ Component({
// 获取设备绑定的用户列表
fetchUserList() {
if (!app.globalData.ppScale.device.mac) {
wx.showToast({ title: '设备 MAC 地址为空,请重新连接', icon: 'none' });
return;
}
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) {
@@ -51,7 +51,7 @@ Component({
otaMessage: '升级初始化中...'
});
// 启动超时保护,3 分钟内未完成则视为失败
// 启动超时保护,30 秒内未完成则视为失败
this._clearOtaTimer();
this._otaTimer = setTimeout(() => {
console.log('OTA 升级超时');
@@ -60,8 +60,8 @@ Component({
// 回调参数:数字(指令状态码)或对象 { progress, isFailed }
activeProtocol.codeOtaUpdate((res, status) => {
console.log('OTA 升级回调:', status);
console.log('OTA 升级回调:', res);
console.log('OTA 升级 status 回调 :', status);
console.log('OTA 升级 res 回调:', res);
// // 已完成(成功或失败)则忽略后续回调
// if (this._otaCompleted) return;
@@ -22,14 +22,17 @@ Component({
title: "正在获取Wi-Fi列表...",
mask: true
});
app.globalData.ppScale.activeProtocol.dataFindSurroundDevice((res) => {
console.log("connectedDevice ===》dataFindSurroundDevice", res);
const activeProtocol = app.globalData.ppScale.activeProtocol;
if (!activeProtocol) {
wx.hideLoading();
this.setData({
wifiList: res
})
})
wx.showToast({ title: '设备未连接,请重试', icon: 'none' });
return;
}
activeProtocol.dataFindSurroundDevice((res) => {
console.log("connectedDevice ===》dataFindSurroundDevice", res);
wx.hideLoading();
this.setData({ wifiList: Array.isArray(res) ? res : [] });
});
},
selectDevice(e) {
+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);
}