wepy微信小程序框架加入版本更新提示
侧边栏壁纸
  • 累计撰写 635 篇文章
  • 累计收到 0 条评论

wepy微信小程序框架加入版本更新提示

加速器之家
2024-08-22 / 0 评论 / 3 阅读 / 正在检测是否收录...

我们经常可以看到一些小程序更新一个版本之后,我们再次打开会收到版本更新提示,这是这么做到的呢?以下以wepy为例,我们可以在:项目\src\app.wpy文件下加入以下更新提示代码:

wepy.app({
    async onLaunch() {
      console.log('热更新')
      if (wx.canIUse("getUpdateManager")) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function(res) {
        console.log("onCheckForUpdate====11111", res);
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
        console.log("res.hasUpdate====");
        }
      });
      updateManager.onUpdateReady(function(res) {
        console.log(111, res);
        wx.showModal({
        title: "版本更新",
        content: "新版本已经准备好,确定重启应用?",
        showCancel: false,
        success: function(res) {
          console.log("success====", res);
          // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
          if (res.confirm) {
          // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate();
          }
        }
        });
      });
      updateManager.onUpdateFailed(function() {
        // 新的版本下载失败
        wx.showModal({
        title: "已经有新版本了哟~",
        content: "新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~"
        });
      });
      }
  },
  methods: {
  },
  globalData: {
  }
})

这样我们若是提交新的版本之后,上线,用不打开就会提示版本更新,需要重新进入。

0

评论

博主关闭了当前页面的评论