TransferCheck.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\LogisticsCompany;
  4. use traits\think\Instance;
  5. class TransferCheck{
  6. use Instance;
  7. protected $no;
  8. protected $name='';
  9. /** @var LogisticsCompany */
  10. protected $logisticsCompany;
  11. /**
  12. * @var mixed
  13. */
  14. private $phone;
  15. /**
  16. * @param mixed $no
  17. */
  18. public function setNo($no)
  19. {
  20. $this->no = $no;
  21. return $this;
  22. }
  23. /**
  24. * @param mixed $no
  25. */
  26. public function setName($no='')
  27. {
  28. $this->name = $no;
  29. return $this;
  30. }
  31. /**
  32. * @param mixed $no
  33. */
  34. public function setPhone($no)
  35. {
  36. $this->phone = $no;
  37. return $this;
  38. }
  39. /**
  40. * @param LogisticsCompany $logisticsCompany
  41. */
  42. public function setLogisticsCompany(LogisticsCompany $logisticsCompany)
  43. {
  44. $this->logisticsCompany = $logisticsCompany;
  45. return $this;
  46. }
  47. public function get(){
  48. return $this->getOrderTracesByJson();
  49. }
  50. function sendPost($url, $datas) {
  51. $postdata = http_build_query($datas);
  52. $options = array(
  53. 'http' => array(
  54. 'method' => 'POST',
  55. 'header' => 'Content-type:application/x-www-form-urlencoded',
  56. 'content' => $postdata,
  57. 'timeout' => 15 * 60 // 超时时间(单位:s)
  58. ),
  59. "ssl" => [
  60. "verify_peer"=>false,
  61. "verify_peer_name"=>false,
  62. ]
  63. );
  64. $context = stream_context_create($options);
  65. $result = file_get_contents($url, false, $context);
  66. return $result;
  67. }
  68. function encrypt($data) {
  69. $ApiKey=config('site.kd_bird_apiKey');
  70. return urlencode(base64_encode(md5($data.$ApiKey)));
  71. }
  72. function getOrderTracesByJson(){
  73. // 组装应用级参数
  74. $requestData= "{".
  75. "'OrderCode': '',".
  76. "'CustomerName': '1234',".
  77. "'ShipperCode': 'SF',".
  78. "'LogisticCode': 'SF00003618100',".
  79. "}";
  80. $requestData=[
  81. 'OrderCode'=>'',
  82. 'CustomerName'=>substr($this->phone,-4),
  83. 'ShipperCode'=>$this->logisticsCompany['no'],
  84. 'LogisticCode'=>$this->no,
  85. ];
  86. $requestDataJson=json_encode($requestData);
  87. // 组装系统级参数
  88. $datas = array(
  89. 'EBusinessID' => config('site.kd_bird_userId'),
  90. 'RequestType' => '8001', //在途监控即时查询接口指令8001/地图版即时查询接口指令8003
  91. 'RequestData' => urlencode($requestDataJson) ,
  92. 'DataType' => '2',
  93. );
  94. $datas['DataSign'] = $this->encrypt($requestDataJson);
  95. //以form表单形式提交post请求,post请求体中包含了应用级参数和系统级参数
  96. $result=$this->sendPost('https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);
  97. //根据公司业务处理返回的信息......
  98. $resultArr=json_decode($result,true);
  99. user_log('trans',[
  100. 'params'=>$requestData,
  101. 'result'=>$resultArr
  102. ]);
  103. if(!$resultArr){
  104. throw_user('请求失败');
  105. }
  106. if(!$resultArr['Success']){
  107. throw_user($resultArr['Reason']);
  108. }
  109. return $resultArr['Traces'];
  110. }
  111. function getCompany(){
  112. // 组装应用级参数
  113. $requestData= "{'LogisticCode':'{$this->no}'}";
  114. // 组装系统级参数
  115. $datas = array(
  116. 'EBusinessID' => config('site.kd_bird_userId'),
  117. 'RequestType' => '2002',
  118. 'RequestData' => urlencode($requestData) ,
  119. 'DataType' => '2',
  120. );
  121. $datas['DataSign'] = $this->encrypt($requestData);
  122. //以form表单形式提交post请求,post请求体中包含了应用级参数和系统级参数
  123. $result=$this->sendPost('https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);
  124. $resultArr=json_decode($result,true);
  125. if(!$resultArr){
  126. throw_user('请求失败');
  127. }
  128. if(!$resultArr['Success']){
  129. throw_user($resultArr['Reason']);
  130. }
  131. $shippers=$resultArr['Shippers'][0]??[];
  132. $no=$shippers?$shippers['ShipperCode']:0;
  133. if(!$no){
  134. return null;
  135. }
  136. return LogisticsCompany::where('no',$no)->value('id');
  137. }
  138. }