/** * 微信小程序版本更新管理 * 调用方式:在 app.ts 的 onLaunch 中调用 checkUpdate() */ export function checkUpdate() { // 获取更新管理器 const updateManager = wx.getUpdateManager() // 监听检查更新结果 updateManager.onCheckForUpdate((res) => { console.log('[版本更新] 是否有新版本:', res.hasUpdate) if (res.hasUpdate) { console.log('[版本更新] 发现新版本,正在后台下载...') } }) // 监听新版本下载成功 updateManager.onUpdateReady(() => { wx.showModal({ title: '版本更新', content: '发现新版本,为了获得更好的体验,建议立即更新', confirmText: '立即更新', cancelText: '稍后再说', success(res) { if (res.confirm) { // 用户确认更新,强制重启应用使用新版本 updateManager.applyUpdate() } } }) }) // 监听新版本下载失败 updateManager.onUpdateFailed(() => { // 下载失败,提示用户下次启动时再尝试 console.warn('[版本更新] 新版本下载失败,请检查网络后重试') }) }