HBuilder实现App资源在线升级更新

梳理思路:

1.获取线上App版本号和当前App版本号
2.比对版本号,判断是否资源在线升级更新
3.是否下载最新安装包[可以静默下载或用户触发]
4.是否执行资源在线升级更新[可以主动或用户触发]
5.是否立即重启生效[可以主动或用户触发]

关键代码:

  1 //通过接口请求,获取线上的版本号
  2 var checkUrl= "2.0.0" //通过接口请求,获取线上的版本号。此处默认2.0.0
  3 // 获取当前版本号
  4 function checkUpdate(){
  5     plus.runtime.getProperty(plus.runtime.appid,function(inf){
  6         wgtVer=inf.version;
  7         console.log("当前应用版本:"+wgtVer);
  8         if(compareVersion(wgtVer, checkUrl)){ // 判断当前版本是否需要更新
  9                plus.nativeUI.confirm('发现新版本'+checkUrl+'是否下载', function(e){ // 此方法请在plusReady()完成后
 10                 if(e.index>0){
 11                     plus.nativeUI.toast('升级包下载中...');
 12                      downWgt();  // 下载升级包
 13                 }
 14                 }, 'HelloH5', ['取消','确定']);
 15         }
 16     });
 17  }
 18  //版本比较
 19 function compareVersion( ov, nv ){ // ov为本地历史版本,nv为当前线上版本
 20     console.log(ov, nv)
 21     if ( !ov || !nv || ov=="" || nv=="" ){
 22         return false;
 23     }
 24     var b=false,
 25     ova = ov.split(".",4),
 26     nva = nv.split(".",4);
 27     for ( var i=0; i<ova.length&&i<nva.length; i++ ) {
 28         var so=ova[i],no=parseInt(so),sn=nva[i],nn=parseInt(sn);
 29         if ( nn>no || sn.length>so.length  ) {
 30             return true;
 31         } else if ( nn<no ) {
 32             return false;
 33         }
 34     }
 35     if ( nva.length>ova.length && 0==nv.indexOf(ov) ) {
 36         return true;
 37     }
 38 }
 39 // 下载wgt文件
 40 var wgtUrl="http://www.vitian.vip/upload/H5D6C9AEA.wgt"; // 线上版本在线更新的.wgt文件路径
 41 function downWgt(){
 42 //  plus.nativeUI.showWaiting("下载wgt文件...");
 43     plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
 44         if ( status == 200 ) { 
 45             console.log("下载wgt成功:"+d.filename);
 46             plus.nativeUI.confirm('升级包下载完成,是否安装最新版本?', function(e){
 47             if(e.index>0){
 48                  installWgt(d.filename); // 安装wgt包
 49             }
 50             }, 'HelloH5', ['取消','确定']);
 51            
 52         } else {
 53             console.log("下载wgt失败!");
 54 //          plus.nativeUI.alert("下载wgt失败!");
 55         }
 56 //      plus.nativeUI.closeWaiting();
 57     }).start();
 58 }
 59 // 更新应用资源
 60 function installWgt(path){
 61     console.log(path)
 62     plus.nativeUI.showWaiting("安装升级文件...");
 63     plus.runtime.install(path,{},function(){
 64         plus.nativeUI.closeWaiting();
 65         console.log("安装wgt文件成功!");
 66         // 是否立即重启
 67         plus.nativeUI.confirm('应用资源更新完成,是否立即重启生效?', function(e){
 68         if(e.index>0){
 69             plus.runtime.restart();
 70         }
 71         }, 'HelloH5', ['取消','确定']);
 72 //      plus.nativeUI.alert("应用资源更新完成!",function(){
 73 //          
 74 //          
 75 //      });
 76     },function(e){
 77         plus.nativeUI.closeWaiting();
 78         console.log("安装wgt文件失败["+e.code+"]:"+e.message);
 79         plus.nativeUI.toast("安装wgt文件失败["+e.code+"]:"+e.message);
 80     });
 81 }
 82 // 用户主动除非检测版本更新
 83 function isCheckUpdate(){
 84     plus.runtime.getProperty(plus.runtime.appid,function(inf){
 85         wgtVer=inf.version;
 86         console.log("当前应用版本:"+wgtVer);
 87         console.log(compareVersion(wgtVer, checkUrl))
 88         if(compareVersion(wgtVer, checkUrl)){
 89               plus.nativeUI.confirm('发现新版本'+wgtVer+'是否下载', function(e){
 90                 if(e.index>0){
 91                     plus.nativeUI.toast('升级包下载中...');
 92                      downWgt();  // 下载升级包
 93                 }
 94                 }, 'HelloH5', ['取消','确定']);
 95         } else {
 96 //             plus.nativeUI.alert("当前应用版本为最新版本");
 97              plus.nativeUI.toast('当前应用版本为最新版本');
 98         }
 99     });
100 }

 

posted @ 2019-12-03 11:24  待炒的鱼  阅读(1325)  评论(0编辑  收藏  举报