.project 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. var server="http://192.168.1.133:28080/account/update",//获取升级描述文件服务器地址133
  2. //var server="http://astroway.net:28081/account/update",//获取升级描述文件服务器地址astroway
  3. localDir="update",localFile="update.json",//本地保存升级描述目录和文件名
  4. keyUpdate="updateCheck",//取消升级键名
  5. keyAbort="updateAbort",//忽略版本键名
  6. checkInterval=0,//升级检查间隔,单位为ms,7天为7*24*60*60*1000=60480000, 如果每次启动需要检查设置值为0
  7. dir=null;
  8. /**
  9. * 准备升级操作
  10. * 创建升级文件保存目录
  11. */
  12. function initUpdate(){
  13. // 打开doc根目录
  14. plus.io.requestFileSystem( plus.io.PRIVATE_DOC, function(fs){
  15. fs.root.getDirectory( localDir, {create:true}, function(entry){
  16. dir = entry;
  17. //进行版本判断,运行版本小于服务器版本则更新
  18. //checkUpdate();
  19. //直接更新不判断版本号的问题
  20. getUpdateData();
  21. }, function(e){
  22. console.log( "准备升级操作,打开update目录失败:"+e.message );
  23. });
  24. },function(e){
  25. console.log( "准备升级操作,打开doc目录失败:"+e.message );
  26. });
  27. }
  28. /**
  29. * 检测程序升级
  30. */
  31. function checkUpdate() {
  32. // 判断升级检测是否过期
  33. // var lastcheck = plus.storage.getItem( keyUpdate );
  34. // if ( lastcheck ) {
  35. // var dc = parseInt( lastcheck );
  36. // var dn = (new Date()).getTime();
  37. // if ( dn-dc < checkInterval ) { // 未超过上次升级检测间隔,不需要进行升级检查
  38. // return;
  39. // }
  40. // // 取消已过期,删除取消标记
  41. // plus.storage.removeItem( keyUpdate );
  42. // }
  43. // 读取本地升级文件
  44. dir.getFile( localFile, {create:false}, function(fentry){
  45. fentry.file( function(file){
  46. var reader = new plus.io.FileReader();
  47. reader.onloadend = function ( e ) {
  48. fentry.remove();
  49. var data = null;
  50. try{
  51. data=JSON.parse(e.target.result);
  52. }catch(e){
  53. console.log( "读取本地升级文件,数据格式错误!" );
  54. return;
  55. }
  56. checkUpdateData( data );
  57. }
  58. reader.readAsText(file);
  59. }, function(e){
  60. console.log( "读取本地升级文件,获取文件对象失败:"+e.message );
  61. fentry.remove();
  62. } );
  63. }, function(e){
  64. // 失败表示文件不存在,从服务器获取升级数据
  65. getUpdateData();
  66. });
  67. }
  68. /**
  69. * 检查升级数据
  70. */
  71. function checkUpdateData( j ){
  72. //当前客户端版本号
  73. var curVer=plus.runtime.version;
  74. //var curVer = "1.0";
  75. inf = j[plus.os.name];
  76. var srvVer = inf.version;
  77. //alert("当前版本:" + curVer + " 新版本 " + srvVer);
  78. // 判断是否需要升级
  79. if ( compareVersion(curVer,srvVer) ) {
  80. // 提示用户是否升级
  81. plus.ui.confirm( inf.note, function(i){
  82. if ( 0==i ) {
  83. var apk_dir = j.Android.url;
  84. createDownload(apk_dir);
  85. } else {
  86. }
  87. }, inf.title, ["立即更新","取  消"] );
  88. }
  89. }
  90. /**
  91. * 从服务器获取升级数据,并存储到本地;
  92. */
  93. function getUpdateData(){
  94. mui.getJSON(server,{},function (data) {
  95. if(true){
  96. // 保存到本地文件中
  97. dir.getFile( localFile, {create:true}, function(fentry){
  98. fentry.createWriter( function(writer){
  99. writer.onerror = function(){
  100. console.log( "获取升级数据,保存文件失败!" );
  101. };
  102. //从服务器下载下来JSON文件
  103. writer.write(data);
  104. //根据下载的JSON文件判断是否更新
  105. checkUpdateData(data);
  106. }, function(e){
  107. console.log( "获取升级数据,创建写文件对象失败:"+e.message );
  108. } );
  109. }, function(e){
  110. console.log( "获取升级数据,打开保存文件失败:"+e.message );
  111. });
  112. }
  113. });
  114. }
  115. /**
  116. * 比较版本大小,如果新版本nv大于旧版本ov则返回true,否则返回false
  117. * @param {String} ov
  118. * @param {String} nv
  119. * @return {Boolean}
  120. */
  121. function compareVersion( ov, nv ){
  122. if ( !ov || !nv || ov=="" || nv=="" ){
  123. return false;
  124. }
  125. var b=false,
  126. ova = ov.split(".",4),
  127. nva = nv.split(".",4);
  128. for ( var i=0; i<ova.length&&i<nva.length; i++ ) {
  129. var so=ova[i],no=parseInt(so),sn=nva[i],nn=parseInt(sn);
  130. if ( nn>no || sn.length>so.length ) {
  131. return true;
  132. } else if ( nn<no ) {
  133. return false;
  134. }
  135. }
  136. if ( nva.length>ova.length && 0==nv.indexOf(ov) ) {
  137. return true;
  138. }
  139. }
  140. // 创建下载任务
  141. function createDownload(apk_dir) {
  142. console.log(apk_dir);
  143. var dtask = plus.downloader.createDownload( apk_dir, {}, function ( d, status ) {
  144. // 下载完成
  145. if ( status == 200 ) {
  146. console.log( "Download success: " + d.filename );
  147. installApp(d.filename);
  148. } else {
  149. console.log( "Download failed: " + status );
  150. }
  151. });
  152. var w=plus.nativeUI.showWaiting("   开始下载...   ");
  153. dtask.addEventListener( "statechanged", function(task,status){
  154. switch(task.state) {
  155. case 1: // 开始
  156. w.setTitle("   开始下载...   ");
  157. break;
  158. case 2: // 已连接到服务器
  159. w.setTitle("   开始下载...   ");
  160. break;
  161. case 3:
  162. var a = task.downloadedSize/task.totalSize*100;
  163. w.setTitle("   已下载"+parseInt(a)+"%   ");
  164. break;
  165. case 4: // 下载完成
  166. w.close();
  167. break;
  168. }
  169. } );
  170. dtask.start();
  171. }
  172. //安装软件
  173. function installApp(install_dir){
  174. plus.runtime.install( install_dir, {}, function(){
  175. console.info("install success !");
  176. plus.runtime.quit();
  177. }, function(){
  178. console.info("install error !");
  179. });
  180. }
  181. //mui.plusReady(initUpdate);