LogisticZopService.php 3.5 KB

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