微信小程序 版本更新提醒

226 阅读1分钟
//!版本更新
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // console.log("onCheckForUpdate", res);
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          // 新版本下载完成
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好,重启应用使用新版本',
              showCancel: false,
              confirmColor: "#04303d",
              success: function (res) {
                if (res.confirm) {
                  // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
        }
      })
      // 新版本下载失败
      updateManager.onUpdateFailed(function () {
        console.log("onUpdateFailed 新版本下载失败");
        wx.showModal({
          title: '更新提示',
          content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
          confirmColor: "#04303d",
        })
      })
    }