112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
function checkReleasedAppVersion() {
|
|
var _this = this;
|
|
uni.request({
|
|
url: this.baseUrl + 'system/app/version', //请求接口
|
|
success: result => {
|
|
console.log('服务器版本:' + result.data);
|
|
// #ifdef APP-PLUS
|
|
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
|
|
console.log(`服务器版本:${result.data},当前版本:${inf.version}`);
|
|
_this.appVersion = inf.version;
|
|
if (inf.version != result.data) {
|
|
var doit = false
|
|
uni.showModal({
|
|
title: "发现新版本",
|
|
content: "发现新版本" + result.data + ' 请点击确定立即更新',
|
|
showCancel: false,
|
|
success: (res) => {
|
|
if (res.confirm == true) { //当用户确定更新,执行更新
|
|
let dtask = plus.downloader.createDownload(
|
|
'http://27.191.152.25:9104/released.apk', {},
|
|
(d, status) => {
|
|
// console.log(d, status)
|
|
// 下载完成
|
|
if (status == 200) {
|
|
plus.runtime.install(plus.io
|
|
.convertLocalFileSystemURL(
|
|
d.filename
|
|
), {}, {}, (
|
|
error) => {
|
|
uni.showToast({
|
|
title: '安装失败',
|
|
mask: false,
|
|
duration: 1500
|
|
});
|
|
})
|
|
} else {
|
|
uni.showToast({
|
|
title: '更新失败',
|
|
mask: false,
|
|
duration: 1500
|
|
});
|
|
}
|
|
});
|
|
try {
|
|
dtask.start(); // 开启下载的任务
|
|
var prg = 0;
|
|
var showLoading = plus.nativeUI
|
|
.showWaiting(
|
|
"正在下载"); //创建一个showWaiting对象
|
|
dtask.addEventListener('statechanged',
|
|
function(
|
|
task,
|
|
status
|
|
) {
|
|
// 给下载任务设置一个监听 并根据状态 做操作
|
|
switch (task.state) {
|
|
case 1:
|
|
showLoading.setTitle(
|
|
"正在下载");
|
|
break;
|
|
case 2:
|
|
showLoading.setTitle(
|
|
"已连接到服务器");
|
|
break;
|
|
case 3:
|
|
prg = parseInt(
|
|
(parseFloat(
|
|
task
|
|
.downloadedSize
|
|
) /
|
|
parseFloat(
|
|
task
|
|
.totalSize
|
|
)) *
|
|
100
|
|
);
|
|
showLoading.setTitle(
|
|
" 正在下载" +
|
|
prg + "% ");
|
|
break;
|
|
case 4:
|
|
plus.nativeUI
|
|
.closeWaiting();
|
|
plus.runtime.quit();
|
|
//下载完成
|
|
break;
|
|
}
|
|
});
|
|
} catch (err) {
|
|
plus.nativeUI.closeWaiting();
|
|
uni.showToast({
|
|
title: '更新失败-03',
|
|
mask: false,
|
|
duration: 1500
|
|
});
|
|
}
|
|
|
|
} else {
|
|
plus.runtime.quit();
|
|
}
|
|
}
|
|
})
|
|
}
|
|
});
|
|
// #endif
|
|
},
|
|
})
|
|
}
|
|
export {
|
|
checkReleasedAppVersion
|
|
}
|