admin管理员组文章数量:1576358
当我们发布了新版本,因为缓存的缘故用户使用的小程序可能还是旧版的,于是我们就有了提醒用户更新版本的需要。小程序提供了UpdateManager对象,用来管理更新,基础库 1.9.90 开始支持,低版本需做兼容处理。
//app.js
App({
onLaunch: function () {
this.checkNewVersion();
},
checkNewVersion: function () { // 检查版本更新
// 获取小程序更新机制兼容
if (wx.canIUse("getUpdateManager")) {
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: "更新提示",
content: "新版本已经准备好,是否重启应用?",
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
}
});
});
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: "已经有新版本了哟~",
content: "新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~"
});
});
}
});
} else {
// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
wx.showModal({
title: "提示",
content: "当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。"
});
}
}
});
参考链接
https://developers.weixin.qq/miniprogram/dev/api/getUpdateManager.html
https://developers.weixin.qq/miniprogram/dev/api/update/wx.getUpdateManager.html
https://developers.weixin.qq/miniprogram/dev/api/update/UpdateManager.html
版权声明:本文标题:微信小程序检查版本更新 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727793027a1130022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论