diff --git a/application/third_party/tuniu/controllers/tuniu_train.php b/application/third_party/tuniu/controllers/tuniu_train.php index b8b3550a..daae0859 100644 --- a/application/third_party/tuniu/controllers/tuniu_train.php +++ b/application/third_party/tuniu/controllers/tuniu_train.php @@ -1,5 +1,4 @@ load->helper('tuniu'); + $this->load->library('Des'); $this->load->model("tuniuprice_model"); } - public function index(){ - echo 'hello tuniu'; - } - //创建请求签名 public function create_sign(){ $time = date('Y-m-d H:i:s',time()); @@ -33,6 +30,11 @@ class Tuniu_train extends CI_Controller{ return strtoupper(md5($sign)); } + //index + public function index(){ + echo 'hi,tuniu'; + } + //获取余票 public function search($date,$fromStation,$toStation){ $sign = $this->create_sign(); @@ -44,62 +46,87 @@ class Tuniu_train extends CI_Controller{ "timestamp": "'.$time.'", "data": { "trainDate": "'.$date.'", "fromStation": "'.$fromStation.'", "toStation": "'.$toStation.'", "trainCode": "" } }'; - $back_data = $this->get_http($url,$post_data,$method = 'POST'); - - //print_r('返回码'.json_decode($back_data)->returnCode.'
'); + $back_data = post_tuniu($url,$post_data,$method = 'POST'); + //print_r($post_data); + //print_r(json_decode($back_data)); $return_data = new stdClass(); + $return_data->returnCode = json_decode($back_data)->returnCode; + $return_data->httpstatus = 200; $return_data->data = new stdClass(); $return_data->data->result = array(); - $return_data->returnCode = json_decode($back_data)->returnCode; $i = 0; $pricestr = ''; - foreach (json_decode($back_data)->data as $value){ - $seat_type = ''; - if(is_numeric($value->gjrwPrice)){ - $gjrwPrice = $value->gjrwPrice * 10; - $seat_type .= '"6":"'.$gjrwPrice.'","A6":"¥'.$value->gjrwPrice.'",'; - } - if(is_numeric($value->qtxbPrice)){ - $seat_type .= '"H":"¥'.$value->qtxbPrice.'",'; - } - if(is_numeric($value->rwPrice)){ - $rwPrice = $value->rwPrice * 10; - $seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rwPrice.'",'; - } - if(is_numeric($value->rzPrice)){ - $rzPrice = $value->rzPrice * 10; - $seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rzPrice.'",'; - } - if(is_numeric($value->tdzPrice)){ - $seat_type .= '"P":"¥'.$value->tdzPrice.'",'; - } - if(is_numeric($value->wzPrice)){ - $wzPrice = $value->wzPrice * 10; - $seat_type .= '"WZ":"¥'.$value->wzPrice.'",'; - } - if(is_numeric($value->ywPrice)){ - $ywPrice = $value->ywPrice * 10; - $seat_type .= '"A3":"'.$ywPrice.'","3":"¥'.$value->ywPrice.'",'; - } - if(is_numeric($value->yzPrice)){ - $yzPrice = $value->yzPrice * 10; - $seat_type .= '"A1":"'.$yzPrice.'","1":"¥'.$value->yzPrice.'",'; - } - if(is_numeric($value->edzPrice)){ - $seat_type .= '"O":"¥'.$value->edzPrice.'",'; - } - if(is_numeric($value->ydzPrice)){ - $seat_type .= '"M":"¥'.$value->ydzPrice.'",'; - } - if(is_numeric($value->swzPrice)){ - $swzPrice = $value->swzPrice * 10; - $seat_type .= '"A9":"'.$swzPrice.'","9":"¥'.$value->swzPrice.'",'; + if(!empty(json_decode($back_data)->data)){ + foreach (json_decode($back_data)->data as $value){ + $seat_type = ''; + if(isset($value->gjrwPrice)){ + if(is_numeric($value->gjrwPrice)){ + $gjrwPrice = $value->gjrwPrice * 10; + $seat_type .= '"6":"'.$gjrwPrice.'","A6":"¥'.$value->gjrwPrice.'",'; + } + } + if(isset($value->qtxbPrice)){ + if(is_numeric($value->qtxbPrice)){ + $seat_type .= '"H":"¥'.$value->qtxbPrice.'",'; + } + } + if(isset($value->rwPrice)){ + if(is_numeric($value->rwPrice)){ + $rwPrice = $value->rwPrice * 10; + $seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rwPrice.'",'; + } + } + if(isset($value->rzPrice)){ + if(is_numeric($value->rzPrice)){ + $rzPrice = $value->rzPrice * 10; + $seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rzPrice.'",'; + } + } + if(isset($value->tdzPrice)){ + if(is_numeric($value->tdzPrice)){ + $seat_type .= '"P":"¥'.$value->tdzPrice.'",'; + } + } + if(isset($value->wzPrice)){ + if(is_numeric($value->wzPrice)){ + $wzPrice = $value->wzPrice * 10; + $seat_type .= '"WZ":"¥'.$value->wzPrice.'",'; + } + } + if(isset($value->ywPrice)){ + if(is_numeric($value->ywPrice)){ + $ywPrice = $value->ywPrice * 10; + $seat_type .= '"A3":"'.$ywPrice.'","3":"¥'.$value->ywPrice.'",'; + } + } + if(isset($value->yzPrice)){ + if(is_numeric($value->yzPrice)){ + $yzPrice = $value->yzPrice * 10; + $seat_type .= '"A1":"'.$yzPrice.'","1":"¥'.$value->yzPrice.'",'; + } + } + if(isset($value->edzPrice)){ + if(is_numeric($value->edzPrice)){ + $seat_type .= '"O":"¥'.$value->edzPrice.'",'; + } + } + 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 .= '"A9":"'.$swzPrice.'","9":"¥'.$value->swzPrice.'",'; + } + } + $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)); } - $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.'||'.$value->trainStartDate.'||||||||'.$value->gjrwNum.'|'.$value->qtxbNum.'|'.$value->rwNum.'|'.$value->rzNum.'|'.$value->tdzNum.'|'.$value->wzNum.'||'.$value->ywNum.'|'.$value->yzNum.'|'.$value->edzNum.'|'.$value->ydzNum.'|'.$value->swzNum.'|'.$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)); } print_r(json_encode($return_data)); } @@ -117,21 +144,22 @@ class Tuniu_train extends CI_Controller{ //占座下单 public function book_ticket(){ + header('Content-Type:application/json;charset=UTF-8'); $sign = $this->create_sign(); $url = TUNIU_URL.'/train/book'; $time = date('Y-m-d H:i:s',time()); - $data = '{ - "retailOrderId": "1", - "cheCi": "D2946", - "fromStationCode": "GLZ", - "fromStationName":"桂林", - "toStationCode":"GBZ", - "toStationName":"桂林北", - "trainDate":"2017-09-23", + $data = '{ + "retailOrderId": "w123", + "cheCi": "K1137", + "fromStationCode": "GBZ", + "fromStationName":"桂林北", + "toStationCode":"NNZ", + "toStationName":"南宁", + "trainDate":"2017-09-25", "callBackUrl":"http://www.mycht.cn/info.php/apps/train/tuniu_callback/book", + "hasSeat":true, "contact":"陈宇超", - "phone":"18877381547", - "insureCode ":"", + "phone":"18877381547", "passengers":[{ "passengerId":1, "ticketNo": null, @@ -141,33 +169,93 @@ class Tuniu_train extends CI_Controller{ "passportTypeName": "二代身份证", "piaoType": "1", "piaoTypeName": "成人票", - "zwCode": "O", - "zwName": "二等座", + "zwCode": "4", + "zwName": "软卧", "cxin": null, - "price": "5.5", - "reason": 0, - "provinceCode": null, - "schoolCode": null, - "schoolName": null, - "studentNo": null, - "schoolSystem": null, - "enterYear": null, - "preferenceFromStationName": null, - "preferenceFromStationCode": null, - "preferenceToStationName": null, - "preferenceToStationCode": null + "price": "166.5", + "reason": 0 }] }'; + $crypt = new DES(); + $mstr = $crypt->encrypt($data,TUNIU_KEY); $post_data = '{ "apiKey": "'.TUNIU_KEY.'", "sign": "'.$sign.'", "timestamp": "'.$time.'", - "data": "'.$data.'" + "data": "'.$mstr.'" }'; //print_r($url); print_r($post_data); - //$back_data = $this->get_http($url,$post_data,$method = 'POST'); - //print_r($back_data); + $back_data = post_tuniu($url,$post_data,$method = 'POST'); + print_r($back_data); + } + + //取消占座 + public function cancel_book($retailOrderId,$orderId){ + $url = TUNIU_URL.'/train/cancel'; + $sign = $this->create_sign(); + $time = date('Y-m-d H:i:s',time()); + $data = + $post_data = '{ + "apiKey": "'.TUNIU_KEY.'", + "sign": "'.$sign.'", + "timestamp": "'.$time.'", + "data": { + "retailOrderId":"'.$retailOrderId.'", + "orderId":"'.$orderId.'", + "callBackUrl":"http://www.mycht.cn/info.php/apps/train/tuniu_callback/cancelbook" + } + }'; + $back_data = post_tuniu($url,$post_data,$method = 'POST'); + print_r($back_data); + } + + //确认出票 + public function confirm_ticket($retailOrderId,$orderId){ + $url = TUNIU_URL.'/train/confirm'; + $sign = $this->create_sign(); + $time = date('Y-m-d H:i:s',time()); + $post_data = '{ + "apiKey": "'.TUNIU_KEY.'", + "sign": "'.$sign.'", + "timestamp": "'.$time.'", + "data": { + "retailOrderId":"'.$retailOrderId.'", + "orderId":"'.$orderId.'", + "callBackUrl":"http://www.mycht.cn/info.php/apps/train/tuniu_callback/confirm" + } + }'; + $back_data = post_tuniu($url,$post_data,$method = 'POST'); + print_r($back_data); + } + + //退票 + public function cancel_ticket($retailOrderId,$orderId){ + $url = TUNIU_URL.'/train/return'; + $sign = $this->create_sign(); + $time = date('Y-m-d H:i:s',time()); + $data = '{ + "retailOrderId": "'.$retailOrderId.'", + "orderId": "'.$orderId.'", + "orderNumber": "E551561213", + "callBackUrl":"http://www.mycht.cn/info.php/apps/train/tuniu_callback/return_ticket", + "tickets":[{ + "ticketNo": "TN_aaac40ec06d72851", + "passengerName": "陈宇超", + "passportTypeId": "1", + "passportNo": "450302199208131039" + }] + }'; + $crypt = new DES(); + $mstr = $crypt->encrypt($data,TUNIU_KEY); + $post_data = '{ + "apiKey": "'.TUNIU_KEY.'", + "sign": "'.$sign.'", + "timestamp": "'.$time.'", + "data": "'.$mstr.'" + }'; + $back_data = post_tuniu($url,$post_data,$method = 'POST'); + print_r($back_data); } //身份验证 @@ -184,86 +272,7 @@ class Tuniu_train extends CI_Controller{ "timestamp": "'.$time.'", "data": { "name": "", "identityType": "", "identifyCard": ""} }'; - $back_data = $this->get_http($url,$post_data,$method = 'POST'); + $back_data = post_tuniu($url,$post_data,$method = 'POST'); print_r($back_data); } - - public function des_encrypt() { - $this->load->library('Des'); - $key = 'Te56CBQmorzJGeYIIK'; - $str = '{ - "retailOrderId": "452514", - "cheCi": "D2946", - "fromStationCode": "GLZ", - "fromStationName":"桂林", - "toStationCode":"GBZ", - "toStationName":"桂林北", - "trainDate":"2017-09-11", - "callBackUrl":"http://www.mycht.cn/info.php/apps/train/tuniu_callback/book", - "hasSeat":false, - "contact":"陈宇超", - "phone":"18877381547", - "insureCode ":"", - "passengers":[{ - "passengerId":1, - "ticketNo": null, - "passengerName": "陈宇超", - "passportNo": "450302199208131039", - "passportTypeId": "1", - "passportTypeName": "二代身份证", - "piaoType": "1", - "piaoTypeName": "成人票", - "zwCode": "O", - "zwName": "二等座", - "cxin": null, - "price": "5.5", - "reason": 0, - "provinceCode": null, - "schoolCode": null, - "schoolName": null, - "studentNo": null, - "schoolSystem": null, - "enterYear": null, - "preferenceFromStationName": null, - "preferenceFromStationCode": null, - "preferenceToStationName": null, - "preferenceToStationCode": null - }] - }'; - $crypt = new DES(); - $mstr = $crypt->encrypt($str,$key); - echo $mstr; - - } - - public function des_decrypt(){ - echo base64_decode('JSh+QwFlC/uPYxpXDpx+g6LQ/82MAh3czFaFvSowBUt/A6LpIVPcNzYC5H8RpjlsGCQ67zcTJs6KA/mnW7+qiVZNgOvRi3x7at57s8k0sYEY23ow5dLanooD+adbv6qJVk2A69GLfHsZ2P7tON4xzWuPgXGftReOigP5p1u/qok8tDL6rQsR1Up+EbOJa5K9/a5Z/gAXP1rb3H95xn1oBIBRwRXG0nS0tHBZ3EcG/+WzzqcER2gRP7O9cXSfnYfagk9nRgjgwCgKEQ8njmZn/JKTf9hwxv9Le/fJyy/abAkUzYx5upiR68JCE6WU35LCIe9Kj4yN2qTOSJ76yhargH5Oz1mhtzL01n0bazZZU+KJefd2ztEf636MEmZPktdn4VksA5yjg3VkIdm+9RkSQmw5j4R6YCcmABwjnTd5eLboWH86kivibc3MZvztC6xWp4he0UqlrZgcNVIyBlgzGIoD+adbv6qJ1e8yF6J0BIr96lEdWNUvM4oD+adbv6qJK4xPdM7sNMAiCptWdAkaI3uGy6J++EHPPmXX74sQVTEkAWwl8mPSTGbYJIWzlCa4ZFDKMY39BBa+fML8BpggFMkoZXI6YP/KK4xPdM7sNMB5k9KcltAaoxwvKGTfJLKwZCHZvvUZEkLjs4DapMFpeqqbSOpZq7c+nOc9rH+wTBTQCYrlhaLowIk/6rO2DgWH9KwZlgSf/sIT/aSMqmYYd5KGQf3Ih7JHjOUcn091cMr3afAvhSbBiiMtId75t3/inMNM+AOHOiJ9aSkrOtjTo+WLSlR2RG/oElEV4xbCun75YbzkSNX8ftDml1n4OV8CElEV4xbCun7CPOQCcbvhSku5LeShh5AaK9vVO6AntV4tqlCQBG1YY9mqNn3LFytpjOUcn091cMqquBuAYn5gEkdxbxg0uov+DGacmkA2YrTOgII9i7gLVEe7i7ofJjoq2118SvJrI2OLq0aZ/yR4hRipAw9bC3frZCHZvvUZEkK7cM7flePbtI1TCis9O6+3o0xdTeTPpiimchHwGjPkQUAQODXW7bbP2118SvJrI2Osg23BZPiruP4xoWN0cvb/tCm7TVCLnWu3aLfmpH9W/cDR5JLdk5lS2WKwFWNBokv2fIKLS8h45A1YLUN4Y7Pn8leGzcgaSHDbXXxK8msjY6yDbcFk+Ku4it0sI2XiO52+fML8BpggFMkoZXI6YP/KDoSNr/1XUWfWlTysVsREpGQh2b71GRJCzI8FUajTFEOQ+mKOWqzgGGzLbSfrK6aAZ2RIcEwNLkSIGdQTpxUuzVyUu7DCLqRbgaEC9bW8QmGowJj/8LdHbuno+JLNfUOQtCm7TVCLnWujTF1N5M+mKJuuv4kqIDIcoGWCauxf45dRRnr3ImAzi7Qpu01Qi51ro0xdTeTPpiibrr+JKiAyHKBlgmrsX+OX6ej4ks19Q5D+98vgpAT13mlSr0Q/lKffCXRX0VOdhhM='); - } - - //发送请求函数 - public 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_HTTPHEADER, array('Content-Type:application/json')); - } - 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; - echo $errno . curl_error($curl); //记录错误日志 - } - curl_close($curl); //关闭CURL会话 - return $tmpInfo; //返回数据 - } } \ No newline at end of file diff --git a/application/third_party/tuniu/helpers/tuniu_helper.php b/application/third_party/tuniu/helpers/tuniu_helper.php new file mode 100644 index 00000000..1b5e1fa9 --- /dev/null +++ b/application/third_party/tuniu/helpers/tuniu_helper.php @@ -0,0 +1,56 @@ + \ No newline at end of file diff --git a/application/third_party/tuniu/libraries/Des.php b/application/third_party/tuniu/libraries/Des.php index 81846fa6..c0d5a2b6 100644 --- a/application/third_party/tuniu/libraries/Des.php +++ b/application/third_party/tuniu/libraries/Des.php @@ -5,7 +5,7 @@ class Des function encrypt($string,$key) { $size = mcrypt_get_block_size('des', 'ecb'); - $string = mb_convert_encoding($string, 'GBK', 'UTF-8'); + //$string = mb_convert_encoding($string, 'GBK', 'UTF-8'); $string = $this->pkcs5_pad($string, $size); $td = mcrypt_module_open('des', '', 'ecb', ''); $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); @@ -17,10 +17,9 @@ class Des return $data; } - function decrypt($string) + function decrypt($string,$key) { $string = base64_decode($string); - $key = $this->key; $td = mcrypt_module_open('des', '', 'ecb', ''); //使用MCRYPT_DES算法,cbc模式 $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);