mui项目实时更新

var wgtVer=null;
function plusReady(){
// ......
// 获取本地应用资源版本号
plus.runtime.getProperty(plus.runtime.appid,function(inf){
wgtVer=inf.version;
console.log("当前应用版本:"+wgtVer);
banben()

});
}
// 进入首页更新
if(window.plus){
plusReady();
}else{
document.addEventListener('plusready',plusReady,false);
}
//检测版本
var num=parseInt(Math.random()*1000);
function banben(){
var banurl="http://cdn.brain-info.top/saima.json?v="+num;
try{
mui.ajax(banurl,{
dataType:'json',//服务器返回json格式数据
type:'get',//HTTP请求类型
timeout:10000,//超时时间设置为10秒;
success:function(data){
if(data.version!=wgtVer){
downWgt()
}
},
error:function(xhr,type,errorThrown){
}
});
}catch(e){
}
}

// 下载wgt文件
var wgtUrl="http://cdn.brain-info.top/saima.wgt?="+num;
function downWgt(){
plus.nativeUI.showWaiting("应用更新中...");
plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
if ( status == 200 ) {
console.log("下载wgt成功:"+d.filename);
installWgt(d.filename); // 安装wgt包
} else {
console.log("下载wgt失败!");
plus.nativeUI.alert("应用更新失败!");
}
plus.nativeUI.closeWaiting();
}).start();
}
//更新应用资源包(wgt文件)
function installWgt(path){
plus.nativeUI.showWaiting("应用安装中...");
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("安装应用失败["+e.code+"]:"+e.message);
});
}

原文地址:https://www.cnblogs.com/wrong930803/p/9295175.html