检测小程序版本更新提示

时间:2019-10-28
本文章向大家介绍检测小程序版本更新提示,主要包括检测小程序版本更新提示使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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: '新版本下载失败,请删除当前小程序,重新搜索下载~'
            })
          })
        }
      })
        
    }
  }

  

原文地址:https://www.cnblogs.com/cmy1996/p/11751932.html