123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace app\common\service;
- use app\common\model\LogisticsCompany;
- use traits\think\Instance;
- class TransferCheck{
- use Instance;
- protected $no;
- protected $name='';
- /** @var LogisticsCompany */
- protected $logisticsCompany;
- /**
- * @var mixed
- */
- private $phone;
- /**
- * @param mixed $no
- */
- public function setNo($no)
- {
- $this->no = $no;
- return $this;
- }
- /**
- * @param mixed $no
- */
- public function setName($no='')
- {
- $this->name = $no;
- return $this;
- }
- /**
- * @param mixed $no
- */
- public function setPhone($no)
- {
- $this->phone = $no;
- return $this;
- }
- /**
- * @param LogisticsCompany $logisticsCompany
- */
- public function setLogisticsCompany(LogisticsCompany $logisticsCompany)
- {
- $this->logisticsCompany = $logisticsCompany;
- return $this;
- }
- public function get(){
- return $this->getOrderTracesByJson();
- }
- function sendPost($url, $datas) {
- $postdata = http_build_query($datas);
- $options = array(
- 'http' => array(
- 'method' => 'POST',
- 'header' => 'Content-type:application/x-www-form-urlencoded',
- 'content' => $postdata,
- 'timeout' => 15 * 60 // 超时时间(单位:s)
- ),
- "ssl" => [
- "verify_peer"=>false,
- "verify_peer_name"=>false,
- ]
- );
- $context = stream_context_create($options);
- $result = file_get_contents($url, false, $context);
- return $result;
- }
- function encrypt($data) {
- $ApiKey=config('site.kd_bird_apiKey');
- return urlencode(base64_encode(md5($data.$ApiKey)));
- }
- function getOrderTracesByJson(){
- // 组装应用级参数
- $requestData= "{".
- "'OrderCode': '',".
- "'CustomerName': '1234',".
- "'ShipperCode': 'SF',".
- "'LogisticCode': 'SF00003618100',".
- "}";
- $requestData=[
- 'OrderCode'=>'',
- 'CustomerName'=>substr($this->phone,-4),
- 'ShipperCode'=>$this->logisticsCompany['no'],
- 'LogisticCode'=>$this->no,
- ];
- $requestDataJson=json_encode($requestData);
- // 组装系统级参数
- $datas = array(
- 'EBusinessID' => config('site.kd_bird_userId'),
- 'RequestType' => '8001', //在途监控即时查询接口指令8001/地图版即时查询接口指令8003
- 'RequestData' => urlencode($requestDataJson) ,
- 'DataType' => '2',
- );
- $datas['DataSign'] = $this->encrypt($requestDataJson);
- //以form表单形式提交post请求,post请求体中包含了应用级参数和系统级参数
- $result=$this->sendPost('https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);
- //根据公司业务处理返回的信息......
- $resultArr=json_decode($result,true);
- user_log('trans',[
- 'params'=>$requestData,
- 'result'=>$resultArr
- ]);
- if(!$resultArr){
- throw_user('请求失败');
- }
- if(!$resultArr['Success']){
- throw_user($resultArr['Reason']);
- }
- return $resultArr['Traces'];
- }
- function getCompany(){
- // 组装应用级参数
- $requestData= "{'LogisticCode':'{$this->no}'}";
- // 组装系统级参数
- $datas = array(
- 'EBusinessID' => config('site.kd_bird_userId'),
- 'RequestType' => '2002',
- 'RequestData' => urlencode($requestData) ,
- 'DataType' => '2',
- );
- $datas['DataSign'] = $this->encrypt($requestData);
- //以form表单形式提交post请求,post请求体中包含了应用级参数和系统级参数
- $result=$this->sendPost('https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);
- $resultArr=json_decode($result,true);
- if(!$resultArr){
- throw_user('请求失败');
- }
- if(!$resultArr['Success']){
- throw_user($resultArr['Reason']);
- }
- $shippers=$resultArr['Shippers'][0]??[];
- $no=$shippers?$shippers['ShipperCode']:0;
- if(!$no){
- return null;
- }
- return LogisticsCompany::where('no',$no)->value('id');
- }
- }
|