[AI Generated]: refactor(Comp): 重写 OTA 升级逻辑,改为自动触发、修正回调字段为 isFailed 布尔值、成功失败后统一跳回首页
This commit is contained in:
@@ -7,32 +7,29 @@ Component({
|
|||||||
otaMessage: ''
|
otaMessage: ''
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
lifetimes: {
|
||||||
bindOk() {
|
attached() {
|
||||||
this.triggerEvent('deviceEvent', {
|
// 组件挂载后自动触发 OTA 升级
|
||||||
status: true
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// OTA 升级方法
|
|
||||||
updateOta() {
|
|
||||||
wx.showModal({
|
|
||||||
title: '提示',
|
|
||||||
content: '确定要进行 OTA 升级吗?升级过程中请保持设备与手机的连接,升级可能需要几分钟时间。',
|
|
||||||
success: (res) => {
|
|
||||||
if (res.confirm) {
|
|
||||||
this.startOtaUpgrade();
|
this.startOtaUpgrade();
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
});
|
|
||||||
|
methods: {
|
||||||
|
// 完成按钮回调(成功或失败后展示)
|
||||||
|
bindOk() {
|
||||||
|
this.triggerEvent('deviceEvent', { status: true });
|
||||||
},
|
},
|
||||||
|
|
||||||
// 开始 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,51 +37,22 @@ 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();
|
|
||||||
wx.showToast({
|
|
||||||
title: '设备未连接,请重新连接',
|
|
||||||
icon: 'none'
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 调用蓝牙插件的 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({
|
this.setData({
|
||||||
otaProgress: status.progress,
|
otaProgress: res.progress,
|
||||||
otaMessage: `升级进度: ${status.progress}%`
|
otaMessage: `升级进度: ${res.progress}%`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 升级完成或失败
|
if (res && res.isFailed === false) {
|
||||||
if (status.status === 0) {
|
|
||||||
this.handleOtaSuccess();
|
this.handleOtaSuccess();
|
||||||
} else if (status.status === 1) {
|
} else if (res && res.isFailed === true) {
|
||||||
this.handleOtaFailed();
|
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>
|
||||||
Reference in New Issue
Block a user