需要说的都在注释里了,直接上代码
function UpdateApp() {
//检测浏览器型号
var userAgent = navigator.userAgent.toLowerCase();
if (/android/.test(userAgent)) {
var version = "";
var download_url = "";
//访问服务端获取最新版本
axios
.get("Api/GetVersion")
.then(function(res) {
//获取服务器版本号和下载地址
var version = res.data.version;
var download_url = res.data.download_url;
plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
var ver = wgtinfo.version; //获取客户端版本号
if (version != ver) { //判断版本
plus.nativeUI.confirm("应用有新版本,是否立即下载更新?", function(event) {
if (event.index == 1) {
/* 用原生等待框显示进度,可自行更换其他UI */
var waiting = plus.nativeUI.showWaiting("0 %");
//创建下载任务
var dtask = plus.downloader.createDownload(
download_url);
//添加下载监听器
dtask.addEventListener("statechanged", function(download, status) {
//计算下载进度
var TotalSize = download.totalSize;
var DownLoadedSize = download.downloadedSize;
var j = parseInt(DownLoadedSize / TotalSize * 100);
if (j > 0) {
waiting.setTitle(j + " %");
}
// 下载完成
if (download.state == 4 && status == 200) {
waiting.close(); //关闭等待框
// 发起安装apk
plus.runtime.install(download.filename);
console.log("Download success: " + download.filename);
}
}, false);
dtask.start();
}
}, '更新提醒', ['取消', '确认']);
} else {
//console.log('当前为最新版本');
}
});
})
.catch(function(error) {
console.log(error);
});
}
}