1. 程式人生 > >APP版本自動更新

APP版本自動更新

APP版本自動更新

前端介面呼叫獲取伺服器專案版本號

$.ajax({
						type: 'POST',
						url: url + 'api/user/get_version',
						data: '',
						dataType: 'json',
						timeout: 3000,
						async: false,
						success: function(data) {
							//update_ksd(data.data)
							 if(data.data.user_version === data.data.version){
	           	   	  	           return false;
			           	   	  }else{
					           	   	  	var vv = ismobile();console.log(vv)
					           	   	  	if(vv == 1){
					           	   	  console.log(data.data.apk)
					           	   	         update_ksd(data.data.apk)
					           	   	  	}else{
					           	   	  console.log(data.data.ios)
					           	   	         update_ksd(data.data.ios)
					           	   	  	}
			           	
			           	   	  }
						}
					})

php獲取版本號介面

config.php配置檔案

  'VERSION'=>
            [
                'apk'=> ['update'=>true,'version'=>'2.0.6','url'=>'http://xiaodishen.ekeli.cn/uploads/H5C63E0E6_0921094109.apk'],

                'ios'=> ['update'=>true,'version'=>'2.0.6','url'=>'itms-services://?action=download-manifest&url=https://xiaodishen.ekeli.cn/uploads/manifest.plist']
            ]
 //獲取版本號
    public function get_version(){
        $data = config('VERSION');
        $data['version'] = $data['apk']['version'];
        $data['update'] = $data['apk']['update'];
        $data['url'] = $data['apk']['url'];
        $this->response('查詢成功',200,$data);
    }

判斷平臺

/**
 * [isMobile 判斷平臺]
 * @param test: 0:iPhone    1:Android
 */
function ismobile(){
    var u = navigator.userAgent, app = navigator.appVersion;
    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
     if(window.location.href.indexOf("?mobile")<0){
      try{
       if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){
        return '0';
       }else{
        return '1';
       }
      }catch(e){}
     }
    }else if( u.indexOf('iPad') > -1){
        return '0';
    }else{
        return '1';
    }
};

對比版本號,確認是否更新

function update_ksd(data) //跨域請求後的回撥函式
					{
						var new_json = data;
						if(new_json.update) //如果是自動更新,則判斷版本號
						{
							$.ajax({
								type: "get",
								url: "manifest.json",
								async: true,
								data: '',
								async: false,
								dataType: 'text',
								success: function(res) {
									var data = res.split('version');
									data = data[1].split('}');
									data = data[0].split('{');
									data = data[1].split('name');
									data = data[1].split(',');
									console.log(data)
									var r = /\d+/g;
									data = data[0].match(r);
									version = data.join('')
									var new_version = new_json.version.replace(/\./g, '');
									localStorage.setItem('version', data.join('.'));
									console.log(new_version + '新版本' + version);
									if(new_version > version) //比對版本號
									{

										mui.plusReady(function() {
											plus.nativeUI.confirm("應用版本已更新,請下載更新", function(event) {

												if(event.index == 1) {

													plus.nativeUI.showWaiting('更新中,請等待...');
													download(new_json.url); //更新函式,在下面
													var UIApplication = plus.ios.import("UIApplication");
													var sharedApplication = UIApplication.sharedApplication();
													var NSURL = plus.ios.import("NSURL");
													sharedApplication.openURL(NSURL.URLWithString('http://qkl.chs518.com/download/download.html'));
													plus.ios.deleteObject(sharedApplication);
												}
											}, '', ['取消', '確認']);
										});

									}
								},
								error: function() {

								}
							});
						}
					}

下載安裝

function download(url) {
						//console.log(url);
						//建立下載管理物件
						var dtask = plus.downloader.createDownload(url, {}, function(d, status) {
							// 下載完成
							if(status == 200) { //下載成功後的回撥函式
								plus.nativeUI.toast("下載成功,準備安裝" + d.filename);
								//console.log(d.filename)
								//安裝程式,第一個引數是路徑,預設的下載路徑在_downloads裡面
								plus.runtime.install(d.filename, {}, function() {
									localStorage.removeItem('uid');
									plus.nativeUI.toast('安裝成功');
								}, function() {
									plus.nativeUI.toast('安裝失敗');
								});
								plus.nativeUI.closeWaiting();
							} else {
								alert("下載失敗 " + status);

							}
						});
						//dtask.addEventListener( "statechanged", onStateChanged, false );
						dtask.start(); //開始下載任務
					}