setTimeout(()=>{
//通過(guò)createXHR()函數(shù)創(chuàng)建一個(gè)XHR對(duì)象:
function createXHR() {
if (window.XMLHttpRequest) {? ? //IE7+、Firefox、Opera、Chrome 和Safari
return new XMLHttpRequest();
} else if (window.ActiveXObject) {? //IE6 及以下
var versions = ['MSXML2.XMLHttp','Microsoft.XMLHTTP'];
for (var i = 0,len = versions.length; i<len; i++) {
try {
return new ActiveXObject(version[i]);
break;
} catch (e) {
//跳過(guò)
}?
}
} else {
throw new Error('瀏覽器不支持XHR對(duì)象!');
}
}
//封裝ajax,參數(shù)為一個(gè)對(duì)象
function ajax(obj) {
var xhr = createXHR();? //創(chuàng)建XHR對(duì)象
//通過(guò)使用JS隨機(jī)字符串解決IE瀏覽器第二次默認(rèn)獲取緩存的問(wèn)題
obj.url = obj.url + '?rand=' + Math.random();
obj.data = params(obj.data);? //通過(guò)params()將名值對(duì)轉(zhuǎn)換成字符串
//若是GET請(qǐng)求,則將數(shù)據(jù)加到url后面
if (obj.method === 'get') {
obj.url += obj.url.indexOf('?') == -1 ? '?' + obj.data : '&' + obj.data;
}
if (obj.async === true) {? //true表示異步,false表示同步
//使用異步調(diào)用的時(shí)候,需要觸發(fā)readystatechange 事件
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {? //判斷對(duì)象的狀態(tài)是否交互完成
callback();? ? ? //回調(diào)
}
};
}
//在使用XHR對(duì)象時(shí),必須先調(diào)用open()方法,
//它接受三個(gè)參數(shù):請(qǐng)求類型(get、post)、請(qǐng)求的URL和表示是否異步。
xhr.open(obj.method, obj.url, obj.async);
if (obj.method === 'post') {
//post方式需要自己設(shè)置http的請(qǐng)求頭,來(lái)模仿表單提交。
//放在open方法之后,send方法之前。
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(obj.data);? ? //post方式將數(shù)據(jù)放在send()方法里
} else {
xhr.send(null);? ? //get方式則填null
}
if (obj.async === false) {? //同步
callback();
}
function callback() {
if (xhr.status == 200) {? //判斷http的交互是否成功,200表示成功
obj.success(xhr.responseText);? ? ? ? ? //回調(diào)傳遞參數(shù)
} else {
alert('獲取數(shù)據(jù)錯(cuò)誤!錯(cuò)誤代號(hào):' + xhr.status + ',錯(cuò)誤信息:' + xhr.statusText);
}?
}
}
//名值對(duì)轉(zhuǎn)換為字符串
function params(data) {
var arr = [];
for (var i in data) {
//特殊字符傳參產(chǎn)生的問(wèn)題可以使用encodeURIComponent()進(jìn)行編碼處理
arr.push(encodeURIComponent(i) + '=' + encodeURIComponent(data[i]));
}
return arr.join('&');
}
ajax({
method : 'post',
url : 'https://shop.ihaosy.com/api/Index/getIndexAdList',
data : {},
success : function (message) {
// alert(message)
var res = JSON.parse(message);
if(res.code == 10000){
// alert('ctycyv'+res);
// alert(res.result.h5_version.value);
var AppUrl = 'https://shop.ihaosy.com/static/haosy.apk';
plus.runtime.getProperty( plus.runtime.appid, function ( wgtinfo ) {
// alert(JSON.stringify(wgtinfo.version));
// alert(JSON.stringify(wgtinfo.versionCode));
if( wgtinfo.versionCode != res.result.h5_version.value ){
installAPP(AppUrl);
}
});
}
},
async : true
});
return false;
},200);
function installAPP(url){
var watiting = plus.nativeUI.showWaiting("更新中:0%");?
//創(chuàng)建下載管理對(duì)象
var dtask = plus.downloader.createDownload(url,{}, function ( d, status ) {
// 下載完成
if ( status == 200 ) { //下載成功后的回調(diào)函數(shù)
plus.nativeUI.toast( "下載成功,準(zhǔn)備安裝" + d.filename );
//安裝程序,第一個(gè)參數(shù)是路徑,默認(rèn)的下載路徑在_downloads里面
plus.runtime.install(d.filename,{},function(){
plus.nativeUI.toast('安裝成功');},function(){plus.nativeUI.toast('安裝失敗');
});
plus.nativeUI.closeWaiting();
plus.downloader.clear();
} else {
alert( "下載失敗 " + status );
plus.downloader.clear();? ? ? ? //清除下載任務(wù)
}
});
dtask.addEventListener("statechanged",? function(task,? status)? {
if? (!dtask) {
return;? ? ? ? ?
}?
// alert(task.state);
switch? (task.state)? {
case? 1:?
// 開始?
break;? ? ? ? ? ? ?
case? 2:?
// 已連接到服務(wù)器?
break;? ? ? ? ? ? ?
case? 3:
// 已接收到數(shù)據(jù)?
var? nowData? =? Math.floor(task.downloadedSize? *? 100? /? task.totalSize);
if? (nowData? %? 5? ===? 0)? {
watiting.setTitle("更新中 "? +? nowData? +? "%");? ? ? ? ? ? ? ? ? ? ?
if? (nowData? ===? 100)? {
plus.nativeUI.toast("正在準(zhǔn)備環(huán)境,請(qǐng)稍后!");
}? ? ? ? ? ? ? ? ?
}? ? ? ? ? ? ? ? ?
break;
case? 4:?
// 下載完成?
break;?
}? ? ?
},false);
dtask.start(); //開始下載任務(wù)
}