You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.6 KiB
116 lines
2.6 KiB
1 year ago
|
import {
|
||
|
appCheckUpdate
|
||
|
} from '../api/request2.js';
|
||
|
export function appUpdate() {
|
||
|
let curversion = 0;
|
||
|
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
|
||
|
const data = {
|
||
|
action: 'checkVersion',
|
||
|
appid: plus.runtime.appid,
|
||
|
appVersion: plus.runtime.version,
|
||
|
wgtVersion: widgetInfo.version,
|
||
|
versionCode: widgetInfo.versionCode
|
||
|
}
|
||
|
curversion=data.versionCode
|
||
|
appCheckUpdate().then(res => {
|
||
|
console.log("当前版本提示",curversion)
|
||
|
if (res.data) {
|
||
|
if (res.data.versionCode > Number(curversion)) {
|
||
|
var downUrl = res.data.downUrl;
|
||
|
var content = res.data.content;
|
||
|
var version =res.data.version
|
||
|
console.log("新版本提示")
|
||
|
uni.showModal({
|
||
|
title: "发现新版本:("+version+")",
|
||
|
content: content,
|
||
|
confirmText: "更新",
|
||
|
cancelText: "取消",
|
||
|
success: (res) => {
|
||
|
if (res.confirm) {
|
||
|
confirm(downUrl);
|
||
|
console.log('comfirm') //点击确定之后执行的代码
|
||
|
} else {
|
||
|
console.log('cancel') //点击取消之后执行的代码
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
} else {
|
||
|
uni.showToast({
|
||
|
title:"当前是最新版本"
|
||
|
})
|
||
|
console.log("没有新版本")
|
||
|
}
|
||
|
}
|
||
|
}).catch(error => {
|
||
|
console.log("版本错误", error)
|
||
|
})
|
||
|
|
||
|
})
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
export function confirm(downUrl) {
|
||
|
var downloadApkUrl = downUrl
|
||
|
var dtask = plus.downloader.createDownload(downloadApkUrl, {},
|
||
|
function(d, status) {
|
||
|
|
||
|
// 下载完成
|
||
|
if (status == 200) {
|
||
|
|
||
|
plus.runtime.install(plus.io.convertLocalFileSystemURL(
|
||
|
d.filename), {}, {}, function(error) {
|
||
|
uni.showToast({
|
||
|
title: '安装失败',
|
||
|
duration: 1500
|
||
|
});
|
||
|
})
|
||
|
} else {
|
||
|
uni.showToast({
|
||
|
title: '更新失败',
|
||
|
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();
|
||
|
//下载完成
|
||
|
break;
|
||
|
}
|
||
|
});
|
||
|
} catch (err) {
|
||
|
plus.nativeUI.closeWaiting();
|
||
|
uni.showToast({
|
||
|
title: '更新失败-03',
|
||
|
mask: false,
|
||
|
duration: 1500
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|