LogisticZopService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Services;
  3. use App\OrderPackage;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\Http;
  6. class LogisticZopService implements LogisticRouteInterface
  7. {
  8. public function query($logistic_number)
  9. {
  10. $url = config('api_logistic.ZTO.url');
  11. $xAppKey = config('api_logistic.ZTO.x-appKey');
  12. $appSecret = config('api_logistic.ZTO.appSecret');
  13. $body = json_encode([
  14. 'billCode' => $logistic_number,
  15. ], JSON_UNESCAPED_UNICODE);
  16. $data_digest = base64_encode(md5($body . $appSecret, TRUE));
  17. $headers = [
  18. 'Content-Type' => 'application/json; charset=UTF-8',
  19. 'x-companyid' => $xAppKey,
  20. 'x-datadigest' => $data_digest,
  21. ];
  22. $response = Http::withHeaders($headers)->withBody($body, 'application/json')->post($url);
  23. return json_decode($response->body());
  24. }
  25. public function format($nativeResponse,$logistic_number)
  26. {
  27. $order_package = OrderPackage::query()->where('logistic_number', $logistic_number)->first();
  28. $result = [
  29. 'logistic_number' => $logistic_number,
  30. 'exception_type' => $order_package->exception_type,
  31. 'exception' => $order_package->exception,
  32. 'status' => $order_package->status,
  33. 'transfer_status' => $order_package->transfer_status,
  34. 'received_at' => $order_package->received_at,
  35. ];
  36. $nativeRoutes = $nativeResponse->result??[];
  37. if (empty($nativeRoutes)) {
  38. $result['exception_type'] = '揽件异常';
  39. $result['exception'] = '是';
  40. return $result;
  41. }
  42. $lastRoute = $nativeRoutes[count($nativeRoutes) - 1]??[];
  43. list($status, $received_at) = $this->getNormalStatusAndReceivedAt($lastRoute);
  44. $result['status'] = $status;
  45. $result['received_at'] = $received_at;
  46. $result['transfer_status'] = $this->getTransferStatus($nativeRoutes);
  47. $result['routes_length'] = count($result['transfer_status']);
  48. /**
  49. * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
  50. */
  51. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  52. $lastRouteDate = Carbon::parse($lastRoute->scanDate / 1000)->addHours(8)->toDateTimeString();
  53. $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastRouteDate);
  54. $result['exception_type'] = $exceptionData['exception_type'];
  55. $result['exception'] = $exceptionData['exception'];
  56. return $result;
  57. }
  58. /**
  59. * 正常的状态与签收时间
  60. */
  61. private function getNormalStatusAndReceivedAt($lastRoute): array
  62. {
  63. $status = null;
  64. $received_at = null;
  65. switch ($lastRoute->scanType) {
  66. case '收件':
  67. $status = '已揽收';
  68. break;
  69. case '到件':
  70. case '发件':
  71. $status = '在途';
  72. break;
  73. case 'ARRIVAL':
  74. case '派件':
  75. $status = '派送中';
  76. break;
  77. case 'SIGNED':
  78. case '签收':
  79. $status = '已收件';
  80. $received_at = Carbon::parse($lastRoute->scanDate / 1000)->addHours(8)->toDateTimeString();
  81. break;
  82. default:
  83. $status = '无';
  84. break;
  85. }
  86. return array($status, $received_at);
  87. }
  88. /**
  89. * @param $nativeRoutes
  90. * @return array
  91. */
  92. private function getTransferStatus($nativeRoutes): array
  93. {
  94. $transfer_status = [];
  95. foreach ($nativeRoutes as $item) {
  96. $data = [];
  97. $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->addHours(8)->toDateTimeString();
  98. $scanSite = $item->scanSite;
  99. $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
  100. $data['remark'] = $item->desc;
  101. $transfer_status[] = $data;
  102. }
  103. return $transfer_status;
  104. }
  105. }