將以下代碼寫入app.js的onLaunch中,可檢測并提示用戶重啟更新版本
//console.log('onLaunch:', options);
// 檢測并獲取小程序更新 api 說明:https://developers.weixin.qq.com/miniprogram/dev/api/getUpdateManager.html
if (wx.canIUse('getUpdateManager')) { // 基礎庫 1.9.90 開始支持,低版本需做兼容處理
? ? const updateManager = wx.getUpdateManager();
? ? updateManager.onCheckForUpdate(function(result) {
? ? ? ? if (result.hasUpdate) { // 有新版本
? ? ? ? ? ? updateManager.onUpdateReady(function() { // 新的版本已經下載好
? ? ? ? ? ? ? ? wx.showModal({
? ? ? ? ? ? ? ? ? ? title: '更新提示',
? ? ? ? ? ? ? ? ? ? content: '新版本已經下載好,請重啟應用。',
? ? ? ? ? ? ? ? ? ? success: function(result) {
? ? ? ? ? ? ? ? ? ? ? ? if (result.confirm) { // 點擊確定,調用 applyUpdate 應用新版本并重啟
? ? ? ? ? ? ? ? ? ? ? ? ? ? updateManager.applyUpdate();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? ? ? updateManager.onUpdateFailed(function() { // 新的版本下載失敗
? ? ? ? ? ? ? ? wx.showModal({
? ? ? ? ? ? ? ? ? ? title: '已經有新版本了喲~',
? ? ? ? ? ? ? ? ? ? content: '新版本已經上線啦~,請您刪除當前小程序,重新搜索打開喲~'
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? }
? ? });
}
else { // 有更新肯定要用戶使用新版本,對不支持的低版本客戶端提示
? ? wx.showModal({
? ? ? ? title: '溫馨提示',
? ? ? ? content: '當前微信版本過低,無法使用該應用,請升級到最新微信版本后重試。'
? ? });
}