134 lines
3.9 KiB
JavaScript
134 lines
3.9 KiB
JavaScript
/**
|
||
* 小程序自动更新管理器
|
||
* 基于微信官方UpdateManager API实现
|
||
* 文档:https://developers.weixin.qq.com/miniprogram/dev/api/base/update/UpdateManager.html
|
||
*/
|
||
|
||
let updateRetryCount = 0; // 更新重试次数
|
||
const MAX_RETRY_COUNT = 3; // 最大重试次数
|
||
|
||
export default () => {
|
||
// 检查是否支持UpdateManager
|
||
if (!wx.canIUse('getUpdateManager')) {
|
||
console.warn('当前微信版本过低,无法使用 UpdateManager');
|
||
return;
|
||
}
|
||
|
||
const updateManager = wx.getUpdateManager();
|
||
|
||
// 监听检查更新结果
|
||
updateManager.onCheckForUpdate(function (res) {
|
||
console.log('检查更新结果:', res);
|
||
|
||
if (res.hasUpdate) {
|
||
console.log('发现新版本,开始下载...');
|
||
// 显示下载提示
|
||
wx.showToast({
|
||
title: '发现新版本,正在下载...',
|
||
icon: 'loading',
|
||
duration: 2000,
|
||
mask: true
|
||
});
|
||
} else {
|
||
console.log('当前已是最新版本');
|
||
}
|
||
});
|
||
|
||
// 监听新版本下载完成
|
||
updateManager.onUpdateReady(function () {
|
||
console.log('新版本下载完成,准备更新');
|
||
|
||
wx.hideToast(); // 隐藏下载提示
|
||
|
||
wx.showModal({
|
||
title: '更新提示',
|
||
content: '新版本已准备就绪,立即重启应用以获得更好的体验?',
|
||
confirmText: '立即更新',
|
||
cancelText: '稍后更新',
|
||
confirmColor: '#576B95',
|
||
success(res) {
|
||
if (res.confirm) {
|
||
console.log('用户确认更新,开始应用新版本');
|
||
// 显示更新中提示
|
||
wx.showLoading({
|
||
title: '更新中...',
|
||
mask: true
|
||
});
|
||
|
||
try {
|
||
// 应用新版本并重启
|
||
updateManager.applyUpdate();
|
||
} catch (error) {
|
||
console.error('应用更新失败:', error);
|
||
wx.hideLoading();
|
||
wx.showToast({
|
||
title: '更新失败,请稍后重试',
|
||
icon: 'error'
|
||
});
|
||
}
|
||
} else {
|
||
console.log('用户选择稍后更新');
|
||
// 可以在这里设置定时提醒或其他逻辑
|
||
setTimeout(() => {
|
||
wx.showToast({
|
||
title: '建议及时更新以获得最佳体验',
|
||
icon: 'none',
|
||
duration: 3000
|
||
});
|
||
}, 30000); // 30秒后提醒
|
||
}
|
||
},
|
||
fail(error) {
|
||
console.error('显示更新弹窗失败:', error);
|
||
}
|
||
});
|
||
});
|
||
|
||
// 监听新版本下载失败
|
||
updateManager.onUpdateFailed(function (error) {
|
||
console.error('新版本下载失败:', error);
|
||
updateRetryCount++;
|
||
|
||
wx.hideToast(); // 隐藏下载提示
|
||
|
||
if (updateRetryCount < MAX_RETRY_COUNT) {
|
||
console.log(`第${updateRetryCount}次重试下载更新`);
|
||
|
||
wx.showModal({
|
||
title: '下载失败',
|
||
content: `新版本下载失败,是否重试?(${updateRetryCount}/${MAX_RETRY_COUNT})`,
|
||
confirmText: '重试',
|
||
cancelText: '取消',
|
||
success(res) {
|
||
if (res.confirm) {
|
||
// 重新检查更新
|
||
setTimeout(() => {
|
||
console.log('重新检查更新...');
|
||
// 这里可以添加重新检查更新的逻辑
|
||
// 注意:微信会自动检查更新,开发者无需主动触发
|
||
}, 2000);
|
||
} else {
|
||
console.log('用户取消重试');
|
||
updateRetryCount = 0; // 重置重试次数
|
||
}
|
||
}
|
||
});
|
||
} else {
|
||
console.error('更新重试次数已达上限');
|
||
updateRetryCount = 0; // 重置重试次数
|
||
|
||
wx.showModal({
|
||
title: '更新失败',
|
||
content: '新版本下载多次失败,请检查网络连接或稍后再试',
|
||
showCancel: false,
|
||
confirmText: '知道了',
|
||
success() {
|
||
console.log('用户确认更新失败提示');
|
||
}
|
||
});
|
||
}
|
||
});
|
||
|
||
console.log('UpdateManager 初始化完成');
|
||
};
|