LogisticAliJiSuApiService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Services;
  3. use App\Traits\ServiceAppAop;
  4. class LogisticAliJiSuApiService
  5. {
  6. use ServiceAppAop;
  7. public function query($logistic_number)
  8. {
  9. $app_code = config('api_logistic.AliJiSu.prod.app-code');
  10. $type = config('api_logistic.AliJiSu.prod.type');
  11. $host = config('api_logistic.AliJiSu.prod.search.host');
  12. $path = config('api_logistic.AliJiSu.prod.search.path');
  13. $method = config('api_logistic.AliJiSu.prod.method');
  14. $headers = array();
  15. array_push($headers, "Authorization:APPCODE " . $app_code);
  16. array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
  17. $query='number='.$logistic_number.'&type='.$type;
  18. $bodys = "null";
  19. $url = $host . $path . "?" . $query;
  20. // $response = Http::withHeaders($headers)->get($url);
  21. // return json_decode($response->body());
  22. $curl = curl_init();
  23. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  24. curl_setopt($curl, CURLOPT_URL, $url);
  25. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  26. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  27. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  28. // curl_setopt($curl, CURLOPT_HEADER, true);
  29. if (1 == strpos("$".$host, "https://"))
  30. {
  31. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  32. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  33. }
  34. curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
  35. return json_decode(curl_exec($curl));
  36. }
  37. public function format($response,$logistic_number): array
  38. {
  39. $result = [];
  40. if (!isset($response)||($response && $response->status!=0)) {
  41. return [
  42. 'logistic_number' => $logistic_number,
  43. ];
  44. }
  45. else {
  46. try {
  47. if ($response->result->number??false)$result['logistic_number'] = $response->result->number;
  48. } catch (\Exception $e) {
  49. LogService::log(LogisticYTOService::class, "AliJiSu快递信息异常", $response);
  50. }
  51. $list=$response->result->list;
  52. if (!empty($list) && is_array($list)) {
  53. $lastNativeRoute = $list[0];
  54. $result['status'] = $this->getStatus($response);
  55. if ($result['status'] == '已签收') $result['received_at'] = $lastNativeRoute->time;
  56. $result['transfer_status'] = $this->getTransferStatus($list);
  57. $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
  58. } else {
  59. $result['status'] = null;
  60. $result['transfer_status'] = [];
  61. }
  62. if (!array_key_exists('status', $result)) {
  63. $result['status'] = null;
  64. $result['transfer_status'] = [];
  65. }
  66. return $result;
  67. }
  68. }
  69. /**
  70. * @param $nativeData
  71. * @return string
  72. */
  73. private function getStatus($nativeData): string
  74. {
  75. $status = null;
  76. switch ($nativeData->result->deliverystatus) {
  77. case '1':
  78. $status = '在途';
  79. break;
  80. case '2':
  81. $status = '派送中';
  82. break;
  83. case '3':
  84. $status = '已签收';
  85. break;
  86. case '4':
  87. $status = '派送异常';
  88. break;
  89. default:
  90. $status = '其他';
  91. }
  92. return $status;
  93. }
  94. /**
  95. * @param $nativeRoutes
  96. * @return array
  97. */
  98. private function getTransferStatus($nativeRoutes): array
  99. {
  100. $transferStatus = [];
  101. foreach ($nativeRoutes as $nativeRoute) {
  102. $item = [];
  103. $item['accept_time'] = $nativeRoute->time;
  104. $item['accept_address'] = $nativeRoute->status;
  105. $item['remark'] = "";
  106. $transferStatus[] = $item;
  107. }
  108. return $transferStatus;
  109. }
  110. }