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

版权声明:本文为博主原创文章,未经博主允许不得转载。如有转载请留言,谢谢~ https://blog.csdn.net/qq_38209578/article/details/86219999

本文只要介绍HBuilder实现App资源在线升级更新。

梳理思路:

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

关键代码:

由于HBuilder开发的App版本,为了方便大家直接使用,下列代码plus.nativeUI.未做封装。使用的时候可以根据自己的习惯自行整理。

//通过接口请求,获取线上的版本号
var checkUrl= "2.0.0" //通过接口请求,获取线上的版本号。此处默认2.0.0
// 获取当前版本号
function checkUpdate(){
	plus.runtime.getProperty(plus.runtime.appid,function(inf){
        wgtVer=inf.version;
        console.log("当前应用版本:"+wgtVer);
	    if(compareVersion(wgtVer, checkUrl)){ // 判断当前版本是否需要更新
	    	   plus.nativeUI.confirm('发现新版本'+checkUrl+'是否下载', function(e){ // 此方法请在plusReady()完成后
				if(e.index>0){
					plus.nativeUI.toast('升级包下载中...');
					 downWgt();  // 下载升级包
				}
				}, 'HelloH5', ['取消','确定']);
	    }
    });
 }
 //版本比较
function compareVersion( ov, nv ){ // ov为本地历史版本,nv为当前线上版本
	console.log(ov, nv)
	if ( !ov || !nv || ov=="" || nv=="" ){
		return false;
	}
	var b=false,
	ova = ov.split(".",4),
	nva = nv.split(".",4);
	for ( var i=0; i<ova.length&&i<nva.length; i++ ) {
		var so=ova[i],no=parseInt(so),sn=nva[i],nn=parseInt(sn);
		if ( nn>no || sn.length>so.length  ) {
			return true;
		} else if ( nn<no ) {
			return false;
		}
	}
	if ( nva.length>ova.length && 0==nv.indexOf(ov) ) {
		return true;
	}
}
// 下载wgt文件
var wgtUrl="http://www.vitian.vip/upload/H5D6C9AEA.wgt"; // 线上版本在线更新的.wgt文件路径
function downWgt(){
//  plus.nativeUI.showWaiting("下载wgt文件...");
    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
        if ( status == 200 ) { 
            console.log("下载wgt成功:"+d.filename);
            plus.nativeUI.confirm('升级包下载完成,是否安装最新版本?', function(e){
			if(e.index>0){
				 installWgt(d.filename); // 安装wgt包
			}
			}, 'HelloH5', ['取消','确定']);
           
        } else {
            console.log("下载wgt失败!");
//          plus.nativeUI.alert("下载wgt失败!");
        }
//      plus.nativeUI.closeWaiting();
    }).start();
}
// 更新应用资源
function installWgt(path){
	console.log(path)
    plus.nativeUI.showWaiting("安装升级文件...");
    plus.runtime.install(path,{},function(){
        plus.nativeUI.closeWaiting();
        console.log("安装wgt文件成功!");
        // 是否立即重启
    	plus.nativeUI.confirm('应用资源更新完成,是否立即重启生效?', function(e){
		if(e.index>0){
			plus.runtime.restart();
		}
		}, 'HelloH5', ['取消','确定']);
//      plus.nativeUI.alert("应用资源更新完成!",function(){
//      	
//          
//      });
    },function(e){
        plus.nativeUI.closeWaiting();
        console.log("安装wgt文件失败["+e.code+"]:"+e.message);
        plus.nativeUI.toast("安装wgt文件失败["+e.code+"]:"+e.message);
    });
}
// 用户主动除非检测版本更新
function isCheckUpdate(){
	plus.runtime.getProperty(plus.runtime.appid,function(inf){
        wgtVer=inf.version;
        console.log("当前应用版本:"+wgtVer);
        console.log(compareVersion(wgtVer, checkUrl))
	    if(compareVersion(wgtVer, checkUrl)){
	    	  plus.nativeUI.confirm('发现新版本'+wgtVer+'是否下载', function(e){
				if(e.index>0){
					plus.nativeUI.toast('升级包下载中...');
					 downWgt();  // 下载升级包
				}
				}, 'HelloH5', ['取消','确定']);
	    } else {
//	    	 plus.nativeUI.alert("当前应用版本为最新版本");
	    	 plus.nativeUI.toast('当前应用版本为最新版本');
	    }
    });
}

注:App资源在线更新,如果只是改过目录结构,如js、css、html页面,属于同名文件覆盖,所以非同名文件是会被保留在本机的。如果改变manifest.json的文件内容,是需要整包升级的。

DEMO下载路径为:https://download.csdn.net/download/qq_38209578/10908123

猜你喜欢

转载自blog.csdn.net/qq_38209578/article/details/86219999