diff --git a/application/controllers/amp.php b/application/controllers/amp.php index 652e5f48..d84d0c0c 100644 --- a/application/controllers/amp.php +++ b/application/controllers/amp.php @@ -114,7 +114,8 @@ class Amp extends CI_Controller { $this->InfoMetas_model->update($ic_id, 'AMP_STATUS', $amp_status); } - echo json_encode(array('name' => 'ok', 'value' => $this->lang->line('form_info_success'))); + $data[] = array('name' => 'ok', 'value' => $this->lang->line('form_info_success')); + echo json_encode($data); } } diff --git a/application/third_party/train/controllers/search.php b/application/third_party/train/controllers/search.php index ee023727..0e217f82 100644 --- a/application/third_party/train/controllers/search.php +++ b/application/third_party/train/controllers/search.php @@ -93,7 +93,7 @@ class search extends CI_Controller{ if($this->differ_time <= 5){ $cache_train_info = NULL; }else{ - $cache_train_info = $this->BIZ_train_model->get_train_info($fromStation,$toStation); + $cache_train_info = $this->BIZ_train_model->get_train_cache($fromStation,$toStation); } //判断缓存是否为空 @@ -206,7 +206,7 @@ class search extends CI_Controller{ $pricestr = $seat_type.'"train_no":'.'"'.$value->train_no.'"'; //余票字符串 $this->can_buy_now = $value->can_buy_now; - $return_data->data->result[$i] = '|预定|'.$value->train_no.'|'.$value->train_code.'|'.$value->from_station_name.'|'.$value->to_station_name.'|'.$value->from_station_code.'|'.$value->to_station_code.'|'.$value->start_time.'|'.$value->arrive_time.'|'.$value->run_time.'|'.$value->can_buy_now.'||'.$value->train_start_date.'||||||||'.$this->ticket_exchange($value->gjrw_price,$value->gjrw_num).'|'.$this->ticket_exchange($value->qtxb_price,$value->qtxb_num).'|'.$this->ticket_exchange($value->rw_price,$value->rw_num).'|'.$this->ticket_exchange($value->rz_price,$value->rz_num).'|'.$this->ticket_exchange($value->tdz_price,$value->tdz_num).'|'.$this->ticket_exchange($value->wz_price,$value->wz_num).'||'.$this->ticket_exchange($value->yw_price,$value->yw_num).'|'.$this->ticket_exchange($value->yz_price,$value->yz_num).'|'.$this->ticket_exchange($value->edz_price,$value->edz_num).'|'.$this->ticket_exchange($value->ydz_price,$value->ydz_num).'|'.$this->ticket_exchange($value->swz_price,$value->swz_num).'|'.$this->ticket_exchange($value->dw_price,$value->dw_num).'||'; + $return_data->data->result[$i] = '|预定|'.$value->train_no.'|'.$value->train_code.'|'.$value->from_station_name.'|'.$value->to_station_name.'|'.$value->from_station_code.'|'.$value->to_station_code.'|'.$value->start_time.'|'.$value->arrive_time.'|'.$value->run_time.'|'.$value->can_buy_now.'||'.$this->train_date.'||||||||'.$this->ticket_exchange($value->gjrw_price,$value->gjrw_num).'|'.$this->ticket_exchange($value->qtxb_price,$value->qtxb_num).'|'.$this->ticket_exchange($value->rw_price,$value->rw_num).'|'.$this->ticket_exchange($value->rz_price,$value->rz_num).'|'.$this->ticket_exchange($value->tdz_price,$value->tdz_num).'|'.$this->ticket_exchange($value->wz_price,$value->wz_num).'||'.$this->ticket_exchange($value->yw_price,$value->yw_num).'|'.$this->ticket_exchange($value->yz_price,$value->yz_num).'|'.$this->ticket_exchange($value->edz_price,$value->edz_num).'|'.$this->ticket_exchange($value->ydz_price,$value->ydz_num).'|'.$this->ticket_exchange($value->swz_price,$value->swz_num).'|'.$this->ticket_exchange($value->dw_price,$value->dw_num).'||'; $data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}'; $return_data->data->price[$i] = $data; $i++; diff --git a/application/third_party/train/models/BIZ_train_model.php b/application/third_party/train/models/BIZ_train_model.php index ebad9cd0..3cebc984 100644 --- a/application/third_party/train/models/BIZ_train_model.php +++ b/application/third_party/train/models/BIZ_train_model.php @@ -8,7 +8,7 @@ class BIZ_train_model extends CI_Model { $this->INFO = $this->load->database('INFO', TRUE); } - //新增或更新价格 + //新增或更新缓存 public function addOrUpdate($tpc_from_station,$tpc_to_station,$tpc_content){ $sql = "IF NOT EXISTS( SELECT 1 @@ -49,7 +49,7 @@ class BIZ_train_model extends CI_Model { //获取缓存的火车信息 //如果读取到缓存是7天以前的数据就不返回任何数据,并且将其删除。 - public function get_train_info($tpc_from_station,$tpc_to_station){ + public function get_train_cache($tpc_from_station,$tpc_to_station){ $sql = "SELECT * FROM diff --git a/application/third_party/tuniu/controllers/tuniu_train.php b/application/third_party/tuniu/controllers/tuniu_train.php index cf81e430..7a254903 100644 --- a/application/third_party/tuniu/controllers/tuniu_train.php +++ b/application/third_party/tuniu/controllers/tuniu_train.php @@ -40,123 +40,162 @@ class Tuniu_train extends CI_Controller{ if(!$date || !$fromStation || !$toStation){ exit('传参错误!'); }else{ + //做超过预售期的处理查询 + $isCache = false; + $iseven = false; + $now_time = time(); + $differ = (strtotime($date) - $now_time) / 86400; + if($differ > 29){ + $date = date('Y-m-d',strtotime('+7day')); + $iseven = true; + } + + //途牛报文拼接 $sign = $this->create_sign(); - $url = TUNIU_URL.'/train/search'; - $time = date('Y-m-d H:i:s',time()); - $post_data = '{ - "apiKey": "'.TUNIU_KEY.'", - "sign": "'.$sign.'", - "timestamp": "'.$time.'", - "data": { "trainDate": "'.$date.'", "fromStation": "'.$fromStation.'", "toStation": "'.$toStation.'", "trainCode": "" } - }'; - $back_data = post_tuniu($url,$post_data,$method = 'POST'); - if(empty(json_decode($back_data)->data)){ - log_message('error','tuniu:'.json_encode($post_data)); - } - //print_r(json_decode($back_data)); + $url = TUNIU_URL.'/train/search'; + $time = date('Y-m-d H:i:s',time()); + $post_data = '{ + "apiKey": "'.TUNIU_KEY.'", + "sign": "'.$sign.'", + "timestamp": "'.$time.'", + "data": { "trainDate": "'.$date.'", "fromStation": "'.$fromStation.'", "toStation": "'.$toStation.'", "trainCode": "" } + }'; + $back_data = post_tuniu($url,$post_data,$method = 'POST'); - $return_data = new stdClass(); - $return_data->returnCode = json_decode($back_data)->returnCode; - //echo $return_data->returnCode; - $return_data->httpstatus = 200; - $return_data->data = new stdClass(); - $return_data->data->result = array(); - $return_data->data->map = new stdClass(); - $obj = array(); - $i = 0; - $pricestr = ''; - if(!empty(json_decode($back_data)->data)){ - foreach (json_decode($back_data)->data as $value){ - $obj[$value->fromStationCode] = $value->fromStationName; - $obj[$value->toStationCode] = $value->toStationName; - $seat_type = ''; - if(isset($value->gjrwXiaPrice)){ - if(is_numeric($value->gjrwXiaPrice)){ - $gjrwXiaPrice = $value->gjrwXiaPrice * 10; - $seat_type .= '"6":"'.$gjrwXiaPrice.'","A6":"¥'.$value->gjrwXiaPrice.'",'; + //如果数据为空则调用缓存,有数据则进行数据存储 + if(empty($back_data)){ + log_message('error','tuniutrain 接口未响应|数据源:'.$fromStation.'|'.$toStation.'|'.$date); + $trainCache = $this->tuniuprice_model->get_train_cache($fromStation,$toStation); + $back_data = $trainCache->tpc_content; + $isCache = true; + }else{ + if(empty(json_decode($back_data)->data)){ + $error = json_decode($back_data); + $trainCache = $this->tuniuprice_model->get_train_cache($fromStation,$toStation); + if(!empty($trainCache)){ + $back_data = $trainCache->tpc_content; + $isCache = true; + log_message('error','tuniutrain 查询出错|'.$error->errorMsg); + }else{ + $back_data = null; + log_message('error','tuniutrain 缓存为空|'.$fromStation.'|'.$toStation.'|'.$date); } + + }else{ + $this->tuniuprice_model->addOrUpdate($fromStation,$toStation,$back_data); } - if(isset($value->qtxbPrice)){ - if(is_numeric($value->qtxbPrice)){ - $seat_type .= '"H":"¥'.$value->qtxbPrice.'",'; + } + + //途牛数据格式转换 + $return_data = new stdClass(); + if(empty($back_data)){ + $return_data->returnCode = '404'; + }else{ + $return_data->returnCode = json_decode($back_data)->returnCode; + } + + $return_data->httpstatus = 200; + $return_data->data = new stdClass(); + $return_data->data->seven = $iseven; + $return_data->data->cache = $isCache; + $return_data->data->result = array(); + $return_data->data->map = new stdClass(); + + $obj = array(); + $i = 0; + $pricestr = ''; + if(!empty(json_decode($back_data)->data)){ + foreach (json_decode($back_data)->data as $value){ + $obj[$value->fromStationCode] = $value->fromStationName; + $obj[$value->toStationCode] = $value->toStationName; + $seat_type = ''; + if(isset($value->gjrwXiaPrice)){ + if(is_numeric($value->gjrwXiaPrice)){ + $gjrwXiaPrice = $value->gjrwXiaPrice * 10; + $seat_type .= '"6":"'.$gjrwXiaPrice.'","A6":"¥'.$value->gjrwXiaPrice.'",'; + } } - } - if(isset($value->rwPrice)){ - if(is_numeric($value->rwPrice)){ - $rwPrice = $value->rwPrice * 10; - $seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rwPrice.'",'; + if(isset($value->qtxbPrice)){ + if(is_numeric($value->qtxbPrice)){ + $seat_type .= '"H":"¥'.$value->qtxbPrice.'",'; + } } - } - if(isset($value->rzPrice)){ - if(is_numeric($value->rzPrice)){ - $rzPrice = $value->rzPrice * 10; - $seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rzPrice.'",'; + if(isset($value->rwPrice)){ + if(is_numeric($value->rwPrice)){ + $rwPrice = $value->rwPrice * 10; + $seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rwPrice.'",'; + } } - } - if(isset($value->tdzPrice)){ - if(is_numeric($value->tdzPrice)){ - $seat_type .= '"P":"¥'.$value->tdzPrice.'",'; + if(isset($value->rzPrice)){ + if(is_numeric($value->rzPrice)){ + $rzPrice = $value->rzPrice * 10; + $seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rzPrice.'",'; + } } - } - if(isset($value->wzPrice)){ - if(is_numeric($value->wzPrice)){ - $wzPrice = $value->wzPrice * 10; - $seat_type .= '"WZ":"¥'.$value->wzPrice.'",'; + if(isset($value->tdzPrice)){ + if(is_numeric($value->tdzPrice)){ + $seat_type .= '"P":"¥'.$value->tdzPrice.'",'; + } } - } - if(isset($value->ywPrice)){ - if(is_numeric($value->ywPrice)){ - $ywPrice = $value->ywPrice * 10; - $seat_type .= '"3":"'.$ywPrice.'","A3":"¥'.$value->ywPrice.'",'; + if(isset($value->wzPrice)){ + if(is_numeric($value->wzPrice)){ + $wzPrice = $value->wzPrice * 10; + $seat_type .= '"WZ":"¥'.$value->wzPrice.'",'; + } } - } - if(isset($value->yzPrice)){ - if(is_numeric($value->yzPrice)){ - $yzPrice = $value->yzPrice * 10; - $seat_type .= '"1":"'.$yzPrice.'","A1":"¥'.$value->yzPrice.'",'; + if(isset($value->ywPrice)){ + if(is_numeric($value->ywPrice)){ + $ywPrice = $value->ywPrice * 10; + $seat_type .= '"3":"'.$ywPrice.'","A3":"¥'.$value->ywPrice.'",'; + } } - } - if(isset($value->edzPrice)){ - if(is_numeric($value->edzPrice)){ - $seat_type .= '"O":"¥'.$value->edzPrice.'",'; + if(isset($value->yzPrice)){ + if(is_numeric($value->yzPrice)){ + $yzPrice = $value->yzPrice * 10; + $seat_type .= '"1":"'.$yzPrice.'","A1":"¥'.$value->yzPrice.'",'; + } } - } - if(isset($value->ydzPrice)){ - if(is_numeric($value->ydzPrice)){ - $seat_type .= '"M":"¥'.$value->ydzPrice.'",'; + if(isset($value->edzPrice)){ + if(is_numeric($value->edzPrice)){ + $seat_type .= '"O":"¥'.$value->edzPrice.'",'; + } } - } - if(isset($value->swzPrice)){ - if(is_numeric($value->swzPrice)){ - $swzPrice = $value->swzPrice * 10; - $seat_type .= '"9":"'.$swzPrice.'","A9":"¥'.$value->swzPrice.'",'; + if(isset($value->ydzPrice)){ + if(is_numeric($value->ydzPrice)){ + $seat_type .= '"M":"¥'.$value->ydzPrice.'",'; + } } + if(isset($value->swzPrice)){ + if(is_numeric($value->swzPrice)){ + $swzPrice = $value->swzPrice * 10; + $seat_type .= '"9":"'.$swzPrice.'","A9":"¥'.$value->swzPrice.'",'; + } + } + $pricestr = $seat_type.'"train_no":'.'"'.$value->trainNo.'"'; + + //对返回的数据进行容错处理 + $value->gjrwNum = isset($value->gjrwNum) ? ticket_exchange($value->gjrwNum,$iseven) : ''; + $value->rwNum = isset($value->rwNum) ? ticket_exchange($value->rwNum,$iseven) : ''; + $value->rzNum = isset($value->rzNum)?ticket_exchange($value->rzNum,$iseven) : ''; + $value->tdzNum = isset($value->tdzNum)?ticket_exchange($value->tdzNum,$iseven) : ''; + $value->wzNum = isset($value->wzNum)?ticket_exchange($value->wzNum,$iseven) : ''; + $value->ywNum = isset($value->ywNum)?ticket_exchange($value->ywNum,$iseven) : ''; + $value->yzNum = isset($value->yzNum)?ticket_exchange($value->yzNum,$iseven) : ''; + $value->edzNum = isset($value->edzNum)?ticket_exchange($value->edzNum,$iseven) : ''; + $value->ydzNum = isset($value->ydzNum)?ticket_exchange($value->ydzNum,$iseven) : ''; + $value->swzNum = isset($value->swzNum)?ticket_exchange($value->swzNum,$iseven) : ''; + $value->dwNum = isset($value->dwNum)?ticket_exchange($value->dwNum,$iseven) : ''; + $value->runTime = isset($value->runTime)?$value->runTime : ''; + + //余票字符串 + $return_data->data->result[$i] = '|预定|'.$value->trainNo.'|'.$value->trainCode.'|'.$value->fromStationName.'|'.$value->toStationName.'|'.$value->fromStationCode.'|'.$value->toStationCode.'|'.$value->startTime.'|'.$value->arriveTime.'|'.$value->runTime.'|'.$value->canBuyNow.'||'.date('Ymd',strtotime($value->trainStartDate)).'||||||||'.$value->gjrwNum.'||'.$value->rwNum.'|'.$value->rzNum.'|'.$value->tdzNum.'|'.$value->wzNum.'||'.$value->ywNum.'|'.$value->yzNum.'|'.$value->edzNum.'|'.$value->ydzNum.'|'.$value->swzNum.'|'.$value->dwNum.'||'; + $data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}'; + $return_data->data->price[$i] = $data; + $i++; } - $pricestr = $seat_type.'"train_no":'.'"'.$value->trainNo.'"'; - - //对余票数进行验证 - $value->gjrwNum = isset($value->gjrwNum) ? ticket_exchange($value->gjrwNum) : ''; - $value->rwNum = isset($value->rwNum) ? ticket_exchange($value->rwNum) : ''; - $value->rzNum = isset($value->rzNum)?ticket_exchange($value->rzNum) : ''; - $value->tdzNum = isset($value->tdzNum)?ticket_exchange($value->tdzNum) : ''; - $value->wzNum = isset($value->wzNum)?ticket_exchange($value->wzNum) : ''; - $value->ywNum = isset($value->ywNum)?ticket_exchange($value->ywNum) : ''; - $value->yzNum = isset($value->yzNum)?ticket_exchange($value->yzNum) : ''; - $value->edzNum = isset($value->edzNum)?ticket_exchange($value->edzNum) : ''; - $value->ydzNum = isset($value->ydzNum)?ticket_exchange($value->ydzNum) : ''; - $value->swzNum = isset($value->swzNum)?ticket_exchange($value->swzNum) : ''; - $value->dwNum = isset($value->dwNum)?ticket_exchange($value->dwNum) : ''; - - //余票字符串 - $return_data->data->result[$i] = '|预定|'.$value->trainNo.'|'.$value->trainCode.'|'.$value->fromStationName.'|'.$value->toStationName.'|'.$value->fromStationCode.'|'.$value->toStationCode.'|'.$value->startTime.'|'.$value->arriveTime.'|'.$value->runTime.'|'.$value->canBuyNow.'||'.date('Ymd',strtotime($value->trainStartDate)).'||||||||'.$value->gjrwNum.'||'.$value->rwNum.'|'.$value->rzNum.'|'.$value->tdzNum.'|'.$value->wzNum.'||'.$value->ywNum.'|'.$value->yzNum.'|'.$value->edzNum.'|'.$value->ydzNum.'|'.$value->swzNum.'|'.$value->dwNum.'||'; - $data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}'; - $return_data->data->price[$i] = $data; - $this->tuniuprice_model->addOrUpdate($value->trainNo,$value->trainCode,$value->fromStationCode,$value->toStationCode,json_encode($data)); - $i++; } - } - $return_data->data->map = (object)$obj; - print_r(json_encode($return_data)); + $return_data->data->map = (object)$obj; + print_r(json_encode($return_data)); } } diff --git a/application/third_party/tuniu/helpers/tuniu_helper.php b/application/third_party/tuniu/helpers/tuniu_helper.php index 1b5e1fa9..822429e6 100644 --- a/application/third_party/tuniu/helpers/tuniu_helper.php +++ b/application/third_party/tuniu/helpers/tuniu_helper.php @@ -14,16 +14,20 @@ } //余票转换 - function ticket_exchange($num){ - if(is_numeric($num)){ - if($num == 0){ - return '无'; - }elseif($num == 99){ - return '有'; - }else{ - return $num; - } - } + function ticket_exchange($num,$iseven){ + if($iseven){ + return '有'; + }else{ + if(is_numeric($num)){ + if($num == 0){ + return '无'; + }elseif($num >= 99){ + return '有'; + }else{ + return $num; + } + } + } } //发送请求函数 diff --git a/application/third_party/tuniu/models/tuniuprice_model.php b/application/third_party/tuniu/models/tuniuprice_model.php index c738b903..404ad70e 100644 --- a/application/third_party/tuniu/models/tuniuprice_model.php +++ b/application/third_party/tuniu/models/tuniuprice_model.php @@ -6,47 +6,60 @@ class tuniuprice_model extends CI_Model { $this->INFO = $this->load->database('INFO', TRUE); } - //¼۸ - public function addOrUpdate($TPL_Train_No,$TPL_Train_Code,$TPL_From_Station_Code,$TPL_To_Station_Code,$TPL_Price){ + //» + public function addOrUpdate($tpc_from_station,$tpc_to_station,$tpc_content){ $sql = "IF NOT EXISTS( SELECT 1 - FROM TrainPriceList - WHERE TPL_Train_Code = '$TPL_Train_Code' - AND TPL_From_Station_Code = '$TPL_From_Station_Code' - AND TPL_To_Station_Code = '$TPL_To_Station_Code' + FROM TrainPriceCache + WHERE + tpc_from_station = '$tpc_from_station' + AND tpc_to_station = '$tpc_to_station' ) BEGIN - INSERT INTO TrainPriceList + INSERT INTO TrainPriceCache ( - TPL_Train_No, - TPL_Train_Code, - TPL_From_Station_Code, - TPL_To_Station_Code, - TPL_Price, - TPL_Datetime, - TPL_Source + tpc_from_station, + tpc_to_station, + tpc_content, + tpc_datetime, + tpc_source ) VALUES ( - '$TPL_Train_No','$TPL_Train_Code','$TPL_From_Station_Code','$TPL_To_Station_Code','$TPL_Price',GETDATE(),'tuniu' + '$tpc_from_station','$tpc_to_station','$tpc_content',GETDATE(),'tuniu' ) END ELSE BEGIN - UPDATE TrainPriceList - SET - TPL_Train_No = '$TPL_Train_No', - TPL_Price = '$TPL_Price', - TPL_Datetime = GETDATE(), - TPL_Source = 'tuniu' - WHERE TPL_Train_Code = '$TPL_Train_Code' - AND TPL_From_Station_Code = '$TPL_From_Station_Code' - AND TPL_To_Station_Code = '$TPL_To_Station_Code' + UPDATE TrainPriceCache + SET tpc_from_station = '$tpc_from_station', + tpc_to_station = '$tpc_to_station', + tpc_content = '$tpc_content', + tpc_datetime = GETDATE(), + tpc_source = 'tuniu' + WHERE + tpc_from_station = '$tpc_from_station' + AND tpc_to_station = '$tpc_to_station' END "; $query = $this->INFO->query($sql); return $query; } + //ȡĻϢ + //ȡ7ǰݾͲκݣҽɾ + public function get_train_cache($tpc_from_station,$tpc_to_station){ + $sql = "SELECT + * + FROM + TrainPriceCache + WHERE + tpc_from_station = '$tpc_from_station' + AND + tpc_to_station = '$tpc_to_station'"; + $query = $this->INFO->query($sql); + return $query->row(); + } + //ȡ۸ public function get_price($fromStationCode,$toStationCode,$trainCode){ $sql = "SELECT