LogisticZopService.php 3.9 KB

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