檢測小程序版本更新提示


 

const updateManager = wx.getUpdateManager()

updateManager.onCheckForUpdate(function (res) {
  // 請求完新版本信息的回調
  console.log(res.hasUpdate)
})

updateManager.onUpdateReady(function () {
  wx.showModal({
    title: '更新提示',
    content: '新版本已經准備好,是否重啟應用?',
    success(res) {
      if (res.confirm) {
        // 新的版本已經下載好,調用 applyUpdate 應用新版本並重啟
        updateManager.applyUpdate()
      }
    }
  })
})

updateManager.onUpdateFailed(function () {
  // 新版本下載失敗
})

 

async onLaunch() {
    let res = wx.getSystemInfoSync();
      if (!res.version || !compareVersion(res.version, '6.5.13')) {
        wx.showModal({
          title: '提示',
          content: '當前微信版本過低,功能支持不完善,請升級到最新微信版本后重試。'
        })
      }
      this.checkNewVersion()
}

// 檢查版本更新
  checkNewVersion() {
    if(wx.canIUse('getUpdateManager')) { // 用戶版本更新
      let updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate((res) => {
        // 請求完新版本信息的回調
        if(res.hasUpdate) {
          updateManager.onUpdateReady(() => {
            wx.showModal({
              title: '更新提示',
              content: '新版本已經准備好,是否重啟應用?',
              success: res => {
                if(res.confirm) {
                // 新版本已經下載好,調用applyUpdate應用新版本,並且重啟
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(() => { // 新版本下載失敗 需要用戶手動舊版本刪除,下載新版本
            wx.showModal({
              title: '更新失敗',
              content: '新版本下載失敗,請刪除當前小程序,重新搜索下載~'
            })
          })
        }
      })
        
    }
  }

 

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM