mui app更新代码

参考网站:http://ask.dcloud.net.cn/article/182

document.addEventListener('plusready', function() {

// 检测更新
var checkUrl = " http://demo.dcloud.net.cn/test/update/check.php";//填写实际的更新接口’
function checkUpdate(wgtVer) {
if(sessionStorage['checkedVersion']) return;
console.log(wgtVer);
//plus.nativeUI.showWaiting("检测更新...");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
switch(xhr.readyState) {
case 4:
plus.nativeUI.closeWaiting();
if(xhr.status == 200) {
console.log(Trim(xhr.responseText));
var newVer = Trim(xhr.responseText);
if(newVer != "") {
sessionStorage['checkedVersion'] = 1;
if(wgtVer && newVer && cpr_version(newVer, wgtVer)) {


plus.nativeUI.confirm("检测到新版本,确定要下载吗?", function(e) {
if(e.index == 0) {
downWgt(); // 下载升级包
}
}, "版本更新", ["下载", "取消"]);
} else {
console.log("无新版本可更新!");
}
} else {
console.log("新版本获取失败!");
}
} else {
console.log("检测更新失败!");
}
break;
default:
break;
}
}
xhr.open('POST', checkUrl);
xhr.send();
}


// 下载wgt文件
var wgtUrl = " http://demo.dcloud.net.cn/test/update/H5EF3C469.wgt";//填写实际的资源地址
function downWgt() {
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, {
force: true
}, 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);
});
}


function LTrim(str) {
var whitespace = new String(" \t\n\r");
var s = new String(str);
if(whitespace.indexOf(s.charAt(0)) != -1) {
var j = 0,
i = s.length;
while(j < i && whitespace.indexOf(s.charAt(j)) != -1) {
j++;
}
s = s.substring(j, i);
}
return s;
}


function RTrim(str) {
var whitespace = new String(" \t\n\r");
var s = new String(str);
if(whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
var i = s.length - 1;
while(i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
i--;
}
s = s.substring(0, i + 1);
}
return s;
}


function Trim(str) {
return RTrim(LTrim(str));
}


function toNum(a) {
var a = a.toString();
//也可以这样写 var c=a.split(/\./);
var c = a.split('.');
var num_place = ["", "0", "00", "000", "0000"],
r = num_place.reverse();
for(var i = 0; i < c.length; i++) {
var len = c[i].length;
c[i] = r[len] + c[i];
}
var res = c.join('');
return res;
}


function cpr_version(a, b) {
var _a = toNum(a),
_b = toNum(b);
return _a > _b;
}
if(window.plus) {
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
wgtVer = inf.version;
console.log(wgtVer);
checkUpdate(wgtVer);
});
}
})

猜你喜欢

转载自blog.csdn.net/qq_37530404/article/details/80450571
MUI