// APP整包更新
//#ifdef APP-PLUS
let thatProgress=this;
console.log(thatProgress.progress)
let server = this.constant.baseUrl+"/system-service/api/app/update"; //檢查更新地址
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
uni.request({
url: server+'?version='+widgetInfo.version+'&appid='+widgetInfo.appid,
method:'POST',
success: (res) => {
console.log(res)
let data=res.data
if (data.code === 200 && data.data.update) {
uni.showModal({ //提醒用戶更新
title: "更新提示",
content: data.data.note,
cancelText:'立即更新',
confirmText:'取消',
success: (result) => {
if (result.cancel) {
var task=plus.downloader.createDownload( data.data.pkgUrl, {}, function(download,status){ //安裝到手機的目錄
if ( status == 200 ) {
plus.runtime.install(download.filename); // 安裝下載的apk文件
} else {
mui.toast("下載更新失敗!");
plus.nativeUI.closeWaiting();
}
});
var showLoading = plus.nativeUI.showWaiting("正在下載");
//監聽下載
task.addEventListener("statechanged", function (download, status) {
// console.log(download.state)
// console.log(task.downloadedSize)
switch (download.state) {
case 2:
showLoading.showWaiting("正在下載...");
break;
case 3:
//進度條百分比 totalSize為總量,baifen為當前下載的百分比
let prg = parseInt(
(parseFloat(task.downloadedSize) /parseFloat(task.totalSize)) *100
);
showLoading.setTitle("下載進度" +prg + "% ");
if(prg==100){
plus.nativeUI.closeWaiting()
}
break;
case 4:
mui.toast("下載完成");
plus.nativeUI.closeWaiting();
break;
}
});
task.start();
}
}
})
}
},
fail: (res) =>{
console.log(res)
uni.showToast({
title:res.message,
icon:'none'
})
}
});
});
值得注意的是,iOS的更新只支持HTTPS服務協議,所以,服務端最好用HTTPS服務協議,這樣可以解決更新的兼容性問題。