uni-app 调用h5+接口,实现整包更新

// 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服务协议,这样可以解决更新的兼容性问题。

posted @ 2020-10-23 17:55  前端海  阅读(2305)  评论(0编辑  收藏  举报