diff --git a/yelaymaths/app.js b/yelaymaths/app.js index 8d1d4b9..cc6170c 100644 --- a/yelaymaths/app.js +++ b/yelaymaths/app.js @@ -10,6 +10,42 @@ App({ onLaunch() { this.globalData.ready = this.bootstrap(); + this.watchUpdates(); + }, + + /** + * 版本更新监听。**只在这里注册一次**。 + * + * 以前写在首页的 onShow 里,每切回首页就注册一遍;微信这个接口不去重, + * 回调会一层层叠加,同一次更新会连着弹好几个弹窗。放在 App 里天然只执行一次。 + * + * 提示文案来自后台配置,所以要等启动流程把配置读回来再弹,避免弹出空白内容。 + */ + watchUpdates() { + const updateManager = wx.getUpdateManager(); + + updateManager.onUpdateReady(() => { + void this.ready().then(() => { + wx.showModal({ + title: store.cfg('app.update.title'), + showCancel: false, + content: store.cfg('app.update.content'), + success: (res) => { + if (res.confirm) updateManager.applyUpdate(); + }, + }); + }); + }); + + updateManager.onUpdateFailed(() => { + void this.ready().then(() => { + wx.showModal({ + title: store.cfg('app.update.title'), + showCancel: false, + content: store.cfg('app.update.failed'), + }); + }); + }); }, /** diff --git a/yelaymaths/pages/index/index.js b/yelaymaths/pages/index/index.js index e233dde..261df9e 100644 --- a/yelaymaths/pages/index/index.js +++ b/yelaymaths/pages/index/index.js @@ -4,7 +4,6 @@ const store = require('../../utils/store'); const config = require('../../config/index'); const app = getApp(); -const updateManager = wx.getUpdateManager(); const PAGE_SIZE = 10; @@ -107,21 +106,6 @@ Page({ .catch((err) => console.error('[index] 文章加载失败', err)); }, - onShow() { - updateManager.onCheckForUpdate(() => {}); - updateManager.onUpdateReady(() => { - wx.showModal({ - title: store.cfg('app.update.title'), - showCancel: false, - content: store.cfg('app.update.content'), - success: (res) => { - if (res.confirm) updateManager.applyUpdate(); - }, - }); - }); - updateManager.onUpdateFailed(() => {}); - }, - onPullDownRefresh() { Promise.all([this.reload()]).then(() => wx.stopPullDownRefresh()); },