首页 onShow 代码
···
const systemInfo = uni.getSystemInfoSync()
console.info('检查更新',systemInfo)
plus.runtime.getProperty(systemInfo.appId, (widgetInfo) => {
console.info('widgetInfo', widgetInfo)
let isUpdate = false
let uniUpgradeCenterResult = {}
IndexService.getNewVersion({
type: 'harmonyosnext',
version: widgetInfo.version
}).then(res => {
console.info('检查更新>>>>>>', res)
// 是否有更新
if(res.data.isUpdate){
isUpdate = true
uniUpgradeCenterResult = {
title: '更新',
contents: '修复了一些已知问题',
url: res.data.url, // 安装包下载地址
platform: 'Harmony', // Array<'Android' | 'iOS' | 'Harmony'>
is_mandatory: true, // 是否强制更新
type: 'wgt', // "native_app" | "wgt"
}
// 是否整包更新
if(res.data.isPackageUpdate){
uniUpgradeCenterResult.type = 'native_app'
uniUpgradeCenterResult.url = `store://appgallery.huawei.com/app/detail?id=${res.data.appId}`
}
}
if(isUpdate){
console.info('更新',uniUpgradeCenterResult)
this.$refs.upgradePopup.show(true, uniUpgradeCenterResult)
}
})
})
···
安装更新代码,参考 升级中心 uni-upgrade-center
installPackage() {
if (this.isWGT) {
this.installing = true;
}
plus.runtime.install(
this.tempFilePath,
{
force: false
},
async (res) => {
this.installing = false;
this.installed = true;
// wgt包,安装后会提示 安装成功,是否重启
if (this.isWGT) {
// 强制更新安装完成重启
if (this.is_mandatory) {
setTimeout(() => {
this.restart();
}, 1000);
}
} else {
const localFilePathRecord = uni.getStorageSync(localFilePathKey);
uni.setStorageSync(localFilePathKey, {
...localFilePathRecord,
installed: true
});
}
},
async (err) => {
// 如果是安装之前的包,安装失败后删除之前的包
if (this.installForBeforeFilePath) {
await this.deleteSavedFile(this.installForBeforeFilePath);
this.installForBeforeFilePath = '';
}
// 安装失败需要重新下载安装包
this.installing = false;
this.installed = false;
uni.showModal({
title: '更新失败,请重新下载',
content: err.message,
showCancel: false
});
}
);
// 非wgt包,安装跳出覆盖安装,此处直接返回上一页
if (!this.isWGT && !this.is_mandatory) {
uni.navigateBack();
}
},
restart() {
this.installed = false;
uni.showModal({
title: '更新完毕',
content: '请手动重启',
showCancel: false,
success(res) {
plus.runtime.quit()
}
})
},
1***@qq.com (作者)
可以了,有用
2025-06-16 10:19