2023年11月26日发(作者:)

HBuilderX开发app实现⾃动更新版本

  需求说明:使⽤MUI+Vue等技术并且通过HBuilderX打包开发移动app,在有版本更新时需要⾃动提⽰⽤户有新版本,并且可以

点击下载⾃动安装。

思路说明:

1. 应⽤打开时(使⽤Vue的⽣命周期mounted),获取⾃⼰的版本信息appinfo;

(本⾝app的信息⼀般存放在中,直接获取即可)

ady(function(){

N("", null, function(manifest){

var version = n

var vercode_local = ;

var vername_local = ;

("版本名称:"+vername_local+",版本code"+vercode_local);

});

});

2. 获取remote服务器上移动应⽤最新的版本信息(⼀般是存放⼀个json的数据信息)

N(【服务器的url + n_json, null, function(data) {

var verCode = e;

var verName = e;

//服务器版本

//("服务器版本名称:"+verName +",服务器版本code:"+verCode);

});

3. 判断两个版本号是否相等,不相等则需要更新

if(vercode_local == verCode) {

("当前已经是最新版本!");

} else {

var btnArray = ['', ''];

m('当前版本是:' + vername_local + ', 最新版本是:' + verName + ', 是否⽴即更新?', '发现最新版本', btnArray, function(z) {

if( == 0) {

('确定');

installApk( + n_apk);

} else {

('不确定');

return;

}

});

}

1.vuemounted⽣命周期进⾏更新校验

