通过云端打包后,再使用的是“制作移动APP资源升级包”
var server="http://ipas.lt-edu.net/index.php?s=/Publics/Index/update&appid=",//获取升级描述文件服务器地址
localDir="_doc/update/",//本地保存升级描述目录和文件名
keyUpdate="updateCheck",//取消升级键名
keyAbort="updateAbort",//忽略版本键名
checkInterval=0,//升级检查间隔,单位为ms,1小时为60*60*1000=3600000, 如果每次启动需要检查设置值为0
dir=null;
/**
* 准备升级操作
* 创建升级文件保存目录
*/
function initUpdate(isAuto){
if (isAuto) {
// 判断升级检测是否过期
var lastcheck = plus.storage.getItem( keyUpdate );
if ( lastcheck ) {
var dc = parseInt( lastcheck );
var dn = (new Date()).getTime();
if ( dn-dc < checkInterval ) { // 未超过上次升级检测间隔,不需要进行升级检查
return;
}
// 取消已过期,删除取消标记
plus.storage.removeItem( keyUpdate );
}
}
// 从服务器获取升级数据
var appid = plus.runtime.appid;
mui.getJSON(server + appid,{},function (data) {
//appid一致,才将服务器上的版本数据保存到本地
if(data.appid == appid) checkUpdateData(data, isAuto);
});
}
/**
* 检查升级数据
*/
function checkUpdateData( j, isAuto ){
//当前客户端版本号
var curVer = plus.runtime.version, inf = j[plus.os.name];
if ( inf ){
var srvVer = inf.version;
// 判断是否存在忽略版本号
var vabort = plus.storage.getItem( keyAbort );
if ( vabort && srvVer==vabort ) {
// 忽略此版本
return;
}
// 判断是否需要升级
if ( compareVersion(curVer,srvVer) ) {
// 提示用户是否升级
plus.ui.confirm( inf.note, function(i){
if ( 0==i ) {
if (inf.type == 'wgt') {
downWgt(inf.url);
} else {
plus.runtime.openURL( inf.url );
}
} else if ( 1==i ) {
plus.storage.setItem( keyAbort, srvVer );
plus.storage.setItem( keyUpdate, (new Date()).getTime().toString() );
} else {
// plus.storage.setItem( keyUpdate, (new Date()).getTime().toString() );
}
}, inf.title, ["立即更新","跳过此版本","取 消"] );
} else {
if (!isAuto) plus.ui.alert('当前已经是最新版本!');
}
}
}
function downWgt(path){
plus.nativeUI.showWaiting("下载wgt文件...");
plus.downloader.createDownload( path, {filename:localDir}, function(d,status){
if ( status == 200 ) {
console.log("下载wgt成功:"+d.filename);
installWgt(d.filename); // 安装wgt包
} else {
console.log("下载wgt失败!");
plus.nativeUI.alert("下载wgt失败!");
}
plus.nativeUI.closeWaiting();
}).start();
}
function installWgt(path){
plus.nativeUI.showWaiting("安装wgt文件...");
plus.runtime.install(path,{force:true},function(){
plus.nativeUI.closeWaiting();
console.log("安装wgt文件成功!");
plus.nativeUI.alert("应用资源更新完成,请重新启动应用!",function(){
plus.runtime.quit();
});
},function(e){
plus.nativeUI.closeWaiting();
console.log("安装wgt文件失败["+e.code+"]:"+e.message);
plus.nativeUI.alert("安装wgt文件失败["+e.code+"]:"+e.message);
});
}
/**
* 比较版本大小,如果新版本nv大于旧版本ov则返回true,否则返回false
* @param {String} ov
* @param {String} nv
* @return {Boolean}
*/
function compareVersion( ov, nv ){
// return true;
if ( !ov || !nv || ov=="" || nv=="" ){
return false;
}
var b=false,
ova = ov.split(".",4),
nva = nv.split(".",4);
for ( var i=0; i var so=ova[i],no=parseInt(so),sn=nva[i],nn=parseInt(sn); if ( nn>no || sn.length>so.length ) { return true; } else if ( nn return false; } } if ( nva.length>ova.length && 0==nv.indexOf(ov) ) { return true; } }