LogisticZopSync.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Jobs;
  3. use App\library\zop\ZopClient;
  4. use App\library\zop\ZopProperties;
  5. use App\library\zop\ZopRequest;
  6. use App\Services\OrderPackageReceivedSyncService;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Carbon;
  13. class LogisticZopSync implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $logistic_number;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @param $logistic_number
  21. */
  22. public function __construct($logistic_number)
  23. {
  24. $this->logistic_number = $logistic_number;
  25. }
  26. /**
  27. * Execute the job.
  28. *
  29. * @return void
  30. */
  31. public function handle()
  32. {
  33. //
  34. ini_set('max_execution_time', 10);
  35. $zopResult = [];
  36. $url = config('api_logistic.ZTO.url');
  37. $xAppKey = config('api_logistic.ZTO.x-appKey');
  38. $appSecret = config('api_logistic.ZTO.appSecret');
  39. $properties = new ZopProperties($xAppKey, $appSecret);
  40. $client = new ZopClient($properties);
  41. $request = new ZopRequest();
  42. $request->setUrl($url);
  43. $request->setBody(json_encode([
  44. 'billCode' => $this->logistic_number,
  45. ]));
  46. $response = json_decode($client->execute($request));
  47. if ($response->status) {
  48. $zopResult[] = [
  49. 'routes' => $response->result,
  50. 'logisticNum' => $this->logistic_number,
  51. ];
  52. }
  53. $result = $this->transformRoutes($zopResult);
  54. /**
  55. * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
  56. */
  57. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  58. $orderPackageReceivedSyncService->update($result);
  59. }
  60. /**
  61. * 转换快递路由信息
  62. * @param array $routs 快递路由
  63. * @return array
  64. */
  65. public function transformRoutes(array $routs): array
  66. {
  67. $result = [];
  68. foreach ($routs as $route) {
  69. $resultItem = [];
  70. $resultItem['logistic_number'] = $route['logisticNum'];
  71. $itemRoutes = $route['routes'];
  72. if (empty($itemRoutes)) {
  73. continue;
  74. }
  75. $lastRoute = $itemRoutes[count($itemRoutes) - 1];
  76. switch ($lastRoute->scanType) {
  77. case '收件':
  78. $resultItem['status'] = '已揽收';
  79. break;
  80. case '到件':
  81. case '发件':
  82. $resultItem['status'] = '在途';
  83. break;
  84. case 'ARRIVAL':
  85. case '派件':
  86. $resultItem['status'] = '派送中';
  87. break;
  88. case 'SIGNED':
  89. case '签收':
  90. $resultItem['status'] = '已收件';
  91. $resultItem['received_at'] = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
  92. break;
  93. default:
  94. break;
  95. }
  96. $transfer_status = [];
  97. foreach ($itemRoutes as $item) {
  98. $data = [];
  99. $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->toDateTimeString();
  100. $scanSite = $item->scanSite;
  101. $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
  102. $data['remark'] = $item->scanType;
  103. $transfer_status[] = $data;
  104. }
  105. $resultItem['transfer_status'] = $transfer_status;
  106. $result[] = $resultItem;
  107. }
  108. return $result;
  109. }
  110. }