[AI Generated]: refactor(Comp): 重写 OTA 升级逻辑,改为自动触发、修正回调字段为 isFailed 布尔值、成功失败后统一跳回首页

This commit is contained in:
17792275749
2026-04-08 10:22:07 +08:00
parent d2c914df79
commit c86f3c3d5c
2 changed files with 41 additions and 95 deletions
@@ -7,32 +7,29 @@ Component({
otaMessage: '' otaMessage: ''
}, },
methods: { lifetimes: {
bindOk() { attached() {
this.triggerEvent('deviceEvent', { // 组件挂载后自动触发 OTA 升级
status: true this.startOtaUpgrade();
}); }
}, },
// OTA 升级方法 methods: {
updateOta() { // 完成按钮回调(成功或失败后展示)
wx.showModal({ bindOk() {
title: '提示', this.triggerEvent('deviceEvent', { status: true });
content: '确定要进行 OTA 升级吗?升级过程中请保持设备与手机的连接,升级可能需要几分钟时间。',
success: (res) => {
if (res.confirm) {
this.startOtaUpgrade();
}
}
});
}, },
// 开始 OTA 升级 // 开始 OTA 升级
startOtaUpgrade() { startOtaUpgrade() {
wx.showLoading({ const activeProtocol = app.globalData.ppScale.activeProtocol;
title: '正在升级...',
mask: true if (!activeProtocol) {
}); wx.showToast({ title: '设备未连接,请重新连接', icon: 'none' });
return;
}
wx.showLoading({ title: '正在升级...', mask: true });
this.setData({ this.setData({
otaProgress: 0, otaProgress: 0,
@@ -40,50 +37,21 @@ Component({
otaMessage: '升级初始化中...' otaMessage: '升级初始化中...'
}); });
const activeProtocol = app.globalData.ppScale.activeProtocol; // 回调参数为 { progress, isFailed }
activeProtocol.codeOtaUpdate((res) => {
console.log('OTA 升级回调:', res);
if (!activeProtocol) { if (res && res.progress !== undefined) {
wx.hideLoading(); this.setData({
wx.showToast({ otaProgress: res.progress,
title: '设备未连接,请重新连接', otaMessage: `升级进度: ${res.progress}%`
icon: 'none' });
}); }
return;
}
// 调用蓝牙插件的 OTA 升级接口 if (res && res.isFailed === false) {
activeProtocol.codeOtaUpdate((status) => { this.handleOtaSuccess();
console.log('OTA 升级状态:', status); } else if (res && res.isFailed === true) {
this.handleOtaFailed();
// 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)}%`
});
}
} }
}); });
}, },
@@ -94,22 +62,11 @@ Component({
this.setData({ this.setData({
otaProgress: 100, otaProgress: 100,
otaStatus: 'success', otaStatus: 'success',
otaMessage: '升级成功' otaMessage: '升级成功,设备即将重启'
}); });
wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 });
wx.showToast({
title: 'OTA 升级成功',
icon: 'success',
duration: 2000
});
// 2秒后重置状态
setTimeout(() => { setTimeout(() => {
this.setData({ this.triggerEvent('deviceEvent', { status: true });
otaProgress: 0,
otaStatus: '',
otaMessage: ''
});
}, 2000); }, 2000);
}, },
@@ -119,22 +76,11 @@ Component({
this.setData({ this.setData({
otaProgress: 0, otaProgress: 0,
otaStatus: 'failed', otaStatus: 'failed',
otaMessage: '升级失败,请重试' otaMessage: '升级失败,设备即将重启'
}); });
wx.showToast({ title: '升级失败,设备即将重启', icon: 'none', duration: 2000 });
wx.showToast({
title: '升级失败,请重试',
icon: 'none',
duration: 2000
});
// 2秒后重置状态
setTimeout(() => { setTimeout(() => {
this.setData({ this.triggerEvent('deviceEvent', { status: true });
otaProgress: 0,
otaStatus: '',
otaMessage: ''
});
}, 2000); }, 2000);
} }
} }
@@ -17,6 +17,6 @@
<view class="progress-text">{{otaProgress}}%</view> <view class="progress-text">{{otaProgress}}%</view>
</view> </view>
<view class="btn" bind:tap="bindOk">完成</view> <!-- 成功或失败后才展示完成按钮 -->
<view class="ota" bind:tap="updateOta">OTA升级</view> <view wx:if="{{otaStatus === 'success' || otaStatus === 'failed'}}" class="btn" bind:tap="bindOk">完成</view>
</view> </view>