微信小程序自动更新

微信小程序自动监测更新小程序版本

在 app.js 中增加一个 onLaunch 生命周期函数

// app.js
App({
  async onLaunch() {
    this.checkUpdateVersion(); // 检查版本更新
  },
  // 全局变量
  globalData: {
 
  },
 
 
  /**
   * @description 检查微信版本,及时更新
   */
  checkUpdateVersion() {
    if (wx.canIUse("getUpdateManager")) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            updateManager.applyUpdate();
          });
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: "已经有新版本喽~",
              content:
                "请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开哦~",
            });
          });
        }
      });
    } else {
      //TODO 此时微信版本太低(一般而言版本都是支持的)
      wx.showModal({
        title: "溫馨提示",
        content:
          "当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。",
      });
    }
  },
});

 

 

posted @ 2019-07-29 09:38  大熊丨rapper  阅读(650)  评论(0编辑  收藏  举报