const app = getApp(); Component({ data: {}, methods: { // 开始 OTA 升级 startOtaUpgrade() { const activeProtocol = app.globalData.ppScale.activeProtocol; if (!activeProtocol) { wx.showToast({ title: '设备未连接,请重新连接', icon: 'none' }); return; } wx.showLoading({ title: '正在升级固件,请稍候...', mask: true }); // 重置完成标志位,防止回调重复触发 this._otaCompleted = false; // status: false = 成功,true = 失败 activeProtocol.codeOtaUpdate((status) => { // 延迟 3 秒,让 Loading 多转一会再收口 setTimeout(() => { if (status == 0) { // status false → 升级成功 this._finish(true); } else { // status true → 升级失败 this._finish(false, '固件升级失败'); } }, 3000); }); }, // 统一收口:成功/失败提示后跳转首页 _finish(success, failMsg) { if (this._otaCompleted) return; this._otaCompleted = true; wx.hideLoading(); if (success) { wx.showToast({ title: '固件升级成功', icon: 'success', duration: 2000 }); } else { wx.showToast({ title: failMsg || '固件升级失败', icon: 'none', duration: 2000 }); } setTimeout(() => { app.cleanupConnection(); wx.switchTab({ url: '/pages/home/home' }); }, 2000); } } });