微信小程序 - 小程序版本更新處理

1. 在 app.js 文件中加入方法:

// 校驗更新
hasNewBanben() {
    console.log('校驗更新')
    //判斷微信版本是否 兼容小程序更新機制API的使用
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate((res1) => {
        if (res1.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已經準備好,是否重啓應用?',
              success: function(res) {
                // todo 做一些操作,比如說清除 Storage 緩存等
                updateManager.applyUpdate();
              }
            })
            console.log('正在更新中......')
          })
          updateManager.onUpdateFailed(function () {
          	// todo 做一些操作,比如說清除 Storage 緩存等
            // 新版本下載失敗
            wx.showModal({
              title: '已經有新版本呦~',
              content: '請您刪除當前小程序,到微信 “發現-小程序” 頁,重新搜索打開哦~',
            })
          })
        }
      })
    } else {
      // todo 做一些操作,比如說清除 Storage 緩存等
      // 此時微信版本太低(一般而言版本都是支持的)
      wx.showModal({
        title: '溫馨提示',
        content: '當前微信版本過低,請升級到最新微信版本後重試。'
      })
    }
  }

2. 在 app.js 中的 onLaunch 調用1中方法即可:

this.hasNewBanben() // 校驗小程序版本
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章