diff --git a/components/configureDevice_4/configureDevice_4.js b/components/configureDevice_4/configureDevice_4.js index b035d54..cb09624 100644 --- a/components/configureDevice_4/configureDevice_4.js +++ b/components/configureDevice_4/configureDevice_4.js @@ -7,32 +7,29 @@ Component({ otaMessage: '' }, - methods: { - bindOk() { - this.triggerEvent('deviceEvent', { - status: true - }); - }, + lifetimes: { + attached() { + // 组件挂载后自动触发 OTA 升级 + this.startOtaUpgrade(); + } + }, - // OTA 升级方法 - updateOta() { - wx.showModal({ - title: '提示', - content: '确定要进行 OTA 升级吗?升级过程中请保持设备与手机的连接,升级可能需要几分钟时间。', - success: (res) => { - if (res.confirm) { - this.startOtaUpgrade(); - } - } - }); + methods: { + // 完成按钮回调(成功或失败后展示) + bindOk() { + this.triggerEvent('deviceEvent', { status: true }); }, // 开始 OTA 升级 startOtaUpgrade() { - wx.showLoading({ - title: '正在升级...', - mask: true - }); + const activeProtocol = app.globalData.ppScale.activeProtocol; + + if (!activeProtocol) { + wx.showToast({ title: '设备未连接,请重新连接', icon: 'none' }); + return; + } + + wx.showLoading({ title: '正在升级...', mask: true }); this.setData({ otaProgress: 0, @@ -40,50 +37,21 @@ Component({ otaMessage: '升级初始化中...' }); - const activeProtocol = app.globalData.ppScale.activeProtocol; + // 回调参数为 { progress, isFailed } + activeProtocol.codeOtaUpdate((res) => { + console.log('OTA 升级回调:', res); - if (!activeProtocol) { - wx.hideLoading(); - wx.showToast({ - title: '设备未连接,请重新连接', - icon: 'none' - }); - return; - } + if (res && res.progress !== undefined) { + this.setData({ + otaProgress: res.progress, + otaMessage: `升级进度: ${res.progress}%` + }); + } - // 调用蓝牙插件的 OTA 升级接口 - activeProtocol.codeOtaUpdate((status) => { - console.log('OTA 升级状态:', status); - - // status 可能是进度百分比或状态码 - if (typeof status === 'object') { - // 处理进度对象 { progress: 0-100, status: 0/1 } - if (status.progress !== undefined) { - this.setData({ - otaProgress: status.progress, - otaMessage: `升级进度: ${status.progress}%` - }); - } - - // 升级完成或失败 - if (status.status === 0) { - this.handleOtaSuccess(); - } else if (status.status === 1) { - this.handleOtaFailed(); - } - } else if (typeof status === 'number') { - // 处理数字状态码 - if (status === 0) { - this.handleOtaSuccess(); - } else if (status === 1) { - this.handleOtaFailed(); - } else { - // 当作进度百分比处理 - this.setData({ - otaProgress: Math.min(status, 100), - otaMessage: `升级进度: ${Math.min(status, 100)}%` - }); - } + if (res && res.isFailed === false) { + this.handleOtaSuccess(); + } else if (res && res.isFailed === true) { + this.handleOtaFailed(); } }); }, @@ -94,22 +62,11 @@ Component({ this.setData({ otaProgress: 100, otaStatus: 'success', - otaMessage: '升级成功' + otaMessage: '升级成功,设备即将重启' }); - - wx.showToast({ - title: 'OTA 升级成功', - icon: 'success', - duration: 2000 - }); - - // 2秒后重置状态 + wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 }); setTimeout(() => { - this.setData({ - otaProgress: 0, - otaStatus: '', - otaMessage: '' - }); + this.triggerEvent('deviceEvent', { status: true }); }, 2000); }, @@ -119,23 +76,12 @@ Component({ this.setData({ otaProgress: 0, otaStatus: 'failed', - otaMessage: '升级失败,请重试' + otaMessage: '升级失败,设备即将重启' }); - - wx.showToast({ - title: '升级失败,请重试', - icon: 'none', - duration: 2000 - }); - - // 2秒后重置状态 + wx.showToast({ title: '升级失败,设备即将重启', icon: 'none', duration: 2000 }); setTimeout(() => { - this.setData({ - otaProgress: 0, - otaStatus: '', - otaMessage: '' - }); + this.triggerEvent('deviceEvent', { status: true }); }, 2000); } } -}); \ No newline at end of file +}); diff --git a/components/configureDevice_4/configureDevice_4.wxml b/components/configureDevice_4/configureDevice_4.wxml index 74150b5..bda9c5e 100644 --- a/components/configureDevice_4/configureDevice_4.wxml +++ b/components/configureDevice_4/configureDevice_4.wxml @@ -17,6 +17,6 @@ {{otaProgress}}% - 完成 - OTA升级 - \ No newline at end of file + + 完成 +