LogisticYDService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Services;
  3. use App\OrderPackage;
  4. use App\Traits\ServiceAppAop;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\Http;
  7. class LogisticYDService
  8. {
  9. use ServiceAppAop;
  10. private $app_key;
  11. private $app_secret;
  12. private $url;
  13. /**
  14. * 批量订阅接口
  15. * @param $logistic_numbers array
  16. * @return mixed
  17. */
  18. public function registerApi(array $logistic_numbers)
  19. {
  20. $this->app_key = config('api_logistic.YD.prod.app-key', '999999');
  21. $this->app_secret = config('api_logistic.YD.prod.app-secret', '04d4ad40eeec11e9bad2d962f53dda9d');
  22. $this->url = config('api_logistic.YD.prod.register.url');
  23. $sender = [
  24. "address" => "上海市松江区泗泾镇泗砖公路351号",
  25. "city" => "上海市",
  26. "county" => "松江区",
  27. "name" => "施尧",
  28. "phone" => '13761413262',
  29. "province" => "上海市"
  30. ];
  31. $body = [
  32. "orders" => [],
  33. ];
  34. $order_packages = OrderPackage::query()
  35. ->with('order')
  36. ->whereIn('logistic_number', $logistic_numbers)->get();
  37. foreach ($order_packages as $order_package) {
  38. $order = $order_package->order;
  39. $body['orders'][] = [
  40. 'orderid' => $order->client_code,
  41. "mailno" => $order_package->logistic_number,
  42. "receiver" => [
  43. "address" => $order->address,
  44. "city" => $order->city,
  45. "county" => $order->district,
  46. "name" => $order->consignee_name,
  47. "phone" => $order->consignee_phone,
  48. "province" => $order->province
  49. ],
  50. "sender" => $sender
  51. ];
  52. }
  53. $json_body = json_encode($body, JSON_UNESCAPED_UNICODE);
  54. $sign = md5($json_body . '_' . $this->app_secret);
  55. $headers = [
  56. 'app-key' => $this->app_key,
  57. 'sign' => $sign,
  58. 'req-time' => now()->timestamp,
  59. "Content-Type" => "application/json"
  60. ];
  61. $response = Http::withHeaders($headers)->withBody($json_body, 'application/json')->post($this->url);
  62. return json_decode($response);
  63. }
  64. public function query($logistic_number)
  65. {
  66. $this->app_key = config('api_logistic.YD.prod.app-key', '999999');
  67. $this->app_secret = config('api_logistic.YD.prod.app-secret', '04d4ad40eeec11e9bad2d962f53dda9d');
  68. $this->url = config('api_logistic.YD.prod.search.url');
  69. $body = [
  70. "mailno" => $logistic_number
  71. ];
  72. $sign = md5(json_encode($body, JSON_UNESCAPED_UNICODE) . '_' . $this->app_secret);
  73. $headers = [
  74. 'app-key' => $this->app_key,
  75. 'sign' => $sign,
  76. 'req-time' => now()->timestamp,
  77. "Content-Type" => "application/json"
  78. ];
  79. $response = Http::withHeaders($headers)->withBody(json_encode($body, JSON_UNESCAPED_UNICODE), 'application/json')->post($this->url);
  80. return json_decode($response->body());
  81. }
  82. public function format($nativeResponse)
  83. {
  84. if (is_null($nativeResponse) || $nativeResponse->code != '0000' || $nativeResponse->data->result == "false") {
  85. return [];
  86. } else {
  87. $nativeData = $nativeResponse->data;
  88. try {
  89. $result['logistic_number'] = $nativeData->mailno;
  90. } catch (\Exception $e) {
  91. LogService::log(LogisticYDService::class, "YD快递信息异常", $nativeResponse);
  92. return [];
  93. }
  94. $nativeRoutes = $nativeData->steps;
  95. if (!empty($nativeRoutes)) {
  96. $lastNativeRoute = $nativeRoutes[count($nativeRoutes) - 1];
  97. $result['status'] = $this->getStatus($nativeData);
  98. if ($result['status'] == '已收件') {
  99. $result['received_at'] = $lastNativeRoute->time;
  100. }
  101. $result['transfer_status'] = $this->getTransferStatus($nativeRoutes);
  102. $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
  103. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  104. $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastNativeRoute ? $lastNativeRoute->time : null);
  105. $result['exception_type'] = $exceptionData['exception_type'];
  106. $result['exception'] = $exceptionData['exception'];
  107. } else {
  108. $result['status'] = null;
  109. $result['transfer_status'] = [];
  110. }
  111. if (!array_key_exists('status', $result)) {
  112. $result['status'] = null;
  113. $result['transfer_status'] = [];
  114. }
  115. //如果没有发现额外的异常,且查询到物流轨迹,将异常置为无
  116. if (!array_key_exists('exception', $result)
  117. && !array_key_exists('exception_type', $result)
  118. && array_key_exists('transfer_status', $result)
  119. ) {
  120. $result['exception_type'] = '无';
  121. $result['exception'] = '否';
  122. }
  123. return $result;
  124. }
  125. }
  126. /**
  127. * @param $nativeData
  128. * @return string
  129. */
  130. private function getStatus($nativeData): string
  131. {
  132. $status = null;
  133. switch ($nativeData->status) {
  134. case 'GOT':
  135. $status = '已揽收';
  136. break;
  137. case 'TRANSIT':
  138. $status = '在途';
  139. break;
  140. case 'SIGNED':
  141. $status = '已收件';
  142. break;
  143. case 'RETURN':
  144. $status = '返回中';
  145. break;
  146. case 'SIGNFAIL':
  147. $status = '无';
  148. break;
  149. default:
  150. $status = '无';
  151. }
  152. return $status;
  153. }
  154. /**
  155. * @param $nativeRoutes
  156. * @return array
  157. */
  158. private function getTransferStatus($nativeRoutes): array
  159. {
  160. $transferStatus = [];
  161. foreach ($nativeRoutes as $nativeRoute) {
  162. $item = [];
  163. $item['accept_time'] = $nativeRoute->time;
  164. $item['accept_address'] = $nativeRoute->description;
  165. $item['remark'] = "";
  166. $transferStatus[] = $item;
  167. }
  168. return $transferStatus;
  169. }
  170. }