async mounted() {
let _this = this
// #ifdef APP-PLUS
let version = '' //线上版本
let insVersion = '' //当前APP安装版本
//请求 获取服务器最新版本号 以及最新文件地址 APK 和wgt文件
const res = await this.$api('getVersion')
version = res.newversion
//获取本地安装的版本号,app环境才有此方法 否则会报错
plus.runtime.getProperty(plus.runtime.appid, (idx)=> {
insVersion = idx.version
if(version != insVersion){
// 线上版本与本地安装版本不一致 唤醒版本安装弹窗
if(res.updateType == 1){
// 整包更新
uni.showModal({
title: '版本更新',
content: '有新的版本发布,检测到当前最新版本为:' + version +
'是否立即进行版本下载?',
confirmText: '立即更新',
cancelText: '稍后进行',
success: (showResult) => {
if (showResult.confirm) {
//创建下载任务
var dtask = plus.downloader.createDownload(res
.downloadurlApk, {
method: 'GET'
},
function(d, status) {
if (status == 200) {
uni.showToast({
title: '版本更新成功',
icon: 'success',
duration: 2000,
mask: true
});
var path = d.filename;
plus.runtime.install(path);
} else {
plus.nativeUI.alert('版本更新失败:' +
status);
}
});
dtask.addEventListener('statechanged', function(task,status){
// 监听下载任务 并计算下载任务进度
_this.progress = parseInt((parseFloat(task.downloadedSize) /parseFloat(task.totalSize)) * 100);
if(_this.progress>=100){
//下载完成 做相应操作
}
});
dtask.start();
}else{
uni.showToast({
title: '正在启动,请稍后!',
icon: 'none',
duration: 2000,
mask: true
});
}
}
})
}else{
// 热更新
uni.showLoading({
title: '正在更新,请稍后!',
mask:true
})
uni.downloadFile({
url: res.downloadurlWget,
success: (downloadResult) => {
if (downloadResult.statusCode === 200) {
uni.hideLoading()
uni.showModal({
title: '',
content: '更新成功,确定现在重启吗?',
confirmText: '重启',
confirmColor: '#EE8F57',
success: function(res) {
if (res.confirm == true) {
plus.runtime.install(
//安装
downloadResult.tempFilePath, {
force: true
},
function(res) {
utils.showToast('已重启');
plus.runtime.restart();
}
);
}
}
});
}else{
uni.hideLoading()
}
}
});
}
}else{
// 当前是最新版本
}
})
// #endif
},
uniapp整包更新和热更新
最新推荐文章于 2024-04-10 10:48:38 发布