先看官方文档,对uni-app 的资源热更新有一个大致的了解:
整包升级
在线资源热更新
app方法
本文主要讲述,实现资源热更新时客户端需要进行的操作:
可以在***app.vue***的***onLaunch***函数(全局监测)中进行监测,也可以在***index.vue***的***onLoad***事件中监测
代码:
onLaunch(){
// #ifdef APP-PLUS
this.checkUpdateApp()
// #endif
}
//methods中定义的方法
async checkUpdateApp() {
let that = this
let data = await that.updateInfoBack() || '' //后台接口返回的热更新资源的版本信息
const platform = uni.getSystemInfoSync().platform
let version = '',
appName = ""
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
version = widgetInfo.version //当前版本信息
appName = widgetInfo.name || ''
if (data && data.apk_client_force_update == '0') {
if (platform == 'android') {
if (version != data.apk_client_version) {
uni.showModal({
title: appName + '更新',
content: data.apk_client_intro ? data.apk_client_intro :
'修复了一些已知问题以及产品优化',
confirmText: '下载更新',
success: (res) => {
if (res.confirm) {
uni.showLoading({
title: '正在检查更新,请稍后',
mask: true
});
uni.downloadFile({
url: data.apk_client_download,
success: (downres) => {
if (downres.statusCode === 200) {
plus.runtime.install(downres
.tempFilePath, {
force: false
}, () => {
uni.hideLoading()
console.log(
'install success...'
);
plus.runtime
.restart();
}, (e) => {
uni.hideLoading()
uni.showToast({
title: '更新失败',
icon: "none",
duration: 2200
})
console.error(e,
'install fail...'
);
});
}
}
})
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}else if(platform=='ios'){
uni.showModal({
title: appName + '更新',
content: data.ios_client_intro ? data.ios_client_intro :
'修复了一些已知问题以及产品优化',
confirmText: '下载更新',
success: (res) => {
if (res.confirm) {
plus.runtime.openURL(data.ios_client_download)
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
}
} else if (data && data.apk_client_force_update == '1') {
let intro = '', appurl = '',appVersion = '', updata = ''
switch (platform) {
case 'android':
intro = data.apk_client_intro
appurl = data.apk_client_download
appVersion = data.apk_client_version
updata = data.apk_client_force_update
break;
case 'ios':
intro = data.ios_client_intro
appurl = data.ios_client_download
appVersion = data.ios_client_version
updata = data.ios_client_force_update
break;
default:
console.log('运行在开发者工具上')
break;
}
if (version/1 < appVersion/1) {
uni.showModal({
title: appName+'更新',
confirmText: '立即升级',
showCancel: updata == 1 ? false : true,
content: intro,
success: async function(res) {
if (res.confirm) {
plus.runtime.openURL(appurl)
}
}
})
}
}
});
},
//返回更新信息
updateInfoBack() {
return new Promise((resolve, reject) => {
api.updateApp({}, res => { //后台返回需热更新资源的接口
if (res.error == '0') {
let data = res.data || null;
resolve(data)
} else {
reject(null)
}
})
})
},
图片说明: