LogisticYDService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 ($nativeResponse->code != '0000' || $nativeResponse->data->result=="false") {
  85. return [];
  86. } else {
  87. $nativeData = $nativeResponse->data;
  88. $result['logistic_number'] = $nativeData->mailno;
  89. $nativeRoutes = $nativeData->steps;
  90. if (!empty($nativeRoutes)) {
  91. $lastNativeRoute = $nativeRoutes[count($nativeRoutes) - 1];
  92. $result['status'] = $this->getStatus($nativeData);
  93. if ($result['status'] == '已收件') {
  94. $result['received_at'] = $lastNativeRoute->time;
  95. }
  96. $result['transfer_status'] = $this->getTransferStatus($nativeRoutes);
  97. $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
  98. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  99. $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastNativeRoute ? $lastNativeRoute->time : null);
  100. $result['exception_type'] = $exceptionData['exception_type'];
  101. $result['exception'] = $exceptionData['exception'];
  102. } else {
  103. $result['status'] = null;
  104. $result['transfer_status'] = [];
  105. }
  106. if (!array_key_exists('status', $result)) {
  107. $result['status'] = null;
  108. $result['transfer_status'] = [];
  109. }
  110. //如果没有发现额外的异常,且查询到物流轨迹,将异常置为无
  111. if (!array_key_exists('exception', $result)
  112. && !array_key_exists('exception_type', $result)
  113. && array_key_exists('transfer_status', $result)
  114. ) {
  115. $result['exception_type'] = '无';
  116. $result['exception'] = '否';
  117. }
  118. return $result;
  119. }
  120. }
  121. /**
  122. * @param $nativeData
  123. * @return string
  124. */
  125. private function getStatus($nativeData): string
  126. {
  127. $status = null;
  128. switch ($nativeData->status) {
  129. case 'GOT':
  130. $status = '已揽收';
  131. break;
  132. case 'TRANSIT':
  133. $status = '在途';
  134. break;
  135. case 'SIGNED':
  136. $status = '已收件';
  137. break;
  138. case 'RETURN':
  139. $status = '返回中';
  140. break;
  141. case 'SIGNFAIL':
  142. $status = '无';
  143. break;
  144. default:
  145. $status = '无';
  146. }
  147. return $status;
  148. }
  149. /**
  150. * @param $nativeRoutes
  151. * @return array
  152. */
  153. private function getTransferStatus($nativeRoutes): array
  154. {
  155. $transferStatus = [];
  156. foreach ($nativeRoutes as $nativeRoute) {
  157. $item = [];
  158. $item['accept_time'] = $nativeRoute->time;
  159. $item['accept_address'] = $nativeRoute->description;
  160. $item['remark'] = "";
  161. $transferStatus[] = $item;
  162. }
  163. return $transferStatus;
  164. }
  165. }