@ -11,7 +11,12 @@ class search extends CI_Controller{
//查询聚合余票接口,对返回的数据进行处理
public function index($train_date=null,$fromStation=null,$toStation=null){
//定义一些变量
$flag = true;
$juheTrainInfo = '';
set_time_limit(0);
//判断数据是否合法
if(!$train_date || !$fromStation || !$toStation){
exit('传参不能为空!');
}else{
@ -21,48 +26,82 @@ class search extends CI_Controller{
if(strlen($toStation) != 3){
exit('toStation:参数错误!');
}
$this->train_date = $train_date;
//$url = 'http://op.juhe.cn/trainTickets/ticketsAvailable?key='.JUHE_TRAIN_API_KEY.'&train_date='.$train_date.'&from_station='.$fromStation.'&to_station='.$toStation;
$url = 'http://139.129.246.118:12309/trainTickets/ticketsAvailable?key='.JUHE_TRAIN_API_KEY.'&train_date='.$train_date.'&from_station='.$fromStation.'&to_station='.$toStation;
$back_data = $this->get_http($url);
//特殊车次处理(贵阳 -- 重庆)
/*if(($fromStation == 'GIW') & & ($toStation == 'CQW') & & (strtotime($train_date) - strtotime('2018-01-16') >= 0)){
exit('{"httpstatus":200,"reason":"特殊车次不开车","data":{"result":""}}');
}*/
$firstdata = $back_data;
$return_data = new stdClass();
$cache = 'no';
if(empty($back_data) || empty(json_decode($back_data)->result)){
$obj = $this->BIZ_train_model->get_train_info($fromStation,$toStation);
if($obj){
$back_data = $obj->tpc_content;
$cache = 'yes';
$this->cache = true;
$reason = '接口数据为空,调用缓存';
log_message('error','查询为空的链接:'.$url.'|出错的原因:'.$firstdata.'|调用缓存号:'.$obj->tpc_sn.'|缓存生成的时间:'.$obj->tpc_datetime);
//调用查询模块
$search_return = $this->Searchtrain($train_date,$fromStation,$toStation);
//调用数据处理模块
$returnJson = $this->dataOperate($search_return,$fromStation,$toStation);
//调用拼接处理模块
$trainjson = $this->createTrainJson($returnJson);
print_r($trainjson);
}
}
//搜索模块
function Searchtrain($train_date,$fromStation,$toStation){
$this->train_date = $train_date;
$now_time = time();
$differ_time = (strtotime($train_date) - $now_time) / 86400;
//屏蔽掉超过预售期的搜索,提高速度
if($differ_time > 29){
$train_date = date('Y-m-d',strtotime('+7day'));
$this->seventh = true;
}
$url = 'http://139.129.246.118:12309/trainTickets/ticketsAvailable?key='.JUHE_TRAIN_API_KEY.'&train_date='.$train_date.'&from_station='.$fromStation.'&to_station='.$toStation;
$this->url = $url;
$train_info = $this->get_http($url);
//如果为网络错误就再执行一次
if(json_decode($train_info)->reason == '网络故障, 请重试( 0) '){
$train_info = $this->get_http($url);
}
return $train_info;
}
//缓存处理模块(包含缓存处理)
function dataOperate($search_return,$fromStation,$toStation){
$this->reason = json_decode($search_return)->reason;
$this->cache = 'no';
if(!empty($search_return) & & !empty(json_decode($search_return)->result)){
$this->BIZ_train_model->addOrUpdate($fromStation,$toStation,$search_return);
$operate_data = $search_return;
}else{
$cache_train_info = $this->BIZ_train_model->get_train_info($fromStation,$toStation);
if(empty($cache_train_info)){
$operate_data = null;
}else{
$cache_time = $cache_train_info->tpc_datetime;
$now_time = time();
$differ_time = ($now_time - strtotime($cache_time)) / 86400;
if($differ_time >= 3){
$this->delete_traincache($fromStation,$toStation);
}else{
$reason = '接口数据为空,缓存也为空';
log_message('error','查询为空的链接:'.$url.'|出错的原因:'.$firstdata.'|缓存为空');
$this->cache = 'yes';
$operate_data = $cache_train_info->tpc_content;
log_message('error','查询为空的链接:'.$this->url.'|出错的原因:'.$this->reason.'|调用缓存号:'.$cache_train_info->tpc_sn.'|缓存生成的时间:'.$cache_train_info->tpc_datetime);
}
}else{
$reason = json_decode($back_data)->reason;
$this->BIZ_train_model->addOrUpdate($fromStation,$toStation,$back_data);
}
}
return $operate_data;
}
$return_data->httpstatus = 200;
$return_data->reason = $reason;
$return_data->cache = $cache;
$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)->result->list)){
foreach (json_decode($back_data)->result->list as $value){
//字符串拼接模块
function createTrainJson($returnjson){
$return_data = new stdClass();
$return_data->data = new stdClass();
$return_data->httpstatus = 200;
$return_data->reason = $this->reason;
$return_data->cache = $this->cache;
$return_data->data->result = array();
$return_data->data->map = new stdClass();
$obj = array();
$i = 0;
$pricestr = '';
if(!empty($returnjson)){
if(!empty(json_decode($returnjson)->result->list)){
foreach (json_decode($returnjson)->result->list as $value){
$obj[$value->from_station_code] = $value->from_station_name;
$obj[$value->to_station_code] = $value->to_station_name;
$seat_type = '';
@ -141,12 +180,14 @@ class search extends CI_Controller{
$i++;
}
}
$return_data->data->map = (object)$obj;
print_r(json_encode($return_data));
}
}
$return_data->data->map = (object)$obj;
return json_encode($return_data);
}
//获取价格
//获取价格(废弃)
/*
fromStationCode: 出发站三字码
toStationCode:终点站三字码
@ -167,7 +208,7 @@ class search extends CI_Controller{
//余票转换
function ticket_exchange($num){
if(isset($this->cache) ){
if($this->cache == 'yes' ){
$time = strtotime($this->train_date) - time();
$day = $time / 86400;
if($day > 15){