Files
bodyWeight/components/configureDevice_4/configureDevice_4.js
T

88 lines
2.6 KiB
JavaScript

const app = getApp();
Component({
data: {
otaProgress: 0,
otaStatus: '', // 'upgrading' | 'success' | 'failed' | ''
otaMessage: ''
},
lifetimes: {
attached() {
// 组件挂载后自动触发 OTA 升级
this.startOtaUpgrade();
}
},
methods: {
// 完成按钮回调(成功或失败后展示)
bindOk() {
this.triggerEvent('deviceEvent', { status: true });
},
// 开始 OTA 升级
startOtaUpgrade() {
const activeProtocol = app.globalData.ppScale.activeProtocol;
if (!activeProtocol) {
wx.showToast({ title: '设备未连接,请重新连接', icon: 'none' });
return;
}
wx.showLoading({ title: '正在升级...', mask: true });
this.setData({
otaProgress: 0,
otaStatus: 'upgrading',
otaMessage: '升级初始化中...'
});
// 回调参数为 { progress, isFailed }
activeProtocol.codeOtaUpdate((res) => {
console.log('OTA 升级回调:', res);
if (res && res.progress !== undefined) {
this.setData({
otaProgress: res.progress,
otaMessage: `升级进度: ${res.progress}%`
});
}
if (res && res.isFailed === false) {
this.handleOtaSuccess();
} else if (res && res.isFailed === true) {
this.handleOtaFailed();
}
});
},
// OTA 升级成功处理
handleOtaSuccess() {
wx.hideLoading();
this.setData({
otaProgress: 100,
otaStatus: 'success',
otaMessage: '升级成功,设备即将重启'
});
wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 });
setTimeout(() => {
this.triggerEvent('deviceEvent', { status: true });
}, 2000);
},
// OTA 升级失败处理
handleOtaFailed() {
wx.hideLoading();
this.setData({
otaProgress: 0,
otaStatus: 'failed',
otaMessage: '升级失败,设备即将重启'
});
wx.showToast({ title: '升级失败,设备即将重启', icon: 'none', duration: 2000 });
setTimeout(() => {
this.triggerEvent('deviceEvent', { status: true });
}, 2000);
}
}
});