[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: ''
},
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);
}
}
});
});
@@ -17,6 +17,6 @@
<view class="progress-text">{{otaProgress}}%</view>
</view>
<view class="btn" bind:tap="bindOk">完成</view>
<view class="ota" bind:tap="updateOta">OTA升级</view>
</view>
<!-- 成功或失败后才展示完成按钮 -->
<view wx:if="{{otaStatus === 'success' || otaStatus === 'failed'}}" class="btn" bind:tap="bindOk">完成</view>
</view>