[AI Generated]: refactor(Comp): 简化 OTA 升级流程,移除进度显示与超时定时器,直接依据 status 回调提示并跳转首页
This commit is contained in:
@@ -1,36 +1,9 @@
|
|||||||
const app = getApp();
|
const app = getApp();
|
||||||
|
|
||||||
Component({
|
Component({
|
||||||
data: {
|
data: {},
|
||||||
otaProgress: 0,
|
|
||||||
otaStatus: '', // 'upgrading' | 'success' | 'failed' | ''
|
|
||||||
otaMessage: ''
|
|
||||||
},
|
|
||||||
|
|
||||||
lifetimes: {
|
|
||||||
attached() {
|
|
||||||
// OTA 升级由父页面在 activeProtocol 就绪后显式调用,此处不自动触发
|
|
||||||
},
|
|
||||||
detached() {
|
|
||||||
// 组件销毁时清除超时定时器,防止泄漏
|
|
||||||
this._clearOtaTimer();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
// 完成按钮回调(成功或失败后展示)
|
|
||||||
bindOk() {
|
|
||||||
this.triggerEvent('deviceEvent', { status: true });
|
|
||||||
},
|
|
||||||
|
|
||||||
// 清除 OTA 超时定时器
|
|
||||||
_clearOtaTimer() {
|
|
||||||
if (this._otaTimer) {
|
|
||||||
clearTimeout(this._otaTimer);
|
|
||||||
this._otaTimer = null;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// 开始 OTA 升级
|
// 开始 OTA 升级
|
||||||
startOtaUpgrade() {
|
startOtaUpgrade() {
|
||||||
const activeProtocol = app.globalData.ppScale.activeProtocol;
|
const activeProtocol = app.globalData.ppScale.activeProtocol;
|
||||||
@@ -40,92 +13,42 @@ Component({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
wx.showLoading({ title: '正在升级...', mask: true });
|
wx.showLoading({ title: '正在升级固件,请稍候...', mask: true });
|
||||||
|
|
||||||
// 重置完成标志位,防止超时与回调重复触发
|
// 重置完成标志位,防止回调重复触发
|
||||||
this._otaCompleted = false;
|
this._otaCompleted = false;
|
||||||
|
|
||||||
this.setData({
|
// status: false = 成功,true = 失败
|
||||||
otaProgress: 0,
|
activeProtocol.codeOtaUpdate((_res, status) => {
|
||||||
otaStatus: 'upgrading',
|
console.log('OTA status 回调:', status);
|
||||||
otaMessage: '升级初始化中...'
|
// 延迟 3 秒,让 Loading 多转一会再收口
|
||||||
});
|
setTimeout(() => {
|
||||||
|
if (status) {
|
||||||
// 启动超时保护,30 秒内未完成则视为失败
|
// status true → 升级失败
|
||||||
this._clearOtaTimer();
|
this._finish(false, '固件升级失败');
|
||||||
this._otaTimer = setTimeout(() => {
|
} else {
|
||||||
console.log('OTA 升级超时');
|
// status false → 升级成功
|
||||||
this.handleOtaFailed('升级超时,设备即将重启');
|
this._finish(true);
|
||||||
}, 30000);
|
}
|
||||||
|
}, 3000);
|
||||||
// 回调参数:数字(指令状态码)或对象 { progress, isFailed }
|
|
||||||
activeProtocol.codeOtaUpdate((res, status) => {
|
|
||||||
console.log('OTA 升级 status 回调 :', status);
|
|
||||||
console.log('OTA 升级 res 回调:', res);
|
|
||||||
|
|
||||||
// // 已完成(成功或失败)则忽略后续回调
|
|
||||||
// if (this._otaCompleted) return;
|
|
||||||
//
|
|
||||||
// // SDK 返回数字时:1 表示指令已下发、设备开始通过 WiFi 升级并重启
|
|
||||||
// if (typeof res === 'number') {
|
|
||||||
// if (res === 1) {
|
|
||||||
// this._clearOtaTimer();
|
|
||||||
// this.handleOtaSuccess();
|
|
||||||
// } else {
|
|
||||||
// this._clearOtaTimer();
|
|
||||||
// this.handleOtaFailed(`OTA 指令失败,错误码:${res}`);
|
|
||||||
// }
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // SDK 返回对象时:{ progress, isFailed }
|
|
||||||
// if (res && res.progress !== undefined) {
|
|
||||||
// this.setData({
|
|
||||||
// otaProgress: res.progress,
|
|
||||||
// otaMessage: `升级进度: ${res.progress}%`
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (res && res.isFailed === false) {
|
|
||||||
// this._clearOtaTimer();
|
|
||||||
// this.handleOtaSuccess();
|
|
||||||
// } else if (res && res.isFailed === true) {
|
|
||||||
// this._clearOtaTimer();
|
|
||||||
// this.handleOtaFailed();
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// OTA 升级成功处理
|
// 统一收口:成功/失败提示后跳转首页
|
||||||
handleOtaSuccess() {
|
_finish(success, failMsg) {
|
||||||
if (this._otaCompleted) return;
|
if (this._otaCompleted) return;
|
||||||
this._otaCompleted = true;
|
this._otaCompleted = true;
|
||||||
wx.hideLoading();
|
wx.hideLoading();
|
||||||
this.setData({
|
|
||||||
otaProgress: 100,
|
|
||||||
otaStatus: 'success',
|
|
||||||
otaMessage: '升级指令已下发,设备正在重启...'
|
|
||||||
});
|
|
||||||
wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 });
|
|
||||||
setTimeout(() => {
|
|
||||||
this.bindOk();
|
|
||||||
}, 2000);
|
|
||||||
},
|
|
||||||
|
|
||||||
// OTA 升级失败处理
|
if (success) {
|
||||||
handleOtaFailed(msg) {
|
wx.showToast({ title: '固件升级成功', icon: 'success', duration: 2000 });
|
||||||
if (this._otaCompleted) return;
|
} else {
|
||||||
this._otaCompleted = true;
|
wx.showToast({ title: failMsg || '固件升级失败', icon: 'none', duration: 2000 });
|
||||||
wx.hideLoading();
|
}
|
||||||
const message = msg || '升级失败,设备即将重启';
|
|
||||||
this.setData({
|
|
||||||
otaProgress: 0,
|
|
||||||
otaStatus: 'failed',
|
|
||||||
otaMessage: message
|
|
||||||
});
|
|
||||||
wx.showToast({ title: message, icon: 'none', duration: 2000 });
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.bindOk();
|
app.cleanupConnection();
|
||||||
|
wx.switchTab({ url: '/pages/home/home' });
|
||||||
}, 2000);
|
}, 2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,4 @@
|
|||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="describe">随时随地同步测量数据</view>
|
<view class="describe">随时随地同步测量数据</view>
|
||||||
|
|
||||||
<!-- OTA 升级进度显示 -->
|
|
||||||
<view wx:if="{{otaStatus}}" class="ota-progress-container">
|
|
||||||
<view class="ota-message">{{otaMessage}}</view>
|
|
||||||
<view class="progress-bar">
|
|
||||||
<view class="progress-fill" style="width: {{otaProgress}}%"></view>
|
|
||||||
</view>
|
|
||||||
<view class="progress-text">{{otaProgress}}%</view>
|
|
||||||
</view>
|
|
||||||
|
|
||||||
<!-- 成功或失败后才展示完成按钮 -->
|
|
||||||
<view wx:if="{{otaStatus === 'success' || otaStatus === 'failed'}}" class="btn" bind:tap="bindOk">完成</view>
|
|
||||||
</view>
|
</view>
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ Page({
|
|||||||
},
|
},
|
||||||
|
|
||||||
getDeviceSettingList() {
|
getDeviceSettingList() {
|
||||||
// app.globalData.ppScale.plugin.Blue.visibleLog(true);
|
app.globalData.ppScale.plugin.Blue.visibleLog(true);
|
||||||
this.setData({
|
this.setData({
|
||||||
initText: "设备搜索中..."
|
initText: "设备搜索中..."
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user