uniapp中App升级和wgt热更新的逻辑处理
一、前言
在使用uniapp开发中常常遇到APP升级和wgt热更新需求。这里为了方便后续开发将相关代码整理下以供参考。
注意:
1、由于里面涉及了其它地方的代码所以,实际使用时候需要进行调整
2、由于升级和更新功能是放在App.vue文件中,所以没有使用自定义对话框的功能。使用了uniapp自带的对话框和加载进度条
3、该逻辑是执行完升级功能后再执行热更新功能
二、代码示例
App.vue
onLaunch: async function(options) {
let that = this;
// #ifdef APP-PLUS
that.checkAppUpdate().finally(() => {
that.checkWgtUpdate();
});
// #endif
}
//检查App更新
checkAppUpdate() {
let that = this
return new Promise((resolve, reject) => {
let type = 1 //获取版本号类型 0小程序版本号 1安卓版本号 2IOS版本号 1
if (plus.os.name == "Android") {
type = 1
} else {
type = 2
}
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
let params = {
// version: widgetInfo.version,
// name: widgetInfo.name
appid: that.$appConfig.appid,
type: type,
version: widgetInfo.version
}
that.$api('update.app', params).then(res => {
if (res.code == 0) {
var data = res.data;
if (data.is_update == 0) {
//不需要更新
resolve()
return
}
if (data.app_edition_resources_url) {
uni.showModal({ //提醒用户更新
title: "APP更新提示",
content: data.update_desc,
success: (res) => {
if (res.confirm) {
if (plus.os.name == "Android") {
that.downLoadFileAndInstall(
resolve, reject, data
.app_edition_resources_url)
} else {
plus.runtime.openURL(data
.app_edition_resources_url);
}
}
},
fail:(error) => {
reject()
}
})
}
}
});
});
});
},
//下载并进行安装
downLoadFileAndInstall(resolve, reject,apkFile) {
uni.showLoading({
title: '程序下载中...'
})
uni.downloadFile({
url: apkFile, //仅为示例,并非真实的资源
success: (res) => {
if (res.statusCode === 200) {
uni.hideLoading();
resolve()
plus.runtime.install(plus.io.convertLocalFileSystemURL(res.tempFilePath), {}, {},
function(error) {
uni.showToast({
title: '安装失败',
duration: 1500
});
})
} else {
reject()
uni.hideLoading();
uni.showToast({
title: '更新失败',
duration: 1500
});
}
},
fail: (error) => {
reject()
uni.hideLoading();
uni.showToast({
title: '更新失败',
duration: 1500
});
}
});
},
//检查热更新
checkWgtUpdate() {
let that = this
// #ifdef APP-PLUS
let type = 1 //获取版本号类型 0小程序版本号 1安卓版本号 2IOS版本号 1
if (plus.os.name == "Android") {
type = 1
} else {
type = 2
}
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
let params = {
// version: widgetInfo.version,
// name: widgetInfo.name
appid: that.$appConfig.appid,
type: type,
version: widgetInfo.version
}
that.$api('update.wgt', params).then(res => {
if (res.code == 0) {
var data = res.data;
if (data.is_update == 0) {
//不需要更新
return
}
if (data.app_resources_url) {
that.updateWgtModal(data.app_resources_url)
}
}
});
});
// #endif
},
//确定是否下载更新
updateWgtModal(wgtUrl) {
uni.showModal({
title: "资源更新",
content: "发现新的资源包更新,是否进行下载?",
success: function(res) {
if (res.confirm) {
uni.downloadFile({
url: wgtUrl,
success: (downloadResult) => {
if (downloadResult.statusCode === 200) {
plus.runtime.install(downloadResult.tempFilePath, {
force: false
}, function() {
console.log('install success...');
plus.runtime.restart();
}, function(e) {
console.error('install fail...');
});
}
}
});
} else if (res.cancel) {
console.log('用户点击取消');
}
},
})
}
},
三、参考链接
1、uniapp下载文件api:
https://uniapp.dcloud.io/api/request/network-file?id=downloadfile
2、Promise:
https://es6.ruanyifeng.com/#docs/promise
3、App资源在线升级更新
https://ask.dcloud.net.cn/article/182
4、APP整包升级方案