141 lines
4.2 KiB
JavaScript
141 lines
4.2 KiB
JavaScript
const app = getApp();
|
|
|
|
Component({
|
|
data: {
|
|
otaProgress: 0,
|
|
otaStatus: '', // 'upgrading' | 'success' | 'failed' | ''
|
|
otaMessage: ''
|
|
},
|
|
|
|
methods: {
|
|
bindOk() {
|
|
this.triggerEvent('deviceEvent', {
|
|
status: true
|
|
});
|
|
},
|
|
|
|
// OTA 升级方法
|
|
updateOta() {
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '确定要进行 OTA 升级吗?升级过程中请保持设备与手机的连接,升级可能需要几分钟时间。',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
this.startOtaUpgrade();
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
// 开始 OTA 升级
|
|
startOtaUpgrade() {
|
|
wx.showLoading({
|
|
title: '正在升级...',
|
|
mask: true
|
|
});
|
|
|
|
this.setData({
|
|
otaProgress: 0,
|
|
otaStatus: 'upgrading',
|
|
otaMessage: '升级初始化中...'
|
|
});
|
|
|
|
const activeProtocol = app.globalData.ppScale.activeProtocol;
|
|
|
|
if (!activeProtocol) {
|
|
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({
|
|
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)}%`
|
|
});
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
// OTA 升级成功处理
|
|
handleOtaSuccess() {
|
|
wx.hideLoading();
|
|
this.setData({
|
|
otaProgress: 100,
|
|
otaStatus: 'success',
|
|
otaMessage: '升级成功'
|
|
});
|
|
|
|
wx.showToast({
|
|
title: 'OTA 升级成功',
|
|
icon: 'success',
|
|
duration: 2000
|
|
});
|
|
|
|
// 2秒后重置状态
|
|
setTimeout(() => {
|
|
this.setData({
|
|
otaProgress: 0,
|
|
otaStatus: '',
|
|
otaMessage: ''
|
|
});
|
|
}, 2000);
|
|
},
|
|
|
|
// OTA 升级失败处理
|
|
handleOtaFailed() {
|
|
wx.hideLoading();
|
|
this.setData({
|
|
otaProgress: 0,
|
|
otaStatus: 'failed',
|
|
otaMessage: '升级失败,请重试'
|
|
});
|
|
|
|
wx.showToast({
|
|
title: '升级失败,请重试',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
|
|
// 2秒后重置状态
|
|
setTimeout(() => {
|
|
this.setData({
|
|
otaProgress: 0,
|
|
otaStatus: '',
|
|
otaMessage: ''
|
|
});
|
|
}, 2000);
|
|
}
|
|
}
|
|
}); |