hbuilder热更新

前端代码:

// 获取当前应用的版本号

​
var wgtVer=null;
function plusReady(){
    // ......
    // 获取本地应用资源版本号
    plus.runtime.getProperty(plus.runtime.appid,function(inf){
        wgtVer=inf.version;
        console.log("当前应用版本:"+wgtVer);
        checkUpdate(); 
    });
}
if(window.plus){
    plusReady();
}else{
    document.addEventListener('plusready',plusReady,false);
}

​




// 发起ajax请求检测是否有新版本

var checkUrl="http://ptest.liby.com.cn/MessageTransfer.php?c=message&m=getupdatetwo";
function checkUpdate(){
//     plus.nativeUI.showWaiting("检测更新...");
    var xhr=new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        switch(xhr.readyState){
            case 4:
            plus.nativeUI.closeWaiting();
            if(xhr.status==200){
                console.log("检测更新成功:"+xhr.responseText);
                var newVer=xhr.responseText;
                newVer = JSON.parse(newVer);
       
                if(wgtVer&&newVer&&(wgtVer.substring(0,3) < newVer.versionName.substring(0,3))){
                if(window.confirm('检测到更新,是否更新?')){
                    downWgt(newVer.apk);  // 下载升级包
                }
                }else{
                    // plus.nativeUI.alert("无新版本可更新!");
                }
            }else{
                console.log("检测更新失败!");
                // plus.nativeUI.alert("检测更新失败!");
            }
            break;
            default:
            break;
        }
    }
    xhr.open('GET',checkUrl);
    xhr.send();
}






// 下载wgt文件
// var wgtUrl="http://demo.dcloud.net.cn/test/update/H5EF3C469.wgt";
function downWgt(wgtUrl){
// console.log(wgtUrl);return;
    plus.nativeUI.showWaiting("下载更新文件...");
    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
        if ( status == 200 ) { 
            console.log("下载更新文件成功:"+d.filename);
            installWgt(d.filename); // 安装wgt包
        } else {
            console.log("下载失败!");
            plus.nativeUI.alert("下载失败!");
        }
        plus.nativeUI.closeWaiting();
    }).start();
}






// 更新应用资源
function installWgt(path){
    plus.nativeUI.showWaiting("安装更新文件...");
    plus.runtime.install(path,{},function(){
        plus.nativeUI.closeWaiting();
        console.log("安装更新文件成功!");
        plus.nativeUI.alert("应用资源更新完成!",function(){
            plus.runtime.restart();
        });
    },function(e){
        plus.nativeUI.closeWaiting();
        console.log("安装更新文件失败["+e.code+"]:"+e.message);
        plus.nativeUI.alert("安装更新文件失败["+e.code+"]:"+e.message);
        if(e.code == 10){
        alert('请清除临时目录');
        }
    });

}

php代码:

public function getupdatetwo()
    {
        $arr = array(
            'versionCode' => 102,
            'versionName' => '1.8.0',
            'msg' => '有新版本可供更新.\n 1.界面美化 \n 2.性能优化',
            'apk' => 'http://cloud-dev.playxs.com/static/app/H50488D36.wgt',//存放在服务器的升级资源包
            );
        echo trim(json_encode($arr));
    }

猜你喜欢

转载自blog.csdn.net/qq_39702364/article/details/79633783