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);
});
}
ajax

相關文章
相關標籤/搜索