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);
	  });
},
原创文章 9 获赞 30 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_31093755/article/details/81668924
今日推荐