【小程序】版本发布自动更新机制

如果两次提交的版本号一样, 小程序会不会去更新到最新的版本呢?
其实,这并不影响更新机制。你自己的版本号只是备注,审核后微信会有个数字版本号进行对比。
根据微信提供的能力,小程序的更新流程大致如下:
在这里插入图片描述

配置:
调试基础库,设置在2.17.0以上。

方法:
在app.js加上这两个方法,并在onLaunch调用

  onLaunch(options) {
    
    
    this.autoUpdate()
  },
  // 版本更新
  autoUpdate() {
    
    
    var _this = this;
    // 获取小程序更新机制兼容
    if(!wx.canIUse('getUpdateManager')) {
    
    
      return
    }
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
    
    
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
    
    
        _this.onUpdateReady()
      }
    })
  },
  onUpdateReady() {
    
    
    var _this = this;
    wx.showLoading()
    updateManager.onUpdateReady(function () {
    
    
      wx.showModal({
    
    
        title: '更新提示',
        content: '有版本更新,是否重启小程序?',
        success(res) {
    
    
          wx.hideLoading()
          if (res.confirm) {
    
    
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          } else if(res.cancel) {
    
    
            wx.showModal({
    
    
              title: '提示',
              content: '本次版本有新的功能添加,请及时更新',
              showCancel:false,//隐藏取消按钮
              confirmText:"更新",//只保留确定更新按钮
              success: function(res) {
    
    
                if (res.confirm) {
    
    
                  //下载新版本,并重新应用
                  updateManager.applyUpdate()
                }
              }
            })
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
    
    
      // 新版本下载失败
    })
  }

模拟更新:
体验版无法测试更新机制。但是,开发者工具的编译模式提供模拟更新:
在这里插入图片描述

想了解更多,可以查看:
1、微信开发社区的这篇文章
2、官方文档

猜你喜欢

转载自blog.csdn.net/LuviaWu/article/details/124186811