【小程式】自動更新版本
阿新 • • 發佈:2020-11-25
/** * 自動更新 */ autoUpdate: function () { var self = this // 獲取小程式更新機制相容 if (wx.canIUse('getUpdateManager')) { const updateManager = wx.getUpdateManager() //1. 檢查小程式是否有新版本釋出 updateManager.onCheckForUpdate(function (res) { // 請求完新版本資訊的回撥 if (res.hasUpdate) { //檢測到新版本,需要更新,給出提示 wx.showModal({ title: '更新提示', content: '檢測到新版本,是否下載新版本並重啟小程式?', success: function (res) { if (res.confirm) { //2. 使用者確定下載更新小程式,小程式下載及更新靜默進行 self.downLoadAndUpdate(updateManager) } else if (res.cancel) { //使用者點選取消按鈕的處理,如果需要強制更新,則給出二次彈窗,如果不需要,則這裡的程式碼都可以刪掉了 wx.showModal({ title: '溫馨提示~', content: '本次版本更新涉及到新的功能新增,舊版本無法正常訪問的哦~', showCancel: false,//隱藏取消按鈕 confirmText: "確定更新",//只保留確定更新按鈕 success: function (res) { if (res.confirm) { //下載新版本,並重新應用 self.downLoadAndUpdate(updateManager) } } }) } } }) } }) } else { // 如果希望使用者在最新版本的客戶端上體驗您的小程式,可以這樣子提示 wx.showModal({ title: '提示', content: '當前微信版本過低,無法使用該功能,請升級到最新微信版本後重試。' }) } }, /** * 下載小程式新版本並重啟應用 */ downLoadAndUpdate: function (updateManager) { var self = this wx.showLoading(); //靜默下載更新小程式新版本 updateManager.onUpdateReady(function () { wx.hideLoading() //新的版本已經下載好,呼叫 applyUpdate 應用新版本並重啟 updateManager.applyUpdate() }) updateManager.onUpdateFailed(function () { // 新的版本下載失敗 wx.showModal({ title: '已經有新版本了喲~', content: '新版本已經上線啦~,請您刪除當前小程式,重新搜尋開啟喲~', }) }) }