diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/app.js b/app.js index 5f9a4d9..a262dda 100644 --- a/app.js +++ b/app.js @@ -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); }); }, diff --git a/components/configureDevice_2/configureDevice_2.js b/components/configureDevice_2/configureDevice_2.js index 44c3d5f..bf5ce78 100644 --- a/components/configureDevice_2/configureDevice_2.js +++ b/components/configureDevice_2/configureDevice_2.js @@ -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) { diff --git a/components/configureDevice_4/configureDevice_4.js b/components/configureDevice_4/configureDevice_4.js index fa634a5..def3714 100644 --- a/components/configureDevice_4/configureDevice_4.js +++ b/components/configureDevice_4/configureDevice_4.js @@ -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; diff --git a/components/replaceNetwork_1/replaceNetwork_1.js b/components/replaceNetwork_1/replaceNetwork_1.js index f6550d1..496631d 100644 --- a/components/replaceNetwork_1/replaceNetwork_1.js +++ b/components/replaceNetwork_1/replaceNetwork_1.js @@ -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) { diff --git a/pages/configureDevice/configureDevice.js b/pages/configureDevice/configureDevice.js index af32245..b3e1cb3 100644 --- a/pages/configureDevice/configureDevice.js +++ b/pages/configureDevice/configureDevice.js @@ -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(); } }) \ No newline at end of file diff --git a/pages/replaceNetwork/replaceNetwork.js b/pages/replaceNetwork/replaceNetwork.js index 88ce7d9..cf0ca7e 100644 --- a/pages/replaceNetwork/replaceNetwork.js +++ b/pages/replaceNetwork/replaceNetwork.js @@ -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); }