微信小程序-检查版本更新以及用户提示

384 阅读1分钟

当微信小程序发布新版本以后,旧有用户(之前使用打开过该小程序且是从旧有小程序入口进入未移除过小程序)进入小程序后会提示用户更新新版本,用户选择更新会重新打开新版本小程序,选择取消则使用旧版本

解决方案:

  • 1.检测用户当前小程序版本是否支持微信小程序版本更新管理器方法(wx.canIUse)
  • 2.向微信后台请求检查更新结果(UpdateManager.onCheckForUpdate)
  • 3.判断是否有新版本更新
  • 4.如有新版本更新,客户端主动进行更新,等待更新完成
  • 5.询问用户是否切换使用新版本
  • 6.新版本下载失败处理
  • 7.用户当前版本不支持微信小程序版本更新管理器方法处理(UpdateManager.onUpdateFailed),该步骤可有可无,根据实际情况操作
APP({
     onLaunch(){
         this.updateNewVersions()
     },
     updateNewVersions(){
         let result = wx.canIUse('getUpdateManager')
         if(result){
             const updateManager = wx.getUpdateManager()
             updateManager.onCheckForUpdate(res=>{
               if(res.hasUpdate){
                 updateManager.onUpdateReady(()=>{
                    wx.showModal({
                      title: '更新提示',
                      content: '新版本已经准备好,是否重启应用?',
                      complete: (res) => {
                        wx.showLoading();
                         if (res.confirm) {
                          updateManager.applyUpdate()
                          wx.hideLoading();
                        }
                     }
                  })
               })
            }
       })
      updateManager.onUpdateFailed(res=>{
        wx.showModal({
          title: '更新提示',
          content: '小程序有新版本,请您移除当前小程序,重新搜索打开'
        })
      })
    }else{ //该步骤可有可无,根据实际情况操作
          wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      })
    }
     }
})