更新版本

/*

自动检测更新APP版本

[ismanual:true/false(是否手动点击获取更新)]

ismanual=true 会提醒是否最新版本

ismanual=false 不会提醒是否最新版本

*/

function appUpdate(ismanual) {

console.log('开始版本更新检测...');

mui.plusReady(function() {

var wgtWaiting = null,

percent = 0;

plus.runtime.getProperty(plus.runtime.appid, function(inf) {

var version = inf.version;

console.log('当前版本号:V' + version);

var client;

var ua = navigator.userAgent.toLowerCase();

if (/android/.test(ua)) {

//Android系统

mui.ajax({

url: app.service + 'app/version',

type: 'get',

dataType: 'json',

timeout: 10000,

data: {},

success: function(r) {

console.log(JSON.stringify(r))

if (r.StatusCode = 200) {

//服务器最新版本

var _version = r.data.version;

//远程数据库中最新版本andriod版本

console.log("应用商店最新版本:V" + _version);

if (_version > version) {

mui.back = function() {

return false;

};

mui.confirm('应用有新版本V' + _version + ',是否立即下载更新?', "新版本更新", ["立即更新"], function(e) {

if (e.index == 0) {

//执行升级操作

wgtWaiting = plus.nativeUI.showWaiting("0%");

var dtask = plus.downloader.createDownload(r.data.apkUrl, {}, function(d, status) {

if (status == 200) {

var path = d.filename; //已下载apk

// 自动安装apk文件

plus.runtime.install(path, {}, function(wgtinfo) {

wgtWaiting.close();

mui.alert("更新完成,须重新启动应用!", function() {

plus.runtime.restart();

});

}, function(error) {

wgtWaiting.close();

mui.alert("安装失败!\n" + error.message);

});

} else {

plus.nativeUI.alert('下载失败:' + status);

wgtWaiting.close();

}

});

dtask.addEventListener("statechanged", function(download, status) {

switch (download.state) {

case 2:

wgtWaiting.setTitle("0%");

break;

case 3:

try {

percent = download.downloadedSize / download.totalSize * 100;

wgtWaiting.setTitle( parseInt(percent) + "%");

} catch (e) {}

break;

case 4:

wgtWaiting.setTitle("下载完成");

break;

}

});

dtask.start();

} else {

appUpdate();

}

});

} else {

console.log('已经是最新版本了!');

if (ismanual) {

mui.toast('已经是最新版本了!');

}

return;

}

} else {

plus.nativeUI.alert('版本更新失败:接口异常');

return;

}

},

error: function(xhr, type, errerThrown) {

if (ismanual) {

mui.toast('网络异常,请稍候再试');

}

return;

}

});

} else if (/iphone|ipad|ipod/.test(ua)) {

//IOS系统           

mui.ajax({

url: "https://itunes.apple.com/cn/lookup?id=123456789", //获取当前上架APPStore版本信息

type: "get",

dataType: 'json',

data: {

id: 123456789//APP唯一标识ID

},

contentType: 'application/x-www-form-urlencoded;charset=UTF-8',

success: function(res) {

var resultCount = res.resultCount;

for (var i = 0; i < resultCount; i++) {

//服务器最新版本

var _version = res.results[i].version;

//获取远程数据库中上新IOS版本号

console.log("App Store最新版本:V" + _version);

if (_version > version) {

var old_back = mui.back;

mui.back = function() {

plus.navigator.setStatusBarStyle("dark");

old_back();

}

//plus.nativeUI.alert("应用有新版本:V" + _version);

console.log("应用有新版本:V" + _version);

mui.confirm('应用有新版本V' + _version + ',是否立即下载更新?', "新版本更新", ["立即更新"], function(e) {

if (e.index == 0) { //执行升级操作

document.location.href = 'https://itunes.apple.com/cn/app/san-gu-hui/id1472718929?mt=8'; //上新APPStore下载地址

} else {

appUpdate();

}

});

return;

}

}

if (ismanual) {

mui.toast('已经是最新版本了!');

}

return;

}

});

}

});

});

}

你可能感兴趣的:(更新版本)