LogisticYTOService.php 4.6 KB

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