LogisticAliJiSuApiService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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): array
  38. {
  39. $result = [];
  40. if (!isset($response)) {return [];}
  41. else {
  42. try {
  43. if ($response->result->number??false)$result['logistic_number'] = $response->result->number;
  44. } catch (\Exception $e) {
  45. LogService::log(LogisticYTOService::class, "AliJiSu快递信息异常", $response);
  46. }
  47. $list=$response->result->list;
  48. if (!empty($list) && is_array($list)) {
  49. $lastNativeRoute = $list[0];
  50. $result['status'] = $this->getStatus($response);
  51. if ($result['status'] == '已收件') $result['received_at'] = $lastNativeRoute->time;
  52. $result['transfer_status'] = $this->getTransferStatus($list);
  53. $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
  54. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  55. $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastNativeRoute ? $lastNativeRoute->time : null);
  56. $result['exception_type'] = $exceptionData['exception_type'];
  57. $result['exception'] = $exceptionData['exception'];
  58. } else {
  59. $result['status'] = null;
  60. $result['transfer_status'] = [];
  61. }
  62. if (!array_key_exists('exception', $result)
  63. && !array_key_exists('exception_type', $result)
  64. && array_key_exists('transfer_status', $result)
  65. ) {
  66. $result['exception_type'] = '无';
  67. $result['exception'] = '否';
  68. }
  69. if (!array_key_exists('status', $result)) {
  70. $result['status'] = null;
  71. $result['transfer_status'] = [];
  72. }
  73. return $result;
  74. }
  75. }
  76. /**
  77. * @param $nativeData
  78. * @return string
  79. */
  80. private function getStatus($nativeData): string
  81. {
  82. $status = null;
  83. switch ($nativeData->result->deliverystatus) {
  84. case '1':
  85. $status = '在途';
  86. break;
  87. case '2':
  88. $status = '派送中';
  89. break;
  90. case '3':
  91. $status = '已收件';
  92. break;
  93. case '4':
  94. $status = '派送异常';
  95. break;
  96. default:
  97. $status = '无';
  98. }
  99. return $status;
  100. }
  101. /**
  102. * @param $nativeRoutes
  103. * @return array
  104. */
  105. private function getTransferStatus($nativeRoutes): array
  106. {
  107. $transferStatus = [];
  108. foreach ($nativeRoutes as $nativeRoute) {
  109. $item = [];
  110. $item['accept_time'] = $nativeRoute->time;
  111. $item['accept_address'] = $nativeRoute->status;
  112. $item['remark'] = "";
  113. $transferStatus[] = $item;
  114. }
  115. return $transferStatus;
  116. }
  117. }