首先调用版本更新的接口传入当前版本好,判断是否需要版本更新,版本需要更新使用plus.downloader.createDownload进行下载,下载完成后使用plus.runtime.install进行安装
updateVersion(data).then(res => {
if (res.statusCode == 200) {
console.log(res.data.Data);
this.updateUrl = res.data.Data
if (res.data.Data != '无新版本!') {
uni.showModal({
title: "发现新版本",
content: "是否下载新版本",
success: (res) => {
if (res.confirm) {
// this.doUpData()
//开启下载任务
uni.showToast({
icon: 'success',
mask: true,
duration: 1000,
title: "开始下载了",
})
// #ifdef APP-PLUS
var dtask = plus.downloader.createDownload(
this.updateUrl,
// {
// filename:'_download/apk/'
// },
{
method: "GET"
}, (d, status) => {
console.log(d);
if (status == 200) {
console.log("下载成功安装: " + d.filename);
plus.runtime.install(d.filename)
} else {
plus.nativeUI.alert("安装失败,请稍候重试: " + status)
}
});
dtask.start();
// #endif
} else {
uni.showToast({
icon: 'error',
mask: true,
duration: 2000,
title: "继续使用旧版本",
})
}
}
})
}
}
}).catch(err => {
console.log(err);
})