热更新app

This commit is contained in:
2025-07-07 10:50:00 +08:00
parent 043c95667b
commit 100521ffa3

71
App.vue
View File

@@ -18,6 +18,9 @@ export default {
console.log(openSdk);
this.setGlobalIMlistener();
this.tryLogin();
// #ifdef APP-PLUS
checkUpdate(); // 直接调用独立函数避免this问题
// #endif
// #ifdef H5
console.error(
`暂时不支持运行到 Web如果需要移动端的 Web 项目,参考 [H5 demo](https://github.com/openimsdk/openim-h5-demo)`
@@ -442,6 +445,74 @@ export default {
},
},
};
function checkUpdate() {
const localVersion = plus.runtime.version;
uni.request({
url: 'https://your-server.com/update/version.json',
success: (res) => {
const remoteVersion = res.data.version;
const wgtUrl = res.data.wgtUrl;
if (compareVersion(remoteVersion, localVersion) > 0) {
uni.showModal({
title: '发现新版本',
content: `检测到新版本(${remoteVersion}),是否立即下载并更新?`,
confirmText: '立即更新',
cancelText: '暂不更新',
success: (modalRes) => {
if (modalRes.confirm) {
uni.showLoading({title: '正在下载更新包...'});
uni.downloadFile({
url: wgtUrl,
success: (downloadResult) => {
uni.hideLoading();
if (downloadResult.statusCode === 200) {
plus.runtime.install(downloadResult.tempFilePath, {force: true}, function() {
uni.showModal({
title: '更新完成',
content: '应用需要重启才能生效,是否立即重启?',
success: function (res) {
if (res.confirm) {
plus.runtime.restart();
}
}
});
}, function(e) {
uni.showToast({title: '安装失败: ' + e.message});
});
} else {
uni.showToast({title: '下载失败'});
}
},
fail: () => {
uni.hideLoading();
uni.showToast({title: '下载失败'});
}
});
}
}
});
} else {
uni.showToast({title: '当前已是最新版本'});
}
},
fail: () => {
uni.showToast({title: '检查更新失败'});
}
});
}
function compareVersion(v1, v2) {
v1 = v1.split('.');
v2 = v2.split('.');
for (let i = 0; i < v1.length; ++i) {
const n1 = parseInt(v1[i] || '0');
const n2 = parseInt(v2[i] || '0');
if (n1 > n2) return 1;
if (n1 < n2) return -1;
}
return 0;
}
</script>
<style lang="scss">