diff --git a/miniprogram/pages/home/home.ts b/miniprogram/pages/home/home.ts index b5b5c00..de55de1 100644 --- a/miniprogram/pages/home/home.ts +++ b/miniprogram/pages/home/home.ts @@ -57,6 +57,8 @@ const splitWeight = (weight: number): { int: number; decimal: string } => { } Page({ + _otaChecked: false as boolean, + data: { hasData: false, device: { @@ -72,6 +74,7 @@ Page({ }) lefuService.onDeviceConnect(() => { this.setData({ 'device.connected': true }) + this._checkOta() }) lefuService.onDisconnected(() => { this.setData({ 'device.connected': false }) @@ -137,6 +140,31 @@ Page({ }) }, + /** OTA 固件检测,连接成功后自动调用,有新版本直接升级,整个页面生命周期只触发一次 */ + _checkOta() { + if (this._otaChecked) return + this._otaChecked = true + lefuService.checkAndUpgrade() + .then(needUpdate => { + if (!needUpdate) return + wx.showLoading({ title: '固件升级中...', mask: true }) + lefuService.checkAndUpgrade() + .then(() => { + wx.hideLoading() + wx.showModal({ + title: '升级完成', + content: '固件升级成功,请重新开启设备', + showCancel: false, + }) + }) + .catch((err: Error) => { + wx.hideLoading() + wx.showToast({ title: err.message || '升级失败,请重试', icon: 'none' }) + }) + }) + .catch(() => {}) + }, + /** 跳转历史数据页 */ onTapHistory() { wx.switchTab({ url: '/pages/data/data' })