How to update the client after the new version of the WeChat applet is released? Manager wx.getUpdateManager()

After the version of the WeChat applet is updated, enter it from the title bar above, and find that it is still not updated, and there is a cache problem

Solution - wx.getUpdateManager() provided by WeChat

WeChat Mini Program Update Mechanism

1. Synchronous update at startup (two cases)

1) Periodically check for version updates

When WeChat is running, it will periodically check whether the recently used applets are updated. If there is an update, the next time the applet is started, it will be updated synchronously, and the applet will be opened after the latest version is updated, so as to ensure that users can use the latest version of the applet as soon as possible. After the developer releases a new version in the background, it cannot immediately affect all existing network users, but in the worst case, it will cover most users within 24 hours after the release.

2) The user has not used the Mini Program for a long time

When the user has not used the Mini Program for a long time, in order to ensure the real-time performance of the Mini Program version, the version update will be checked synchronously, and the Mini Program will be opened after updating to the latest version.

2. Asynchronous update at startup

Even if no update is found before startup, every time the applet is cold-started, it will asynchronously check whether there is an updated version. If a new version is found, the code package of the new version will be downloaded asynchronously. However, the current startup will still use the old version of the client's local code, that is, the new version of the applet will not be used until the next cold startup.

3. The developer manually triggers the update

In the case of asynchronous update at startup, if the developer wants to update the version immediately, he can use the wx.getUpdateManager API (code below) to process it. When there is a new version, the user will be prompted to restart the applet to update the new version.
 

 checkUpdateVersion() {
    const _that = this;
    //判断微信版本是否 兼容小程序更新机制API的使用
    if (wx.canIUse('getUpdateManager')) {
      //创建 UpdateManager 实例
      const updateManager = wx.getUpdateManager();
      console.log('是否进入模拟更新');
      //检测版本更新
      updateManager.onCheckForUpdate(function (res) {
        console.log('是否获取版本');
        if (res.hasUpdate) { // 请求完新版本信息的回调
          updateManager.onUpdateReady(function () { //监听小程序有版本更新事件
            //新的版本已经下载好,调用 applyUpdate 应用新版本并重启 ( 此处进行了自动更新操作)
            wx.showModal({
              title: '更新提示',
              content: '新版本已准备好,是否重启应用?',
              success: (res) => {
                if (res.confirm) {
                  updateManager.applyUpdate();
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新版本下载失败
            wx.showModal({
              title: '已经有新版本喽~',
              content: '请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开哦~',
              showCancel: false,
            })
          })
        }
      })
    } else {
      //此时微信版本太低(一般而言版本都是支持的)
      wx.showModal({
        title: '溫馨提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
        showCancel: false,
      })
    }
  },

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 () {

// 新版本下载失败

})

Guess you like

Origin blog.csdn.net/weixin_42415158/article/details/127591844