upversion.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. var wgtVer = null;
  2. function plusReady() {
  3. // Android处理返回键
  4. plus.key.addEventListener('backbutton', function() {
  5. if(confirm('确认退出?')) {
  6. plus.runtime.quit();
  7. }
  8. }, false);
  9. // 获取本地应用资源版本号
  10. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  11. wgtVer = inf.version;
  12. console.log("当前应用版本:" + wgtVer);
  13. checkUpdate();
  14. });
  15. }
  16. if(window.plus) {
  17. plusReady();
  18. } else {
  19. document.addEventListener('plusready', plusReady, false);
  20. }
  21. var newVer = null;
  22. var newUrl = null; /*下载页,资源路径*/
  23. function checkUpdate() {
  24. var serverUrl = localStorage.getItem('$serverUrl');
  25. if(serverUrl == null || serverUrl == ''){
  26. return false;
  27. }
  28. var checkUrl = serverUrl + "/app/loginV2/getVersion";//版本号从服务器获取
  29. plus.nativeUI.showWaiting("检测更新版本...");
  30. var xhr = new XMLHttpRequest();
  31. xhr.onreadystatechange = function() {
  32. switch(xhr.readyState) {
  33. //后端接受前端发送过来的数据,此时xhr.readyState = 1
  34. //将数据从后台返回给前端,res.send()发送数据到前端,此时xhr.readyState = 2
  35. //前端接受后台发送过来的数据,接受部分数据时 xhr.readyState = 3
  36. //全部数据接收完毕 xhr.readyState=4
  37. //4完成
  38. case 4:
  39. plus.nativeUI.closeWaiting();
  40. if(xhr.status == 200) {
  41. console.log("检测更新成功:" + xhr.responseText);
  42. var resText = xhr.responseText;
  43. resText = resText.replace(/\"/g,"");
  44. var resTextSplit =resText.split("||");
  45. newVer = resTextSplit[0];//新版本号
  46. newUrl = resTextSplit[1];//新url
  47. if(wgtVer && newVer && (wgtVer != newVer)) {
  48. if(parseFloat(newVer) > parseFloat(wgtVer)){
  49. downWgt();
  50. }else{
  51. plus.nativeUI.showWaiting("当前版本:" + newVer);
  52. }
  53. } else {
  54. //plus.nativeUI.showWaiting("已是最新版,无需更新");
  55. }
  56. } else {
  57. console.log("检测版本失败!" + xhr.status + "。。。" + xhr.readyState);
  58. plus.nativeUI.alert("检测版本失败!");
  59. }
  60. plus.nativeUI.closeWaiting();
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. xhr.open('GET', checkUrl);
  67. xhr.send();
  68. }
  69. //下载
  70. function downWgt() {
  71. var cfi = confirm("是否下载新的安装包?当前安装版本:"+wgtVer +",新版本:"+newVer);
  72. if(cfi == true) {
  73. plus.runtime.openURL(newUrl);//调用浏览器打开网址 然后手工点击安装
  74. plus.nativeUI.alert("正在通过浏览器下载新安装包,下载完成后请点击安装包进行安装!");
  75. }
  76. }
  77. function downWgt_20190539() { //放弃了..
  78. var cfi = confirm("是否下载新的安装包?当前安装版本:"+wgtVer +" 新版本:"+newVer);
  79. if(cfi == true) {
  80. var dtask = plus.downloader.createDownload(newUrl, {method:"GET",retry: 0 },
  81. function(d, status) { /*wode.html可以默认不用改,留着就行了*/
  82. if(status == 200) {
  83. //open(d.filename); // 打开下载地址
  84. //plus.nativeUI.alert(d.filename);
  85. //plus.runtime.openFile(d.filename);
  86. //installWgt(d.filename); --尝试几十次,都是安装失败!!!!放弃了!
  87. } else {
  88. console.log("打开下载页面失败!");
  89. plus.nativeUI.alert("打开下载页面失败!");
  90. }
  91. }
  92. );
  93. //下载事件监控
  94. dtask.addEventListener("statechanged", function(task, status) {
  95. if(!dtask) {
  96. return;
  97. }
  98. switch(task.state) {
  99. case 0:
  100. //plus.nativeUI.closeWaiting();
  101. //plus.nativeUI.showWaiting("初始化下载");
  102. break;
  103. case 1:
  104. //plus.nativeUI.closeWaiting();
  105. //plus.nativeUI.showWaiting("开始下载");
  106. break;
  107. case 2:
  108. //plus.nativeUI.closeWaiting();
  109. //plus.nativeUI.showWaiting("已连接到服务器");
  110. break;
  111. case 3:
  112. //plus.nativeUI.closeWaiting();
  113. plus.nativeUI.showWaiting("正在下载,请稍后...");
  114. break;
  115. case 4:
  116. //plus.nativeUI.closeWaiting();
  117. //plus.nativeUI.showWaiting("下载完成!");
  118. break;
  119. }
  120. });
  121. dtask.start();
  122. } else {
  123. //plus.runtime.openURL(newUrl);
  124. }
  125. }
  126. //安装
  127. function installWgt(path) {
  128. plus.nativeUI.showWaiting("正在安装新版本");
  129. plus.runtime.install(path,{force:true},function(){
  130. //plus.nativeUI.closeWaiting();
  131. //console.log("success");
  132. plus.runtime.restart();
  133. },function(e){
  134. plus.nativeUI.closeWaiting();
  135. plus.nativeUI.alert("安装新版本失败[" + e.code + "]:" + e.message);
  136. console.log("failed: "+e.message);
  137. });
  138. }