LogisticYTOService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Services;
  3. use App\Traits\ServiceAppAop;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\Http;
  6. class LogisticYTOService
  7. {
  8. use ServiceAppAop;
  9. public function query($logistic_number)
  10. {
  11. $app_key = config('api_logistic.YTO.prod.app-key');
  12. $app_secret = config('api_logistic.YTO.prod.app-secret');
  13. $user_id = config('api_logistic.YTO.prod.user_id');
  14. $method = config('api_logistic.YTO.prod.method');
  15. $format = config('api_logistic.YTO.prod.format');
  16. $v = config('api_logistic.YTO.prod.v');
  17. $url = config('api_logistic.YTO.prod.search.url');
  18. $body = [
  19. "Number" => $logistic_number
  20. ];
  21. $secret=$app_secret.'app_key'.$app_key.'format'.$format.'method'.$method.'timestamp'.Carbon::now()->toDateTimeString().'user_id'.$user_id.'v'.$v;
  22. $sign = strtoupper(md5($secret));
  23. $headers = [
  24. 'sign' => $sign,
  25. 'app_key' => $app_key,
  26. 'format' => $format,
  27. 'method' => $method,
  28. 'timestamp' => Carbon::now()->toDateTimeString(),
  29. 'user_id' => $user_id,
  30. 'v' => $v,
  31. 'param'=>json_encode($body, JSON_UNESCAPED_UNICODE)
  32. ];
  33. $response = Http::asForm()->post($url,$headers);
  34. return json_decode($response->body());
  35. }
  36. public function format($response,$logistic_number): array
  37. {
  38. $result = [];
  39. if (is_object($response) && $response->code=='1001') {
  40. return [
  41. 'logistic_number' => $logistic_number,
  42. 'exception_type' => '揽件异常',
  43. 'exception' => '是',
  44. ];
  45. }
  46. else {
  47. try {
  48. if (is_array($response))$result['logistic_number'] = $response[0]->waybill_No;
  49. } catch (\Exception $e) {
  50. LogService::log(LogisticYTOService::class, "YTO快递信息异常", $logistic_number);
  51. return [
  52. 'logistic_number' => $logistic_number,
  53. 'exception_type' => '其他',
  54. 'exception' => '是',
  55. ];
  56. }
  57. if (!empty($response) && is_array($response)) {
  58. $lastNativeRoute = $response[count($response) - 1];
  59. $result['status'] = $this->getStatus($lastNativeRoute);
  60. if ($result['status'] == '已收件') $result['received_at'] = $lastNativeRoute->upload_Time;
  61. $result['transfer_status'] = $this->getTransferStatus($response);
  62. $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
  63. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  64. $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastNativeRoute ? $lastNativeRoute->upload_Time : null);
  65. $result['exception_type'] = $exceptionData['exception_type'];
  66. $result['exception'] = $exceptionData['exception'];
  67. } else {
  68. $result['status'] = null;
  69. $result['transfer_status'] = [];
  70. }
  71. if (!array_key_exists('status', $result)) {$result['status'] = null;$result['transfer_status'] = [];}
  72. //如果没有发现额外的异常,且查询到物流轨迹,将异常置为无
  73. if (!array_key_exists('exception', $result)
  74. && !array_key_exists('exception_type', $result)
  75. && array_key_exists('transfer_status', $result)
  76. ) {$result['exception_type'] = '无';$result['exception'] = '否';}
  77. return $result;
  78. }
  79. }
  80. /**
  81. * @param $nativeData
  82. * @return string
  83. */
  84. private function getStatus($nativeData): string
  85. {
  86. $status = null;
  87. switch ($nativeData->infoContent) {
  88. case 'ARRIVAL':
  89. case 'GOT':
  90. $status = '已揽收';
  91. break;
  92. case 'DEPARTURE':
  93. case 'PACKAGE':
  94. $status = '在途';
  95. break;
  96. case 'SENT_SCAN':
  97. case 'INBOUND':
  98. $status = '派送中';
  99. break;
  100. case 'SIGNED':
  101. $status = '已收件';
  102. break;
  103. case 'TMS_RETURN':
  104. $status = '返回中';
  105. break;
  106. default:
  107. $status = '无';
  108. }
  109. return $status;
  110. }
  111. /**
  112. * @param $nativeRoutes
  113. * @return array
  114. */
  115. private function getTransferStatus($nativeRoutes): array
  116. {
  117. $transferStatus = [];
  118. foreach ($nativeRoutes as $nativeRoute) {
  119. $item = [];
  120. $item['accept_time'] = $nativeRoute->upload_Time;
  121. $item['accept_address'] = $nativeRoute->processInfo;
  122. $item['remark'] = "";
  123. $transferStatus[] = $item;
  124. }
  125. return $transferStatus;
  126. }
  127. }