// 在App.vue或者一个全局的js文件中
onLaunch: function() {
const updateApp = () => {
uni.request({
url: 'https://your-api.com/check-version', // 替换为你的后端接口地址
success: (res) => {
const { version } = res.data;
const currentVersion = plus.runtime.version;
if (version > currentVersion) { // 比较版本号
// 提示用户有新版本,是否更新
uni.showModal({
title: '发现新版本',
content: '检查到新版本,是否现在更新?',
success: (modalRes) => {
if (modalRes.confirm) {
plus.runtime.install(
// 新版本下载地址,从后端接口获取
'https://your-api.com/download-url',
{
force: true
},
function(error) {
if (error) {
// 安装新版本失败处理
uni.showModal({
content: '安装新版本失败',
showCancel: false
});
} else {
// 重启应用
plus.runtime.restart();
}
}
);
}
}
});
}
}
});
};
// 首次启动或者一段时间后检查更新
updateApp();
// 可以设置定时器定期检查更新
// setInterval(updateApp, 24 * 60 * 60 * 1000); // 每天检查一次
}
05-29
4661
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)