LogisticZopService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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)
  26. {
  27. if (is_null($nativeResponse)) {
  28. return [];
  29. }
  30. $nativeRoutes = $nativeResponse->result;
  31. // $nativeMessages = $nativeResponse->messages;
  32. // $nativeStatus = $nativeResponse->status;
  33. // $nativeStatusCode = $nativeResponse->statusCode;
  34. $this->logistic_number = $nativeRoutes[0]->billCode;
  35. $order_package = OrderPackage::query()->where('logistic_number', $this->logistic_number)->first();
  36. $lastRoute = $nativeRoutes[count($nativeRoutes) - 1];
  37. $result = [
  38. 'logistic_number' => $this->logistic_number,
  39. 'exception_type' => $order_package->exception_type,
  40. 'exception'=>$order_package->exception,
  41. 'status' => $order_package->status,
  42. 'transfer_status' => $order_package->transfer_status,
  43. 'received_at' =>$order_package->received_at,
  44. ];
  45. list($status, $received_at) = $this->getNormalStatusAndReceivedAt($lastRoute);
  46. $result['status'] = $status;
  47. $result['received_at'] = $received_at;
  48. $result['transfer_status'] = $this->getTransferStatus($nativeRoutes);
  49. $result['routes_length'] = count($result['transfer_status']);
  50. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  51. $lastRouteDate = Carbon::parse($lastRoute->scanDate / 1000)->addHours(8)->toDateTimeString();
  52. $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastRouteDate);
  53. $result['exception_type'] = $exceptionData['exception_type'];
  54. $result['exception'] = $exceptionData['exception'];
  55. return $result;
  56. }
  57. /**
  58. * 正常的状态与签收时间
  59. */
  60. private function getNormalStatusAndReceivedAt($lastRoute): array
  61. {
  62. $status = null;
  63. $received_at = null;
  64. switch ($lastRoute->scanType) {
  65. case '收件':
  66. $status = '已揽收';
  67. break;
  68. case '到件':
  69. case '发件':
  70. $status = '在途';
  71. break;
  72. case 'ARRIVAL':
  73. case '派件':
  74. $status = '派送中';
  75. break;
  76. case 'SIGNED':
  77. case '签收':
  78. $status = '已收件';
  79. $received_at = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
  80. break;
  81. default:
  82. $status = '无';
  83. break;
  84. }
  85. return array($status, $received_at);
  86. }
  87. /**
  88. * @param $nativeRoutes
  89. * @return array
  90. */
  91. private function getTransferStatus($nativeRoutes): array
  92. {
  93. $transfer_status = [];
  94. foreach ($nativeRoutes as $item) {
  95. $data = [];
  96. $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->addHours(8)->toDateTimeString();
  97. $scanSite = $item->scanSite;
  98. $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
  99. $data['remark'] = $item->desc;
  100. $transfer_status[] = $data;
  101. }
  102. return $transfer_status;
  103. }
  104. }