LogisticALiYunService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Services;
  3. use App\Traits\ServiceAppAop;
  4. class LogisticALiYunService
  5. {
  6. use ServiceAppAop;
  7. public function query($logistic_number)
  8. {
  9. $host = config('api_logistic.ALiYun.prod.search.host');
  10. $path = config('api_logistic.ALiYun.prod.search.path');
  11. $headers = [
  12. "Authorization:APPCODE " . config('api_logistic.ALiYun.prod.app-code')
  13. ];
  14. $url = $host.$path.'?no='.$logistic_number;
  15. $curl = curl_init();
  16. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, config('api_logistic.ALiYun.prod.method'));
  17. curl_setopt($curl, CURLOPT_URL, $url);
  18. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  19. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  20. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  21. curl_setopt($curl, CURLOPT_HEADER, true);
  22. if (1 == strpos("$" . $host, "https://")) {
  23. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  24. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  25. }
  26. $out_put = curl_exec($curl);
  27. dd($out_put);
  28. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  29. list($header, $body) = explode("\r\n\r\n", $out_put, 2);
  30. if ($httpCode == 200) {
  31. print("正常请求计费(其他均不计费)<br>");
  32. print($body);
  33. } else {
  34. if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
  35. print("参数错误");
  36. } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
  37. print("AppCode错误");
  38. } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
  39. print("请求的 Method、Path 或者环境错误");
  40. } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
  41. print("服务未被授权(或URL和Path不正确)");
  42. } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
  43. print("套餐包次数用完");
  44. } elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
  45. print("套餐包次数用完,请续购套餐");
  46. } elseif ($httpCode == 500) {
  47. print("API网关错误");
  48. } elseif ($httpCode == 0) {
  49. print("URL错误");
  50. } else {
  51. print("参数名错误 或 其他错误");
  52. print($httpCode);
  53. $headers = explode("\r\n", $header);
  54. $headList = array();
  55. foreach ($headers as $head) {
  56. $value = explode(':', $head);
  57. $headList[$value[0]] = $value[1];
  58. }
  59. print($headList['x-ca-error-message']);
  60. }
  61. }
  62. }
  63. public function format($response, $logistic_number): array
  64. {
  65. $result = [];
  66. if (!isset($response) || ($response && $response->status != 0)) {
  67. return [
  68. 'logistic_number' => $logistic_number,
  69. ];
  70. } else {
  71. try {
  72. if ($response->result->number ?? false) $result['logistic_number'] = $response->result->number;
  73. } catch (\Exception $e) {
  74. LogService::log(LogisticYTOService::class, "AliJiSu快递信息异常", $response);
  75. }
  76. $list = $response->result->list;
  77. if (!empty($list) && is_array($list)) {
  78. $lastNativeRoute = $list[0];
  79. $result['status'] = $this->getStatus($response);
  80. if ($result['status'] == '已签收') $result['received_at'] = $lastNativeRoute->time;
  81. $result['transfer_status'] = $this->getTransferStatus($list);
  82. $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
  83. } else {
  84. $result['status'] = null;
  85. $result['transfer_status'] = [];
  86. }
  87. if (!array_key_exists('status', $result)) {
  88. $result['status'] = null;
  89. $result['transfer_status'] = [];
  90. }
  91. return $result;
  92. }
  93. }
  94. /**
  95. * @param $nativeData
  96. * @return string
  97. */
  98. private function getStatus($nativeData): string
  99. {
  100. $status = null;
  101. switch ($nativeData->result->deliverystatus) {
  102. case '1':
  103. $status = '在途';
  104. break;
  105. case '2':
  106. $status = '派送中';
  107. break;
  108. case '3':
  109. $status = '已签收';
  110. break;
  111. case '4':
  112. $status = '派送异常';
  113. break;
  114. default:
  115. $status = '其他';
  116. }
  117. return $status;
  118. }
  119. /**
  120. * @param $nativeRoutes
  121. * @return array
  122. */
  123. private function getTransferStatus($nativeRoutes): array
  124. {
  125. $transferStatus = [];
  126. foreach ($nativeRoutes as $nativeRoute) {
  127. $item = [];
  128. $item['accept_time'] = $nativeRoute->time;
  129. $item['accept_address'] = $nativeRoute->status;
  130. $item['remark'] = "";
  131. $transferStatus[] = $item;
  132. }
  133. return $transferStatus;
  134. }
  135. }