有新版本提示升级
This commit is contained in:
@@ -123,32 +123,49 @@ App({
|
|||||||
},
|
},
|
||||||
|
|
||||||
checkUpdate() {
|
checkUpdate() {
|
||||||
|
// 老基础库(< 1.9.90)没有 getUpdateManager,先挡一层:
|
||||||
|
// 否则下面直接 .onCheckForUpdate 会抛 TypeError
|
||||||
|
if (!wx.canIUse('getUpdateManager')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 获取小程序更新管理器实例
|
// 获取小程序更新管理器实例
|
||||||
const updateManager = wx.getUpdateManager();
|
const updateManager = wx.getUpdateManager();
|
||||||
|
|
||||||
// 监听检查更新事件,当小程序有新版本时会触发此回调
|
// 监听检查更新事件,当小程序有新版本时会触发此回调
|
||||||
updateManager.onCheckForUpdate(function (res) {
|
updateManager.onCheckForUpdate(function (res) {
|
||||||
// 请求完新版本信息的回调
|
// 请求完新版本信息的回调
|
||||||
console.log(res.hasUpdate); // 打印是否有新版本
|
console.log('[App] 是否有新版本:', res.hasUpdate); // 打印是否有新版本
|
||||||
});
|
});
|
||||||
|
|
||||||
// 监听更新准备就绪事件,当新版本下载完成时会触发此回调
|
// 监听更新准备就绪事件,当新版本下载完成时会触发此回调
|
||||||
updateManager.onUpdateReady(function () {
|
updateManager.onUpdateReady(function () {
|
||||||
wx.showModal({
|
wx.showModal({
|
||||||
title: '更新提示',
|
title: '更新提示',
|
||||||
content: '新版本已经准备好,可以重启应用小程序了!',
|
content: '新版本已经准备好,是否重启应用?',
|
||||||
|
confirmText: '立即重启',
|
||||||
|
cancelText: '稍后',
|
||||||
success(res) {
|
success(res) {
|
||||||
if (res.confirm) {
|
if (res.confirm) {
|
||||||
// 用户确认更新,调用 applyUpdate 应用新版本并重启小程序
|
// 用户确认更新,调用 applyUpdate 应用新版本并重启小程序
|
||||||
updateManager.applyUpdate();
|
updateManager.applyUpdate();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
// 用户选了「稍后」:新版本此时不会生效,要等下次冷启动才自动应用。
|
||||||
|
// 这里必须说清楚,否则用户以为已经升级完了
|
||||||
|
wx.showToast({
|
||||||
|
title: '下次打开将自动更新',
|
||||||
|
icon: 'none'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// 监听更新失败事件,当新版本下载失败时会触发此回调
|
// 监听更新失败事件,当新版本下载失败时会触发此回调
|
||||||
updateManager.onUpdateFailed(function () {
|
updateManager.onUpdateFailed(function () {
|
||||||
// 新版本下载失败,可以在这里处理失败逻辑,例如提示用户稍后再试
|
// 下载失败没有可交给用户的动作(微信会在下次启动时重试),
|
||||||
|
// 只留日志便于线上排查,不打断用户
|
||||||
|
console.warn('[App] 新版本下载失败');
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user