HBuilder webApp熱更新(在線升級)的實現

#####一、製作移動APP資源升級包(wgt文件)
製作wgt文件之前,清先在manifest.json文件中更新版本號,如果項目中的版本號和wgt文件中的版本號相同,則會更新失敗,下圖是製作wgt文件的方法:
這裏寫圖片描述

#####二、打開APP,先檢查當前是否最新版本

// 檢查當前版本,與從後臺獲取的版本作比較,以此判斷是否更新     
 plus.runtime.getProperty(plus.runtime.appid,function(inf){
	 // 當前版本
     var wgtVersion = inf.version;
      mui.ajax("https:// houtai.com", {
          data: { },
          dataType: 'json',
          type: 'get',
          success:function(data){
	          // 如果有新版本,則提示需要更新
              if( data.version > wgtVersion ){
                   mui.confirm('檢查更新','發現新版本,是否更新',['確定','取消'],function(e){
                      if(e.index==0){
                          downloadWgt(); // 下載wgt方法
                      }else{
                          return;
                      }
                   })
              }else{
                  return;
              }
          },
          error:function(err){
          }
      });
  });

});         	  

#####三、如需更新,則下載並安裝更新文件(wgt文件)

// 下載wgt方法
function:downloadWgt (){
   var that = this;
	// 更新文件 wgt 文件地址
	var wgtUrl = "http://192.168.0.156/H503A1250.wgt";
    plus.nativeUI.showWaiting("正在更新...");
    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
     if ( status == 200 ) {
          console.log("下載wgt成功:"+d.filename);
          that.installWgt(d.filename); // 安裝wgt方法
      } else {
          console.log("下載wgt失敗!");
          plus.nativeUI.alert("下載wgt失敗!");
      }
      plus.nativeUI.closeWaiting();
  }).start();
},
// 安裝wgt方法
function:installWgt(path) {
	  plus.nativeUI.showWaiting("安裝wgt文件...");
	  plus.runtime.install(path,{},function(){
	      plus.nativeUI.closeWaiting();
	      console.log("安裝wgt文件成功!");
	      plus.nativeUI.alert("應用資源更新完成!",function(){
	          plus.runtime.restart();
	      });
	  },function(e){ 
	      plus.nativeUI.closeWaiting();
	      console.log("安裝wgt文件失敗["+e.code+"]:"+e.message);
	      plus.nativeUI.alert("安裝wgt文件失敗["+e.code+"]:"+e.message);
	  });
},

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章