mounted() {

ady(function() {

//("检查更新");

({statusBarBackground: '#EEEEEE'});

ientation("landscape-primary");

if(d) {

N("../../", null, function(data) {

var version = n

var vercode_local = ;

var vername_local = ;

//当前版本

//("版本名称:"+vername_local+",版本code"+vercode_local);

N( + n_json, null, function(data) {

var verCode = e;

//服务器版本

//("服务器版本code:"+verCode);

var verName = e;

if(vercode_local == verCode) {

("当前已经是最新版本!");

} else {

var btnArray = ['', ''];

m('当前版本是:' + vername_local + ', 最新版本是:' + verName + ', 是否⽴即更新?', '发现最新版本', btnArray, function(z) {

if( == 0) {

('确定');

installApk( + n_apk);

} else {

('不确定');

return;

}

});

}

});

});

var BASEINFO={

//url

接⼝

maxsvc:"*.*.*.*:port/appUpdate",

maxbiz:"*.*.*.*:port/maxbiz",

download:"*.*.*.*:port/whatsup/"

}

function getWinWH() {

var temp = {};

var winWidth = "0";

var winHeight = "0";

if(window.innerWidth) {

winWidth = window.innerWidth;

} else if((document.body) && (document.body.clientWidth)) {

winWidth = document.body.clientWidth;

}

if(window.innerHeight) {

winHeight = window.innerHeight;

} else if((document.body) && (document.body.clientHeight)) {

winHeight = document.body.clientHeight;

}

if(document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {

winHeight = document.documentElement.clientHeight;

winWidth = document.documentElement.clientWidth;

}

temp.winWidth = winWidth;

temp.winHeight = winHeight;

return temp;

}

function getAppInfo(appname) {

var prefix = "";

var path = "";

var name = "";

// MaxUA

if(appname == "mua") {

path = "";

prefix = "";

name = path;

}

// 异常停线通知(L5)

if(appname == "men") {

path = ".b2b.";

prefix = "asm_";

name = path + prefix + appname;

}

// E点名(L5)

if(appname == "mrc") {

path = ".b2b.";

prefix = "asm_";

name = path + prefix + appname;

}

// 组装⽣产⽇报表(L5)

if(appname == "pdr") {

path = ".b2b.";

prefix = "asm_";

name = path + prefix + appname;

}

// 仓库进料转仓(L6)

if(appname == "mgr") {

path = "n.";

prefix = "";

name = path + prefix + appname;

}

// 发料单查询(L6)

if(appname == "mgi") {

path = "ty";

prefix = "";

name = path;

}

// 仓库分批发料(L5)

if(appname == "mso") {

path = ".b2b.";

prefix = "asm_";

name = path + prefix + appname;

}

// 上料表查询(L6)

if(appname == "mgf") {

path = ".b2b.";

prefix = "asm_";

name = path + prefix + appname;

}

// IQC进料检验(L6)

if(appname == "mqi") {

path = "";

prefix = "";

name = path;

}

// 流程卡(L5)

if(appname == "wfc") {

path = ".";

prefix = "";

name = path + prefix + appname;

}

// mqi_v

if(appname == "mqi_v") {

path = "n.";

prefix = "";

name = path + prefix + appname;

}

var appInfo = {};

appInfo.scheme = "open";

appInfo.host = name;

appInfo.package = name;

appInfo.version_json = "/resource/download/" + appname + ".json";

appInfo.version_apk = "/resource/download/" + appname + ".apk";

appInfo.version_desc = "/resource/download/" + appname + ".desc";

return appInfo;

}

function installApk(url) {

var dtask = plus.downloader.createDownload(url, {}, function(d, status) {

if(status == 200) {

plus.nativeUI.toast("正在准备环境,请稍后!");

sleep(1000);

var path = d.filename;

console.log(d.filename);

plus.runtime.install(path);

} else {

alert('Download failed:' + status);

}

});

dtask.start();

}

function sleep(numberMillis) {

var now = new Date();

var exitTime = now.getTime() + numberMillis;

while(true) {

now = new Date();

if(now.getTime() > exitTime)

return;

}

}

function getAppVersion(packagename) {

var versionJson = {};

var main = plus.android.runtimeMainActivity();

var pm = main.getPackageManager();

var PackageManager = plus.android.importClass(pm);

var pi = pm.getPackageInfo(packagename, 0);

var PackageInfo = plus.android.importClass(pi);

var vercode_local = plus.android.getAttribute(pi, "versionCode");

var vername_local = plus.android.getAttribute(pi, "versionName");

versionJson.vercode_local = vercode_local;

versionJson.vername_local = vername_local;

return versionJson;

}

function jumpAndroid(appname) {

var appInfo = getAppInfo(appname);

//

console.log("appInfo "+appInfo);

var url = appInfo.scheme + "://" + appInfo.host +

"?BUNDLE_CAMP=" + localStorage.getItem("musercamp") +

"&BUNDLE_USERNAME=" + localStorage.getItem("musername") +

"&BUNDLE_USERPASSWRD=" + localStorage.getItem("mpassword");

//

console.log("appInfo "+appInfo);

try {

if(plus.os.name == "Android") {

if(plus.runtime.isApplicationExist({

pname: appInfo.host,

action: appInfo.scheme + '://'

})) {

var versionJosn = getAppVersion(appInfo.package);

var vercode_local = versionJosn.vercode_local;

var vername_local = versionJosn.vername_local;

mui.getJSON(BASEINFO.maxsvc + appInfo.version_json, null, function(data) {

var verCode = data.verCode;

var verName = data.verName;

if(vercode_local == verCode) {

location.href = url;

} else {

var btnArray = ['', ''];

mui.confirm('当前版本是:' + vername_local + ', 最新版本是:' + verName + ', 是否⽴即更新?', '发现最新版本', btnArray, function(z) {

if(z.index == 0) {

installApk(BASEINFO.maxsvc + appInfo.version_apk);

} else {

location.href = url;

}

});

}

});

} else {

var btnArray = ['', ''];

mui.confirm('应⽤程式未安装, 是否⽴即安装?', '应⽤程式安装确认', btnArray, function(z) {

if(z.index == 0) {

installApk(BASEINFO.maxsvc + appInfo.version_apk);

} else {

return;

return;

}

});

}

}

} catch(e) {

mui.toast("更新失败 "+e.toString());

}

}

function myAjax(url, type, input, timeout, success, error) {

var xhr = new plus.net.XMLHttpRequest();

if(timeout && timeout > 0) xhr.timeout = timeout;

xhr.onreadystatechange = function() {

switch(xhr.readyState) {

case 0:

console.log("xhr请求已初始化");

break;

case 1:

console.log("xhr请求已打开");

break;

case 2:

console.log("xhr请求已发送");

break;

case 3:

console.log("xhr请求已响应");

break;

case 4:

if(xhr.status == 200) {

success(eval('('+xhr.responseText + ')'));

} else {

error(xhr.readyState, xhr);

}

break;

default:

break;

}

}

if(input) {

if(type == 'post' || type == 'get') {

xhr.open(type || "GET", url);

xhr.send(JSON.stringify(input));

} else {

throw new Error("type is undefined !")

}

} else {

if(type != 'post' && type != 'get') {

throw new Error("type is undefined !")

}

xhr.open(type || "GET", url);

xhr.send();

}

}

function isSysAdmin(groups) {

var result = false;

for(var i = 0; i < groups.length; i++) {

var group = groups[i];

if(group.groupcode.toUpperCase() == "SYSADMIN") {

result = true;

break;

}

}

return result;

}

}

function isReadonly() {

var result = false;

var muserStr=localStorage.getItem("muser");

var muserObj=eval('(' + muserStr + ')');

var groups=muserObj.group;

if(groups && groups.length>0 && groups[0].groupcode.toUpperCase().indexOf("_READ")>0) result = true;

return result;

}

function getLongUsername(){

var muserStr=localStorage.getItem("muser");

var muserObj=eval('(' + muserStr + ')');

var muser=muserObj.user;

return muser.username+"("+muser.lastname+")";

}

//其他⽅法检查版本

function svn(t) {

var xhr_svn = new plus.net.XMLHttpRequest();

xhr_svn.onreadystatechange = function() {

if (xhr_svn.readyState == 4) {

if (xhr_svn.status == 200) {

var res = JSON.parse(xhr_svn.responseText);

if (res.state == 'yes') {

if (res.mark != t) {

var upr;

plus.nativeUI.confirm( "有新版本发布了,是否件更新?", function(e){

upr=(e.index==0)?"Y":"N";

console.log(upr);

if(upr=="Y"){

var wt = plus.nativeUI.showWaiting('下载更新中,请勿关闭');

var url = res.url; // 下载⽂件地址

var dtask = plus.downloader.createDownload(url, {}, function(d, status) {

if (status == 200) { // 下载成功

var path = d.filename;

console.log(d.filename);

plus.runtime.install(path);

} else { //下载失败

alert("Download failed: " + status);

}

});

dtask.start();

}else{

}

}, "XXX系统", ["确认","取消"] );

} else {

console.log('最新');

}

}

} else {

plus.nativeUI.toast( "⽹络连接错误!");

}

}

}

xhr_svn.open("GET", "http:/XXX/APPobject/imes/");//这⾥的地址是上⾯json⽂件的地址

xhr_svn.send();

}

4.服务器端添加配置信息

1.主要添加json(新版本信息),apk(新版本安装包),desc(描述⽂件)