The WeChat applet starts to automatically detect version updates, and when a new version is detected, it prompts to update the updateManager

Effect

  • Sometimes the automatic update of the applet is not so fast, and sometimes there is a cache, and the applet needs to be deleted to detect the update, so that it cannot be updated in time, so it is necessary to add an automatic update detection.
  • Effect: When the applet is started, if the user's local area is inconsistent with the latest version of the applet, the following pop-up window will appear, the user clicks OK, and the applet will be updated. The effect is as follows:.
    insert image description here

the code

Add app.jsthe following code:

// app.js
App({
    
    
  onLaunch() {
    
    
    this.globalData.sysinfo = wx.getSystemInfoSync()
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
    
    
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })
    updateManager.onUpdateReady(function () {
    
    
      wx.showModal({
    
    
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
    
    
          if (res.confirm) {
    
    
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
    
    
      // 新的版本下载失败
      wx.showModal({
    
    
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      })
    })
  },
  globalData: {
    
    
    sysinfo: {
    
    },
  }
})

WeChat applet version update api

  • wx.getUpdateManager()Obtain a globally unique version update manager for managing applet updates
  • UpdateManager.onCheckForUpdateListen to the WeChat background request to check the update result event
  • UpdateManager.onUpdateReadyThe listening applet has version update events. The client actively triggers the download (no developer trigger is required), and the callback is called after the download is successful
  • UpdateManager.applyUpdate()Force the applet to restart and use the new version. Called after the new version of the applet is downloaded (that is, the onUpdateReady callback is received).
  • UpdateManager.onUpdateFailedMonitor applet update failure events. There is a new version of the applet, the client actively triggers the download (no need to be triggered by the developer), and the callback is called after the download fails (maybe due to network reasons, etc.)

Guess you like

Origin blog.csdn.net/qq_23073811/article/details/131509118