1. 程式人生 > >HTML5進階(三)HBuilder實現軟體自動升級(優化篇)

HTML5進階(三)HBuilder實現軟體自動升級(優化篇)

HBuilder實現軟體自動升級(優化篇)

前言

受前篇部落格《的影響,測試過程中發現APP自動更新還是存在問題,第一次升級沒有任何問題。第二次升級時,若wrt升級包的名字相同,則會出現以下錯誤提示:

 

      估計是HBuilder的BUG導致出現以上錯誤。受社群中盆友們思路的啟發,在每次更新時提交不同檔名的wrt更新包,方可解決以上問題。

      同時在上篇部落格中提到“檢測更新更好的模式應該是客戶端提交本地應用資源版本號到升級伺服器,由升級伺服器判斷是否可更新並且返回App升級資源包下載地址,避免在客戶端寫資源下載地址;”。由此,自己由本地檢測版本更新變更為將本地app版本提交至升級服務端,由服務端判斷app是否可更新,若存在更新版本則返回相應的新版本號及版本下載地址。

      在提交新版本時,應注意版本號的一致性,即wrt版本號、版本號檔案version.txt中的版本號相一致。

客戶端原始碼(拿走不謝)

var wgtVer  = null;
function plusReady(){
// 獲取本地應用資源版本號
plus.runtime.getProperty(plus.runtime.appid,function(inf){
wgtVer = inf.version;
localStorage.setItem('newVer', wgtVer);
console.log(localStorage.getItem('newVer'));
console.log("當前應用版本:" + wgtVer);
/*alert( "國際移動裝置身份碼IMEI: " + plus.device.imei );
alert( "國際移動使用者識別碼IMSI: " + plus.device.imsi );
alert( "裝置唯一標識號uuid: "+plus.device.uuid );*/
console.log("=============版本測試=============");
var version = {
'version': wgtVer
};
appCallServer($http, "9104", version, 
function(data) {
console.log("9104_版本查詢成功" + JSON.stringify(data));
// H5 plus事件處理,彈出提示資訊對話方塊
plus.nativeUI.confirm("\"立馬送藥\"檢測到新版本,是否更新?", function(e) {
if (e.index == 0) {
console.log("確定更新!");
downWgt(data.newVersion, data.url); // 下載升級包
}
}, "                  立馬送藥", ["確定", "取消"]);
}, function(data) {
/*$ionicLoading.show({
template: '測試'
});
$timeout(function() {
$ionicLoading.hide();
}, 1200);*/
});
});
}
if(window.plus){
plusReady();
}else{
document.addEventListener('plusready',plusReady,false);
}
// 下載新版本
function downWgt(newVer, wgtUrl){
plus.nativeUI.showWaiting("下載wgt檔案...");
plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
if ( status == 200 ) { 
console.log("下載wgt成功:"+d.filename);
installWgt(d.filename,newVer);// 安裝wgt包
} else {
console.log("下載wgt失敗!");
plus.nativeUI.alert("下載wgt失敗!");
}
plus.nativeUI.closeWaiting();
}).start();
}
// 更新應用資源
function installWgt(path,newVer){
plus.nativeUI.showWaiting("安裝wgt檔案...");
// force:false進行版本號校驗,如果將要安裝應用的版本號不高於現有應用的版本號則終止安裝,並返回安裝失敗
plus.runtime.install(path,{force:false},function(){
plus.nativeUI.closeWaiting();
console.log("安裝wgt檔案成功!");
localStorage.setItem('newVer', newVer);
// H5 plus事件處理,彈出提示資訊對話方塊
plus.nativeUI.confirm("應用資源更新完成,是否重新開啟應用?", function(e) {
if (e.index == 0) {
console.log("確定重新開啟應用!");
plus.runtime.restart();
}
}, "                  立馬送藥", ["確定", "取消"]);
},function(e){
plus.nativeUI.closeWaiting();
console.log("安裝wgt檔案失敗[" + e.code + "]:" + e.message);
plus.nativeUI.alert("安裝wgt檔案失敗[" + e.code + "]:" + e.message);
});
}

服務端原始碼(拿走不謝)

// check version 
public static boolean do_9104(RequestMessage request,ResponseMessage response) {
 
logger.info("\n\n------------Check_APP_Version_9104 debug info-------------\n請求資料包資訊:" + request.json.toString());
String version = request.getString("version").trim();
String currentVersion = FileUtil.readFile(MyConst.VERSION_FILE_PATH).replaceAll("null","").trim();
logger.info("當前APP版本:[" + currentVersion + "]");
if(!version.isEmpty() && !currentVersion.isEmpty() && (!version.equals(currentVersion))){
response.json.element("newVersion", currentVersion); 	 // 返回最新版本
response.json.element("url", MyConst.WGT_URL + currentVersion + ".wgt"); // 返回wgt檔案下載地址
}else if(!version.isEmpty() && !currentVersion.isEmpty() && (version.equals(currentVersion))){
response.result = MyConst.ERR_VERSION_SAME;
response.errtext = "當前已是最新版本";
}else{
response.result = MyConst.ERR_VERSION;
response.errtext = "版本資訊查詢失敗";
return (false);
}
return true;
}

優化

      在上面的app版本管理中,更新包及版本控制檔案version.txt均需要手動新增、更改,這一體驗令人極為不爽,遂決定進一步優化之~

      為了進一步方便管理人員對版本控制的操作。在管理端進一步增加通過程式增加更新包的功能模組,該功能模組能夠實現更新包的上傳,同時將新的版本號寫入version.txt版本檔案中。

參考文獻

美文美圖