LogisticZopSync.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. sleep(12);
  36. $zopResult = [];
  37. $url = config('api_logistic.ZTO.url');
  38. $xAppKey = config('api_logistic.ZTO.x-appKey');
  39. $appSecret = config('api_logistic.ZTO.appSecret');
  40. $properties = new ZopProperties($xAppKey, $appSecret);
  41. $client = new ZopClient($properties);
  42. $request = new ZopRequest();
  43. $request->setUrl($url);
  44. $request->setBody(json_encode([
  45. 'billCode' => $this->logistic_number,
  46. ]));
  47. $response = json_decode($client->execute($request));
  48. if ($response->status) {
  49. $zopResult[] = [
  50. 'routes' => $response->result,
  51. 'logisticNum' => $this->logistic_number,
  52. ];
  53. }
  54. $result = $this->transformRoutes($zopResult);
  55. /**
  56. * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
  57. */
  58. $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  59. $orderPackageReceivedSyncService->update($result);
  60. }
  61. /**
  62. * 转换快递路由信息
  63. * @param array $routs 快递路由
  64. * @return array
  65. */
  66. public function transformRoutes(array $routs): array
  67. {
  68. $result = [];
  69. foreach ($routs as $route) {
  70. $resultItem = [];
  71. $resultItem['logistic_number'] = $route['logisticNum'];
  72. $itemRoutes = $route['routes'];
  73. if (empty($itemRoutes)) {
  74. continue;
  75. }
  76. $lastRoute = $itemRoutes[count($itemRoutes) - 1];
  77. switch ($lastRoute->scanType) {
  78. case '收件':
  79. $resultItem['status'] = '已揽收';
  80. break;
  81. case '到件':
  82. case '发件':
  83. $resultItem['status'] = '在途';
  84. break;
  85. case 'ARRIVAL':
  86. case '派件':
  87. $resultItem['status'] = '派送中';
  88. break;
  89. case 'SIGNED':
  90. case '签收':
  91. $resultItem['status'] = '已收件';
  92. $resultItem['received_at'] = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
  93. break;
  94. default:
  95. break;
  96. }
  97. $transfer_status = [];
  98. foreach ($itemRoutes as $item) {
  99. $data = [];
  100. $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->toDateTimeString();
  101. $scanSite = $item->scanSite;
  102. $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
  103. $data['remark'] = $item->scanType;
  104. $transfer_status[] = $data;
  105. }
  106. $resultItem['transfer_status'] = $transfer_status;
  107. $result[] = $resultItem;
  108. }
  109. return $result;
  110. }
  111. }