123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- var wgtVer = null;
- function plusReady() {
- // Android处理返回键
- plus.key.addEventListener('backbutton', function() {
- if(confirm('确认退出?')) {
- plus.runtime.quit();
- }
- }, false);
- // 获取本地应用资源版本号
- plus.runtime.getProperty(plus.runtime.appid, function(inf) {
- wgtVer = inf.version;
- console.log("当前应用版本:" + wgtVer);
- checkUpdate();
- });
- }
- if(window.plus) {
- plusReady();
- } else {
- document.addEventListener('plusready', plusReady, false);
- }
- var newVer = null;
- var newUrl = null; /*下载页,资源路径*/
- function checkUpdate() {
- var serverUrl = localStorage.getItem('$serverUrl');
- if(serverUrl == null || serverUrl == ''){
-
- return false;
- }
- var checkUrl = serverUrl + "/app/loginV2/getVersion";//版本号从服务器获取
-
- plus.nativeUI.showWaiting("检测更新版本...");
-
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function() {
- switch(xhr.readyState) {
-
- //后端接受前端发送过来的数据,此时xhr.readyState = 1
-
- //将数据从后台返回给前端,res.send()发送数据到前端,此时xhr.readyState = 2
-
- //前端接受后台发送过来的数据,接受部分数据时 xhr.readyState = 3
-
- //全部数据接收完毕 xhr.readyState=4
-
- //4完成
- case 4:
- plus.nativeUI.closeWaiting();
-
- if(xhr.status == 200) {
- console.log("检测更新成功:" + xhr.responseText);
-
- var resText = xhr.responseText;
- resText = resText.replace(/\"/g,"");
-
- var resTextSplit =resText.split("||");
- newVer = resTextSplit[0];//新版本号
- newUrl = resTextSplit[1];//新url
-
- if(wgtVer && newVer && (wgtVer != newVer)) {
- if(parseFloat(newVer) > parseFloat(wgtVer)){
- downWgt();
- }else{
- plus.nativeUI.showWaiting("当前版本:" + newVer);
- }
- } else {
- //plus.nativeUI.showWaiting("已是最新版,无需更新");
- }
- } else {
- console.log("检测版本失败!" + xhr.status + "。。。" + xhr.readyState);
- plus.nativeUI.alert("检测版本失败!");
- }
-
- plus.nativeUI.closeWaiting();
- break;
- default:
- break;
- }
- }
- xhr.open('GET', checkUrl);
- xhr.send();
- }
- //下载
- function downWgt() {
- var cfi = confirm("是否下载新的安装包?当前安装版本:"+wgtVer +",新版本:"+newVer);
- if(cfi == true) {
- plus.runtime.openURL(newUrl);//调用浏览器打开网址 然后手工点击安装
- plus.nativeUI.alert("正在通过浏览器下载新安装包,下载完成后请点击安装包进行安装!");
- }
- }
- function downWgt_20190539() { //放弃了..
- var cfi = confirm("是否下载新的安装包?当前安装版本:"+wgtVer +" 新版本:"+newVer);
- if(cfi == true) {
- var dtask = plus.downloader.createDownload(newUrl, {method:"GET",retry: 0 },
- function(d, status) { /*wode.html可以默认不用改,留着就行了*/
- if(status == 200) {
- //open(d.filename); // 打开下载地址
- //plus.nativeUI.alert(d.filename);
- //plus.runtime.openFile(d.filename);
- //installWgt(d.filename); --尝试几十次,都是安装失败!!!!放弃了!
- } else {
- console.log("打开下载页面失败!");
- plus.nativeUI.alert("打开下载页面失败!");
- }
- }
- );
- //下载事件监控
- dtask.addEventListener("statechanged", function(task, status) {
- if(!dtask) {
- return;
- }
- switch(task.state) {
- case 0:
- //plus.nativeUI.closeWaiting();
- //plus.nativeUI.showWaiting("初始化下载");
- break;
- case 1:
- //plus.nativeUI.closeWaiting();
- //plus.nativeUI.showWaiting("开始下载");
- break;
- case 2:
- //plus.nativeUI.closeWaiting();
- //plus.nativeUI.showWaiting("已连接到服务器");
- break;
- case 3:
- //plus.nativeUI.closeWaiting();
- plus.nativeUI.showWaiting("正在下载,请稍后...");
- break;
- case 4:
- //plus.nativeUI.closeWaiting();
- //plus.nativeUI.showWaiting("下载完成!");
- break;
- }
- });
- dtask.start();
- } else {
- //plus.runtime.openURL(newUrl);
- }
- }
- //安装
- function installWgt(path) {
- plus.nativeUI.showWaiting("正在安装新版本");
-
- plus.runtime.install(path,{force:true},function(){
- //plus.nativeUI.closeWaiting();
- //console.log("success");
- plus.runtime.restart();
- },function(e){
- plus.nativeUI.closeWaiting();
- plus.nativeUI.alert("安装新版本失败[" + e.code + "]:" + e.message);
- console.log("failed: "+e.message);
- });
- }
|