wap2app在线升级自动更新代码如下:
把代码放入app.js即可,并且把应用标识改成自己的,如“__W2A__tbk.zjitgzs.cn”
App({
options: {
debug: false
},
// 当wap2app初始化完成时,会触发 onLaunch
onLaunch: function() {
},
//当wap2app启动,或从后台进入前台显示,会触发 onShow
onShow: function() {
checkUpdate();
var wgtVer=null;
function plusReady(){
// 获取本地应用资源版本号
plus.runtime.getProperty(plus.runtime.appid,function(inf){
wgtVer=inf.version;
});
}
if(window.plus){
plusReady();
}else{
document.addEventListener('plusready',plusReady,false);
}
// 检测更新
function checkUpdate(){
var checkUrl="https://zjitgzs.cn/update/bbh.php";//获取新版本号
//plus.nativeUI.showWaiting("检测更新...");
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
switch(xhr.readyState){
case 4:
plus.nativeUI.closeWaiting();
if(xhr.status==200){
// console.log("检测更新成功:"+xhr.responseText);
var newVer=xhr.responseText;
if(wgtVer&&newVer&&(wgtVer!=newVer)){
downWgt(); // 下载升级包
}else{
//console.log("无更新版本!");
//plus.nativeUI.alert("无新版本可更新!");
}
}else{
plus.nativeUI.alert("检测更新失败!");
}
break;
default:
break;
}
}
xhr.open('GET',checkUrl);
xhr.send();
}
// 下载wgt文件
var wgtUrl="https://zjitgzs.cn/update/update.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,{},function(){
plus.nativeUI.closeWaiting();
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+("\n")+"请联系管理员QQ:3447362049");
});
}
},
/**
* 当wap2app从前台进入后台,会触发 onHide
*/
onHide: function() {
}
});
Page('__W2A__tbk.zjitgzs.cn', { //首页扩展配置
onShow: function() {
},
onClose: function() {
}
});