H5 App實現熱更新,不需要重新安裝app

直接上代碼吧,你竟然搜到了我的文章就應該知道了,爲什麼要熱更新

//app熱更新下載
//假定字符串的每節數都在5位以下
function toNum(a) {
	//也可以這樣寫 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;
}
 
var btn = ["確定升級", "取消"];
//獲取app系統更新[是否手動點擊獲取更新]
function appUpdate(Index) {
	console.log('appUpdate');
	mui.plusReady(function() {
		plus.runtime.getProperty(plus.runtime.appid, function(inf) {
			ver = inf.version + '';
			console.log('ver:' + ver);
			var client;
			var ua = navigator.userAgent.toLowerCase();
			if(/iphone|ipad|ipod/.test(ua)) { //蘋果手機            
				mui.ajax({
					type: "get",
					dataType: 'json',
					url: "https://itunes.apple.com/lookup?id=1462614850", //獲取當前上架APPStore版本信息
					data: {
						id: 1462614850 //APP唯一標識ID
					},
					contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
					success: function(data) {
						console.log('data:' + JSON.stringify(data));
						var resultCount = data.resultCount;
						for(var i = 0; i < resultCount; i++) {
							var normItem = data.results[i].version;
							console.log('normItem:' + normItem)
							if(normItem > ver) {
								var _msg = "發現新版本:V" + normItem;
								//plus.nativeUI.alert("發現新版本:V" + normItem);
								mui.confirm(_msg, '升級確認', btn, function(e) {
									if(e.index == 0) { //執行升級操作
										document.location.href = 'https://itunes.apple.com/cn/app/%E5%AD%A9%E5%84%BF%E6%AC%A2/id1462614850?mt=8'; //上新APPStore下載地址
									}
								});
								return;
							}
						}
						if(ismanual) {
							mui.toast('當前版本號已是最新');
						}
						return;
					}
				});
			} else if(/android/.test(ua)) {
				mui.ajax(ip + "APIVApp/SelectVApp", {
					data: {
						apkVersion: ver,
					},
					dataType: 'json',
					type: 'get',
					timeout: 10000,
					success: function(data) {
						console.log('data:' + JSON.stringify(data))
						console.log(toNum(ver))
						if(toNum(data[0]._vname) > toNum(ver)) {
							var _msg = "發現新版本:V" + data[0]._vname;
							mui.confirm(_msg, '升級確認', btn, function(e) {
								if(e.index == 0) { //執行升級操作
									downWgt();
								}
							});
						} else {
							console.log(Index);
							if(Index) {
								mui.toast('當前版本號已是最新');
							}
							return;
						}
					},
					error: function(xhr, type, errerThrown) {
						mui.toast('網絡異常,請稍候再試');
					}
				});
			}
		});
	});
}
 
// 下載wgt文件
function downWgt() {
	var wgtUrl = ip + "app/H5750CDB5.wgt";
	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();    
		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);    
		if(e.code == 10) {    
			alert('請清除臨時目錄');    
		}  
	});
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章