From e7c890fee4bbad302b7732821b3f34aa706959cf Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Wed, 8 Apr 2026 17:43:28 +0800 Subject: [PATCH] =?UTF-8?q?[AI=20Generated]:=20refactor(Comp):=20=E7=AE=80?= =?UTF-8?q?=E5=8C=96=20OTA=20=E5=8D=87=E7=BA=A7=E6=B5=81=E7=A8=8B=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=BF=9B=E5=BA=A6=E6=98=BE=E7=A4=BA=E4=B8=8E?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=AE=9A=E6=97=B6=E5=99=A8=EF=BC=8C=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E4=BE=9D=E6=8D=AE=20status=20=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E5=B9=B6=E8=B7=B3=E8=BD=AC=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configureDevice_4/configureDevice_4.js | 129 ++++-------------- .../configureDevice_4/configureDevice_4.wxml | 12 -- pages/searchDevice/searchDevice.js | 2 +- 3 files changed, 27 insertions(+), 116 deletions(-) diff --git a/components/configureDevice_4/configureDevice_4.js b/components/configureDevice_4/configureDevice_4.js index def3714..6be6523 100644 --- a/components/configureDevice_4/configureDevice_4.js +++ b/components/configureDevice_4/configureDevice_4.js @@ -1,36 +1,9 @@ const app = getApp(); Component({ - data: { - otaProgress: 0, - otaStatus: '', // 'upgrading' | 'success' | 'failed' | '' - otaMessage: '' - }, - - lifetimes: { - attached() { - // OTA 升级由父页面在 activeProtocol 就绪后显式调用,此处不自动触发 - }, - detached() { - // 组件销毁时清除超时定时器,防止泄漏 - this._clearOtaTimer(); - } - }, + data: {}, methods: { - // 完成按钮回调(成功或失败后展示) - bindOk() { - this.triggerEvent('deviceEvent', { status: true }); - }, - - // 清除 OTA 超时定时器 - _clearOtaTimer() { - if (this._otaTimer) { - clearTimeout(this._otaTimer); - this._otaTimer = null; - } - }, - // 开始 OTA 升级 startOtaUpgrade() { const activeProtocol = app.globalData.ppScale.activeProtocol; @@ -40,92 +13,42 @@ Component({ return; } - wx.showLoading({ title: '正在升级...', mask: true }); + wx.showLoading({ title: '正在升级固件,请稍候...', mask: true }); - // 重置完成标志位,防止超时与回调重复触发 + // 重置完成标志位,防止回调重复触发 this._otaCompleted = false; - this.setData({ - otaProgress: 0, - otaStatus: 'upgrading', - otaMessage: '升级初始化中...' - }); - - // 启动超时保护,30 秒内未完成则视为失败 - this._clearOtaTimer(); - this._otaTimer = setTimeout(() => { - console.log('OTA 升级超时'); - this.handleOtaFailed('升级超时,设备即将重启'); - }, 30000); - - // 回调参数:数字(指令状态码)或对象 { progress, isFailed } - activeProtocol.codeOtaUpdate((res, status) => { - console.log('OTA 升级 status 回调 :', status); - console.log('OTA 升级 res 回调:', res); - - // // 已完成(成功或失败)则忽略后续回调 - // if (this._otaCompleted) return; - // - // // SDK 返回数字时:1 表示指令已下发、设备开始通过 WiFi 升级并重启 - // if (typeof res === 'number') { - // if (res === 1) { - // this._clearOtaTimer(); - // this.handleOtaSuccess(); - // } else { - // this._clearOtaTimer(); - // this.handleOtaFailed(`OTA 指令失败,错误码:${res}`); - // } - // return; - // } - // - // // SDK 返回对象时:{ progress, isFailed } - // if (res && res.progress !== undefined) { - // this.setData({ - // otaProgress: res.progress, - // otaMessage: `升级进度: ${res.progress}%` - // }); - // } - // - // if (res && res.isFailed === false) { - // this._clearOtaTimer(); - // this.handleOtaSuccess(); - // } else if (res && res.isFailed === true) { - // this._clearOtaTimer(); - // this.handleOtaFailed(); - // } + // status: false = 成功,true = 失败 + activeProtocol.codeOtaUpdate((_res, status) => { + console.log('OTA status 回调:', status); + // 延迟 3 秒,让 Loading 多转一会再收口 + setTimeout(() => { + if (status) { + // status true → 升级失败 + this._finish(false, '固件升级失败'); + } else { + // status false → 升级成功 + this._finish(true); + } + }, 3000); }); }, - // OTA 升级成功处理 - handleOtaSuccess() { + // 统一收口:成功/失败提示后跳转首页 + _finish(success, failMsg) { if (this._otaCompleted) return; this._otaCompleted = true; wx.hideLoading(); - this.setData({ - otaProgress: 100, - otaStatus: 'success', - otaMessage: '升级指令已下发,设备正在重启...' - }); - wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 }); - setTimeout(() => { - this.bindOk(); - }, 2000); - }, - // OTA 升级失败处理 - handleOtaFailed(msg) { - if (this._otaCompleted) return; - this._otaCompleted = true; - wx.hideLoading(); - const message = msg || '升级失败,设备即将重启'; - this.setData({ - otaProgress: 0, - otaStatus: 'failed', - otaMessage: message - }); - wx.showToast({ title: message, icon: 'none', duration: 2000 }); + if (success) { + wx.showToast({ title: '固件升级成功', icon: 'success', duration: 2000 }); + } else { + wx.showToast({ title: failMsg || '固件升级失败', icon: 'none', duration: 2000 }); + } + setTimeout(() => { - this.bindOk(); + app.cleanupConnection(); + wx.switchTab({ url: '/pages/home/home' }); }, 2000); } } diff --git a/components/configureDevice_4/configureDevice_4.wxml b/components/configureDevice_4/configureDevice_4.wxml index bda9c5e..ff27bb8 100644 --- a/components/configureDevice_4/configureDevice_4.wxml +++ b/components/configureDevice_4/configureDevice_4.wxml @@ -7,16 +7,4 @@ 随时随地同步测量数据 - - - - {{otaMessage}} - - - - {{otaProgress}}% - - - - 完成 diff --git a/pages/searchDevice/searchDevice.js b/pages/searchDevice/searchDevice.js index b3bd417..d8dc87b 100644 --- a/pages/searchDevice/searchDevice.js +++ b/pages/searchDevice/searchDevice.js @@ -209,7 +209,7 @@ Page({ }, getDeviceSettingList() { - // app.globalData.ppScale.plugin.Blue.visibleLog(true); + app.globalData.ppScale.plugin.Blue.visibleLog(true); this.setData({ initText: "设备搜索中..." });