let downloadPath = appPath;
uni.showModal({
//提醒用户更新
title: "更新提示",
content: "有新的更新可用,请升级更新!",
showCancel: false, // 隐藏取消按钮
success: (res) => {
if (res.confirm) {
let showLoading = plus.nativeUI.showWaiting("正在下载");
// 用户点击了确认按钮
const dtask = plus.downloader.createDownload(
downloadPath,
{},
(d, status) => {
console.log("----------------statusstatusstatus-----------",status);
const path = (d.filename);
// 下载完成
if (status == 200) {
showLoading.setTitle("安装中,请勿退出!");
// 强制更新
plus.runtime.install(path, { force: true },
function () {
plus.nativeUI.closeWaiting();
uni.showModal({
title: "提示",
content: "安装成功",
showCancel: false, // 隐藏取消按钮
});
console.log("整包更新成功");
plus.runtime.restart();
},
function (e) {
plus.nativeUI.closeWaiting();
uni.showModal({
title: "提示",
content: "整包更新失败",
showCancel: false, // 隐藏取消按钮
});
console.error("整包更新失败,错误原因:" + e);
}
);
} else {
plus.nativeUI.closeWaiting();
uni.showToast({
title: "更新失败",
duration: 1500,
});
}
}
);
dtask.start(); // 下载任务开始下载
dtask.addEventListener("statechanged",
function (task, status) {
console.log("--------------------task---------------", task);
console.log("--------------------task.downloadedSize---------------", task.downloadedSize);
console.log("--------------------task.downloadedSize---------------", task.totalSize);
console.log(
"task.statetask.statetask.state",
parseInt(
(parseFloat(task.downloadedSize) /
parseFloat(task.totalSize)) *
100
)
);
// 给下载任务设置一个监听 并根据状态 做操作
switch (task.state) {
case 1:
// showLoading.setTitle('正在更新');
break;
case 2:
// showLoading.setTitle('已连接到服务器');
break;
case 3:
showLoading.setTitle(
" 正在下载 - " + parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100) + "% "
);
break;
case 4:
//下载完成
plus.nativeUI.closeWaiting();
break;
}
}
);
0 个回复