在App.vue文件里面
版本号和下载地址是接口数据,别的都是uni和html5+里面的方法
onLaunch: function() {
// #ifdef APP-PLUS
let _this = this;
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
let platform = uni.getSystemInfoSync().platform == 'ios' ? 'iOS' : 'Android';
_this.onUpdate(platform, widgetInfo)
});
// #endif
}
检查是否需要更新
onUpdate(platform, widgetInfo) {
// 检查更新
let _this = this;
_this.getreq('api/config/package').then((res)=>{
let data = res.data.data;
if (widgetInfo.version != data.vv) {
uni.showModal({
title: data.t,
showCancel: data.f ? false : true,
content: data.c,
confirmText: '更新',
success: function (result) {
// 更新
if (result.confirm) {
let isWgt = data.u.substring(data.u.length - 3);
if (isWgt == 'wgt') {
_this.onDownloadWgt(data.u);
} else {
_this.onDownloadApp(data.u);
}
}
}
});
}
});
}
整包下载和wgt包下载
onDownloadApp(url) {
// app 整包下载
if (uni.getSystemInfoSync().platform == 'ios') {
// ios
plus.runtime.openURL(url);
} else {
// 安卓包
uni.showLoading({
title: '正在更新,大约需要1-5分钟'
});
let dTask = plus.downloader.createDownload(url, {}, function(d, status){
// 下载完成
if(status == 200){
let path = d.filename;
plus.runtime.install(path); // 安装下载的apk文件
}else{
uni.showModal({
title: '提示',
content: '更新失败',
showCancel: false
})
}
uni.hideLoading();
});
// 监听下载进度
dTask.addEventListener('statechanged',(task) => {
if (!dTask) {
return false;
}
let percentVal = 0;
switch (task.state) {
case 1:
console.log('开始下载');
break;
case 2:
console.log('链接到服务器...');
break;
case 3:
percentVal = Math.ceil((task.downloadedSize / task.totalSize) * 100);
_this.percent = percentVal;
break;
case 4:
console.log('监听下载完成');
break;
}
});
// 开始下载
dTask.start();
}
},
onDownloadWgt(url) {
// wgt热更新下载
uni.downloadFile({
url: url,
success: (downloadResult) => {
uni.hideLoading();
if (downloadResult.statusCode === 200) {
plus.runtime.install(downloadResult.tempFilePath, {
force: true
}, function() {
plus.runtime.restart();
}, function(e) {
});
}
}
});
uni.showLoading({
title: '正在更新,大约需要1-5分钟'
});
}