diff --git a/App.vue b/App.vue index 76dec10..34c821b 100644 --- a/App.vue +++ b/App.vue @@ -23,6 +23,9 @@ export default { console.log('H5刷新,强制断开MQTT连接') }) // #endif + // #ifdef mp-weixin + this.checkMiniProgramUpdate(); + // #endif this.initApp() mqttUtil.disconnectMqtt() @@ -184,6 +187,52 @@ export default { }) } }) + }, + + + // 小程序版本更新检测+MQTT清理 + checkMiniProgramUpdate() { + // 第二步:再判断API是否存在(兜底,避免低版本微信不支持) + if (!uni.canIUse('getUpdateManager')) { + console.log('当前微信版本过低,不支持版本更新检测'); + return; + } + if (!uni.canIUse('getUpdateManager')) return; + + const updateManager = uni.getUpdateManager(); + // 检测到有新版本 + updateManager.onCheckForUpdate((res) => { + if (res.hasUpdate) { + console.log('检测到小程序新版本,准备清理MQTT'); + } + }); + + // 新版本下载完成,准备重启前:先清理MQTT + updateManager.onUpdateReady(() => { + // 第一步:取消MQTT订阅+断开连接 + mqttUtil.disconnectMqtt() + + // 第二步:提示用户重启 + uni.showModal({ + title: '版本更新', + content: '新版本已准备好,重启后即可体验', + success: (res) => { + if (res.confirm) { + // 重启小程序加载新版本 + updateManager.applyUpdate(); + } + } + }); + }); + + // 新版本下载失败,兜底清理 + updateManager.onUpdateFailed(() => { + mqttUtil.disconnectMqtt() + uni.showToast({ + title: '更新失败,已重置连接', + icon: 'none' + }); + }); } } }