From c954aae369a5db01eefffc8fced66f7881d28765 Mon Sep 17 00:00:00 2001 From: cyc Date: Mon, 4 Sep 2017 13:52:16 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E9=80=94=E7=89=9B=E4=BD=99?= =?UTF-8?q?=E7=A5=A8=E6=9F=A5=E8=AF=A2=E5=92=8C=E4=BB=B7=E6=A0=BC=E7=9A=84?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E6=95=B0=E6=8D=AE=202.=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=81=9A=E5=90=88=E4=BD=99=E7=A5=A8=E5=92=8C=E4=BB=B7=E6=A0=BC?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../third_party/train/controllers/search.php | 163 ++++++++++++++++++ .../train/models/BIZ_train_model.php | 59 ++++++- .../tuniu/controllers/tuniu_train.php | 5 +- .../tuniu/models/tuniuprice_model.php | 60 ++++--- 4 files changed, 253 insertions(+), 34 deletions(-) create mode 100644 application/third_party/train/controllers/search.php diff --git a/application/third_party/train/controllers/search.php b/application/third_party/train/controllers/search.php new file mode 100644 index 00000000..c4204461 --- /dev/null +++ b/application/third_party/train/controllers/search.php @@ -0,0 +1,163 @@ +load->model("BIZ_train_model");//加载模型 + } + + public function index($train_date,$fromStation,$toStation){ + $url = 'http://op.juhe.cn/trainTickets/ticketsAvailable?key='.JUHE_TRAIN_API_KEY.'&train_date='.$train_date.'&from_station='.$fromStation.'&to_station='.$toStation; + $back_data = $this->get_http($url); + //print_r(json_decode($back_data)); + $return_data = new stdClass(); + $return_data->httpstatus = 200; + $return_data->data = new stdClass(); + $return_data->data->result = array(); + $i = 0; + $pricestr = ''; + if(!empty(json_decode($back_data)->result->list)){ + foreach (json_decode($back_data)->result->list as $value){ + $seat_type = ''; + if(isset($value->gjrw_price)){ + if($value->gjrw_price > 0){ + $gjrwPrice = $value->gjrw_price * 10; + $seat_type .= '"6":"'.$gjrwPrice.'","A6":"¥'.$value->gjrw_price.'",'; + } + } + if(isset($value->qtxb_price)){ + if($value->qtxb_price > 0){ + $seat_type .= '"H":"¥'.$value->qtxb_price.'",'; + } + } + if(isset($value->rw_price)){ + if($value->rw_price > 0){ + $rwPrice = $value->rw_price * 10; + $seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rw_price.'",'; + } + } + if(isset($value->rz_price)){ + if($value->rz_price > 0){ + $rzPrice = $value->rz_price * 10; + $seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rz_price.'",'; + } + } + if(isset($value->tdz_price)){ + if($value->tdz_price > 0){ + $seat_type .= '"P":"¥'.$value->tdz_price.'",'; + } + } + if(isset($value->wz_price)){ + if($value->wz_price > 0){ + $wzPrice = $value->wz_price * 10; + $seat_type .= '"WZ":"¥'.$value->wz_price.'",'; + } + } + if(isset($value->yw_price)){ + if($value->yw_price > 0){ + $ywPrice = $value->yw_price * 10; + $seat_type .= '"A3":"'.$ywPrice.'","3":"¥'.$value->yw_price.'",'; + } + } + if(isset($value->yz_price)){ + if($value->yz_price > 0){ + $yzPrice = $value->yz_price * 10; + $seat_type .= '"A1":"'.$yzPrice.'","1":"¥'.$value->yz_price.'",'; + } + } + if(isset($value->edz_price)){ + if($value->edz_price > 0){ + $seat_type .= '"O":"¥'.$value->edz_price.'",'; + } + } + if(isset($value->ydz_price)){ + if($value->ydz_price > 0){ + $seat_type .= '"M":"¥'.$value->ydz_price.'",'; + } + } + if(isset($value->swz_price)){ + if($value->swz_price > 0){ + $swzPrice = $value->swz_price * 10; + $seat_type .= '"A9":"'.$swzPrice.'","9":"¥'.$value->swz_price.'",'; + } + } + $pricestr = $seat_type.'"train_no":'.'"'.$value->train_no.'"'; + //余票字符串 + $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_num).'|'.$this->ticket_exchange($value->qtxb_num).'|'.$this->ticket_exchange($value->rw_num).'|'.$this->ticket_exchange($value->rz_num).'|'.$this->ticket_exchange($value->tdz_num).'|'.$this->ticket_exchange($value->wz_num).'||'.$this->ticket_exchange($value->yw_num).'|'.$this->ticket_exchange($value->yz_num).'|'.$this->ticket_exchange($value->edz_num).'|'.$this->ticket_exchange($value->ydz_num).'|'.$this->ticket_exchange($value->swz_num).'|'.$this->ticket_exchange($value->dw_num).'||'; + $i++; + $data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}'; + $this->BIZ_train_model->addOrUpdate($value->train_no,$value->train_code,$value->from_station_code,$value->to_station_code,json_encode($data)); + } + } + //print_r($pricestr); + print_r(json_encode($return_data)); + } + + //获取价格 + /* + fromStationCode:出发站三字码 + toStationCode:终点站三字码 + trainCode:车次号 + */ + public function get_price($fromStationCode,$toStationCode,$trainCode){ + $return_data = $this->BIZ_train_model->get_price($fromStationCode,$toStationCode,$trainCode); + if(!empty($return_data)){ + print_r(json_decode($return_data->TPL_Price)); + }else{ + print_r('没有数据返回'); + } + + } + + //余票转换 + function ticket_exchange($num){ + if(is_numeric($num)){ + if($num == 0){ + return '无'; + }elseif($num >= 99){ + return '有'; + }else{ + return $num; + } + }else{ + if($num == '--'){ + return ''; + } + } + } + + //发送请求 + function get_http($url, $data = '', $method = 'GET') { + $curl = curl_init(); // 启动一个CURL会话 + curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 + curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 + curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer + if ($method == 'POST' && !empty($data)) { + curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 + } + curl_setopt($curl, CURLOPT_TIMEOUT, 45); // 设置超时限制防止死循环 + curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 + $tmpInfo = curl_exec($curl); // 执行操作 + $errno = curl_errno($curl); + if ($errno !== 0) { + return false; + $error_message = $errno . ' ' . curl_error($curl); //记录错误日志 + log_message('error', "third_party/train_app/index/get_http curl {$error_message}"); + } + curl_close($curl); //关闭CURL会话 + return $tmpInfo; //返回数据 + } +} + + + + +?> \ No newline at end of file diff --git a/application/third_party/train/models/BIZ_train_model.php b/application/third_party/train/models/BIZ_train_model.php index b144c738..42a96a93 100644 --- a/application/third_party/train/models/BIZ_train_model.php +++ b/application/third_party/train/models/BIZ_train_model.php @@ -5,8 +5,65 @@ class BIZ_train_model extends CI_Model { function __construct() { parent::__construct(); $this->HT = $this->load->database('HT', TRUE); + $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){ + $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' + ) BEGIN + INSERT INTO TrainPriceList + ( + TPL_Train_No, + TPL_Train_Code, + TPL_From_Station_Code, + TPL_To_Station_Code, + TPL_Price, + TPL_Datetime, + TPL_Source + ) + VALUES + ( + '$TPL_Train_No','$TPL_Train_Code','$TPL_From_Station_Code','$TPL_To_Station_Code','$TPL_Price',GETDATE(),'juhe' + ) + END + ELSE + BEGIN + UPDATE TrainPriceList + SET TPL_Price = '$TPL_Price', + TPL_Datetime = GETDATE(), + TPL_Source = 'juhe' + 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' + END + "; + $query = $this->INFO->query($sql); + return $query; + } + + //获取价格 + public function get_price($fromStationCode,$toStationCode,$trainCode){ + $sql = "SELECT + TPL_Price + FROM + TrainPriceList + WHERE + TPL_Train_Code = '$trainCode' + AND + TPL_From_Station_Code = '$fromStationCode' + AND + TPL_To_Station_Code = '$toStationCode'"; + $query = $this->INFO->query($sql); + return $query->row(); + } + + function biz_order_detail($cold_sn) { $sql = " SELECT TOP 1 bfoi.FOI_SN diff --git a/application/third_party/tuniu/controllers/tuniu_train.php b/application/third_party/tuniu/controllers/tuniu_train.php index 1fe7f2b1..182b088b 100644 --- a/application/third_party/tuniu/controllers/tuniu_train.php +++ b/application/third_party/tuniu/controllers/tuniu_train.php @@ -122,10 +122,11 @@ class Tuniu_train extends CI_Controller{ } } $pricestr = $seat_type.'"train_no":'.'"'.$value->trainNo.'"'; + //余票字符串 $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)).'||||||||'.ticket_exchange($value->gjrwNum).'|'.ticket_exchange($value->qtxbNum).'|'.ticket_exchange($value->rwNum).'|'.ticket_exchange($value->rzNum).'|'.ticket_exchange($value->tdzNum).'|'.ticket_exchange($value->wzNum).'||'.ticket_exchange($value->ywNum).'|'.ticket_exchange($value->yzNum).'|'.ticket_exchange($value->edzNum).'|'.ticket_exchange($value->ydzNum).'|'.ticket_exchange($value->swzNum).'|'.ticket_exchange($value->dwNum).'||'; $i++; $data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}'; - $this->tuniuprice_model->addOrUpdate($value->trainNo,$value->fromStationCode,$value->toStationCode,json_encode($data)); + $this->tuniuprice_model->addOrUpdate($value->trainNo,$value->trainCode,$value->fromStationCode,$value->toStationCode,json_encode($data)); } } print_r(json_encode($return_data)); @@ -139,7 +140,7 @@ class Tuniu_train extends CI_Controller{ */ public function get_price($fromStationCode,$toStationCode,$trainCode){ $return_data = $this->tuniuprice_model->get_price($fromStationCode,$toStationCode,$trainCode); - print_r(json_decode($return_data->tnp_price)); + print_r(json_decode($return_data->TPL_Price)); } //占座下单 diff --git a/application/third_party/tuniu/models/tuniuprice_model.php b/application/third_party/tuniu/models/tuniuprice_model.php index 1fb2e915..64b57979 100644 --- a/application/third_party/tuniu/models/tuniuprice_model.php +++ b/application/third_party/tuniu/models/tuniuprice_model.php @@ -3,64 +3,62 @@ class tuniuprice_model extends CI_Model { function __construct() { parent::__construct(); - $this->HT = $this->load->database('HT', TRUE); + $this->INFO = $this->load->database('INFO', TRUE); } //¼۸ - public function addOrUpdate($tnp_train_no,$tpc_from_station_code,$tpc_to_station_code,$tnp_price){ + public function addOrUpdate($TPL_Train_No,$TPL_Train_Code,$TPL_From_Station_Code,$TPL_To_Station_Code,$TPL_Price){ $sql = "IF NOT EXISTS( SELECT 1 - FROM TuNiuPrice - WHERE tnp_train_no = '$tnp_train_no' - AND tnp_from_station_code = '$tpc_from_station_code' - AND tnp_to_station_code = '$tpc_to_station_code' + 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' ) BEGIN - INSERT INTO TuNiuPrice + INSERT INTO TrainPriceList ( - tnp_train_no - ,tnp_from_station_code - ,tnp_to_station_code - ,tnp_price - ,tnp_datetime + TPL_Train_No, + TPL_Train_Code, + TPL_From_Station_Code, + TPL_To_Station_Code, + TPL_Price, + TPL_Datetime, + TPL_Source ) VALUES ( - '$tnp_train_no','$tpc_from_station_code','$tpc_to_station_code','$tnp_price',GETDATE() + '$TPL_Train_No','$TPL_Train_Code','$TPL_From_Station_Code','$TPL_To_Station_Code','$TPL_Price',GETDATE(),'tuniu' ) END ELSE BEGIN - UPDATE TuNiuPrice - SET tnp_price = '$tnp_price' - ,tnp_datetime = GETDATE() - WHERE tnp_train_no = '$tnp_train_no' - AND tnp_from_station_code = '$tpc_from_station_code' - AND tnp_to_station_code = '$tpc_to_station_code' + UPDATE TrainPriceList + SET 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' END "; - $query = $this->HT->query($sql); + $query = $this->INFO->query($sql); return $query; } //ȡ۸ public function get_price($fromStationCode,$toStationCode,$trainCode){ $sql = "SELECT - tnp_price + TPL_Price FROM - TuNiuPrice + TrainPriceList WHERE - tnp_train_no = '$trainCode' + TPL_Train_Code = '$trainCode' AND - tnp_from_station_code = '$fromStationCode' + TPL_From_Station_Code = '$fromStationCode' AND - tnp_to_station_code = '$toStationCode'"; - $query = $this->HT->query($sql); + TPL_To_Station_Code = '$toStationCode'"; + $query = $this->INFO->query($sql); return $query->row(); } - } - - - - ?> \ No newline at end of file