فهرست منبع

阿里公共快递接口查询

hu hao 4 سال پیش
والد
کامیت
bc1b60eb48

+ 1 - 0
app/Console/Commands/SyncOrderPackageLogisticRouteTask.php

@@ -46,6 +46,7 @@ class SyncOrderPackageLogisticRouteTask extends Command
         LogService::log(SyncOrderPackageLogisticRouteTask::class, "同步快递信息定时任务启动", '');
         ini_set('memory_limit', '2226M');
         $this->service = app('OrderPackageReceivedSyncService');
+        $this->service->syncLogisticRouteByAliJiSu();
         $this->service->syncLogisticRoute();
     }
 }

+ 0 - 8
app/Http/Controllers/TestController.php

@@ -1510,12 +1510,4 @@ TEXT;
         $logistic_number = $request->logistic_number;
         \App\Jobs\LogisticZopSync::dispatchNow($logistic_number);
     }
-    public function testApi()
-    {
-        $headers = array();
-        array_push($headers, "Authorization:APPCODE " . 20202020);
-        //根据API的要求,定义相对应的Content-Type
-        array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
-        dd($headers);
-    }
 }

+ 54 - 0
app/Jobs/LogisticAliJiSuSync.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Services\LogisticAliJiSuApiService;
+use App\Services\LogService;
+use App\Services\OrderPackageReceivedSyncService;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class LogisticAliJiSuSync implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    /**
+     * @var $logisticAliJiSuApiService LogisticAliJiSuApiService
+     * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
+     * @var $logistic_number string
+     */
+
+    protected $logistic_number;
+    protected $logisticAliJiSuApiService;
+    protected $orderPackageReceivedSyncService;
+
+    /**
+     * Create a new job instance.
+     *
+     * @param $logistic_number
+     */
+    public function __construct($logistic_number)
+    {
+        $this->logistic_number=$logistic_number;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        LogService::log(LogisticAliJiSuSync::class, "{$this->logistic_number}-JOB-AliJiSu", '');
+        $this->logisticAliJiSuApiService = app('LogisticAliJiSuApiService');
+        $response = $this->logisticAliJiSuApiService->query($this->logistic_number);
+        if ($response && $response->status==0){
+            $format = $this->logisticAliJiSuApiService->format($response);
+            $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
+            if (count($format)>0 && $format['logistic_number']) $this->orderPackageReceivedSyncService->update([$format]);
+        }
+    }
+}

+ 1 - 3
app/Jobs/LogisticYTOSync.php

@@ -15,7 +15,6 @@ use Illuminate\Queue\SerializesModels;
 class LogisticYTOSync implements ShouldQueue
 {
     public $tries = 2;
-
     public $timeout = 10;
 
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@@ -27,7 +26,6 @@ class LogisticYTOSync implements ShouldQueue
      */
 
     protected $logistic_number;
-
     protected $logisticYTOService;
     protected $orderPackageReceivedSyncService;
 
@@ -53,7 +51,7 @@ class LogisticYTOSync implements ShouldQueue
         $nativeResponse = $this->logisticYTOService->query($this->logistic_number);
         $formattedData = $this->logisticYTOService->format($nativeResponse);
         $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
-        if ($formattedData['logistic_number']){
+        if (count($formattedData)>0 && $formattedData['logistic_number']){
             $this->orderPackageReceivedSyncService->update([$formattedData]);
         }else{
             LogService::log(LogisticYTOService::class, "YTO快递无快递异常", $formattedData);

+ 8 - 6
app/Services/LogisticAliJiSuApiService.php

@@ -3,9 +3,6 @@
 namespace App\Services;
 
 use App\Traits\ServiceAppAop;
-use App\LogisticAliJiSuApi;
-use Carbon\Carbon;
-use Illuminate\Support\Facades\Http;
 
 class LogisticAliJiSuApiService
 {
@@ -69,12 +66,17 @@ class LogisticAliJiSuApiService
                 $result['status'] = null;
                 $result['transfer_status'] = [];
             }
-            if (!array_key_exists('status', $result)) {$result['status'] = null;$result['transfer_status'] = [];}
-            //如果没有发现额外的异常,且查询到物流轨迹,将异常置为无
             if (!array_key_exists('exception', $result)
                 && !array_key_exists('exception_type', $result)
                 && array_key_exists('transfer_status', $result)
-            ) {$result['exception_type'] = '无';$result['exception'] = '否';}
+            ) {
+                $result['exception_type'] = '无';
+                $result['exception'] = '否';
+            }
+            if (!array_key_exists('status', $result)) {
+                $result['status'] = null;
+                $result['transfer_status'] = [];
+            }
             return $result;
         }
     }

+ 20 - 0
app/Services/OrderPackageReceivedSyncService.php

@@ -4,6 +4,7 @@
 namespace App\Services;
 
 
+use App\Jobs\LogisticAliJiSuSync;
 use App\Jobs\LogisticSFSync;
 use App\Jobs\LogisticYDSync;
 use App\Jobs\LogisticYTOSync;
@@ -80,6 +81,25 @@ class OrderPackageReceivedSyncService
         });
     }
 
+    public function syncLogisticRouteByAliJiSu()
+    {
+        ini_set('max_execution_time', 60);
+        LogService::log(OrderPackageReceivedSyncService::class, "阿里同步快递信息定时方法", '');
+        $query = OrderPackage::query()
+            ->select(['logistic_number', 'order_id'])
+            ->whereIn('order_id',function ($query){
+                $query->from('orders')->selectRaw('id')->whereIn('logistic_id',function ($builder){
+                    $builder->from('logistics')->selectRaw('id')->where('type','!=','物流')->whereNotIn('belong_company',['顺丰','中通','韵达','圆通']);
+                });
+            });
+        $query = $query->where('sent_at', '>=', now()->subDays(20))
+            ->whereNull('received_at');
+        $query->chunk(200, function ($orderPackages) {
+            foreach ($orderPackages as $orderPackage){
+                if ($orderPackage && $orderPackage->logistic_number)LogisticAliJiSuSync::dispatch($orderPackage->logistic_number);
+            }
+        });
+    }
     /**
      * 根据传递的承运商与快递单号更新快递信息
      * @param array $logisticNumbers 快递单号

+ 2 - 3
tests/Services/LogisticAliJiSuApiService/FormatTest.php

@@ -29,11 +29,10 @@ class FormatTest extends TestCase
      */
     public function format_test()
     {
-        $response=$this->service->query('9882947888708');
-        dump($response);
+        $response=$this->service->query('9882714987042');
         if ($response && $response->status==0){
             $result = $this->service->format($response);
-            dd($result);
+            $this->assertNotEmpty($result);
         }
 
     }

+ 4 - 3
tests/Services/LogisticAliJiSuApiService/QueryTest.php

@@ -29,8 +29,9 @@ class QueryTest extends TestCase
     public function prod_test()
     {
        $response = $this->service->query('4280174475389');
-       dd($response);
-       if (is_object($response))$this->assertEquals('1001', $response->code);
-       if (!is_object($response))$this->assertNotEmpty($response);
+       if ($response->status==0){
+           $this->assertNotEmpty($response->result);
+           $this->assertEquals($response->result->number,'4280174475389');
+       }
     }
 }