Explorar o código

圆通快递接口开发-快递轨迹

hu hao %!s(int64=5) %!d(string=hai) anos
pai
achega
100f911cab

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

@@ -35,6 +35,7 @@ use App\Imports\OrderTrackingImport;
 use App\InventoryAccount;
 use App\Jobs\BatchTaskJob;
 use App\Jobs\DeleteRepetitionSkuItem;
+use App\Jobs\LogisticYTOSync;
 use App\Jobs\OrderCreateInstantBill;
 use App\Jobs\OrderFreeze;
 use App\Jobs\StoreCreateInstantBill;

+ 2 - 0
app/Providers/AppServiceProvider.php

@@ -24,6 +24,7 @@ use App\Services\ForeignHaiRoboticsService;
 use App\Services\InventoryAccountMissionService;
 use App\Services\InventoryCompareService;
 use App\Services\LaborReportsCountingRecordService;
+use App\Services\LogisticYTOService;
 use App\Services\LogService;
 use App\Services\MaterialBoxService;
 use App\Services\OracleBasCustomerService;
@@ -187,6 +188,7 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('LogService',LogService::class);
         app()->singleton('LogisticService',LogisticService::class);
         app()->singleton('LogisticYDService',LogisticYDService::class);
+        app()->singleton('LogisticYTOService',LogisticYTOService::class);
         app()->singleton('MaterialBoxService',MaterialBoxService::class);
         app()->singleton('OracleActAllocationDetailService',OracleActAllocationDetailService::class);
         app()->singleton('OracleBasCustomerService',OracleBasCustomerService::class);

+ 122 - 0
app/Services/LogisticYTOService.php

@@ -0,0 +1,122 @@
+<?php
+
+namespace App\Services;
+
+use App\OrderPackage;
+use App\Traits\ServiceAppAop;
+use Carbon\Carbon;
+use Doctrine\DBAL\Connection;
+use Illuminate\Support\Facades\Http;
+
+class LogisticYTOService
+{
+    use ServiceAppAop;
+
+
+    public function query($logistic_number)
+    {
+        $app_key = config('api_logistic.YTO.prod.app-key');
+        $app_secret = config('api_logistic.YTO.prod.app-secret');
+        $user_id = config('api_logistic.YTO.prod.user_id');
+        $method = config('api_logistic.YTO.prod.method');
+        $format = config('api_logistic.YTO.prod.format');
+        $v = config('api_logistic.YTO.prod.v');
+        $url = config('api_logistic.YTO.prod.search.url');
+        $body = [
+            "Number" => $logistic_number
+        ];
+        $secret=$app_secret.'app_key'.$app_key.'format'.$format.'method'.$method.'timestamp'.Carbon::now()->toDateTimeString().'user_id'.$user_id.'v'.$v;
+        $sign = strtoupper(md5($secret));
+        $headers = [
+            'sign' => $sign,
+            'app_key' => $app_key,
+            'format' => $format,
+            'method' => $method,
+            'timestamp' => Carbon::now()->toDateTimeString(),
+            'user_id' => $user_id,
+            'v' => $v,
+            'param'=>json_encode($body, JSON_UNESCAPED_UNICODE)
+        ];
+
+        $response = Http::asForm()->post($url,$headers);
+        return json_decode($response->body());
+    }
+
+    public function format($response): array
+    {
+        $result = [];
+        if (is_object($response) && $response->code=='1001') {return [];}
+        else {
+            $result['logistic_number'] = $response[0]->waybill_No;
+            if (!empty($response)) {
+                $lastNativeRoute = $response[count($response) - 1];
+                $result['status'] = $this->getStatus($lastNativeRoute);
+                if ($result['status'] == '已收件') $result['received_at'] = $lastNativeRoute->upload_Time;
+                $result['transfer_status'] = $this->getTransferStatus($response);
+                $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
+                $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastNativeRoute ? $lastNativeRoute->upload_Time : null);
+                $result['exception_type'] = $exceptionData['exception_type'];
+                $result['exception'] = $exceptionData['exception'];
+
+            } else {
+                $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['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
+            return $result;
+        }
+    }
+    /**
+     * @param $nativeData
+     * @return string
+     */
+    private function getStatus($nativeData): string
+    {
+        $status = null;
+        switch ($nativeData->infoContent) {
+            case 'ARRIVAL':
+            case 'GOT':
+                $status = '已揽收';
+                break;
+            case 'DEPARTURE':
+            case 'PACKAGE':
+                $status = '在途';
+                break;
+            case 'SENT_SCAN':
+            case 'INBOUND':
+                $status = '派送中';
+                break;
+            case 'SIGNED':
+                $status = '已收件';
+                break;
+            case 'TMS_RETURN':
+                $status = '返回中';
+                break;
+            default:
+                $status = '无';
+        }
+        return $status;
+    }
+    /**
+     * @param $nativeRoutes
+     * @return array
+     */
+    private function getTransferStatus($nativeRoutes): array
+    {
+        $transferStatus = [];
+        foreach ($nativeRoutes as $nativeRoute) {
+            $item = [];
+            $item['accept_time'] = $nativeRoute->upload_Time;
+            $item['accept_address'] = $nativeRoute->processInfo;
+            $item['remark'] = "";
+            $transferStatus[] = $item;
+        }
+        return $transferStatus;
+    }
+}

+ 7 - 1
app/Services/OrderPackageReceivedSyncService.php

@@ -5,6 +5,7 @@ namespace App\Services;
 
 
 use App\Jobs\LogisticYDSync;
+use App\Jobs\LogisticYTOSync;
 use App\Jobs\LogisticZopSync;
 use App\OrderPackage;
 use Carbon\Carbon;
@@ -36,6 +37,11 @@ class OrderPackageReceivedSyncService
         foreach ($YDLogisticNumbers as $logistic_number) {
             LogisticYDSync::dispatch($logistic_number);
         }
+        //更新圆通
+        $YTOLogisticNumbers = $logisticNumbers['YTO'];
+        foreach ($YTOLogisticNumbers as $logistic_number) {
+            LogisticYTOSync::dispatch($logistic_number);
+        }
     }
 
     /**
@@ -117,7 +123,7 @@ class OrderPackageReceivedSyncService
             $query = $query->where('sent_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString())
                 ->whereNull('received_at');
         } else {//查询20天以内的数据
-            $query = $query->where('sent_at', '>=', now()->subDays(20))
+            $query = $query->where('sent_at', '>=', now()->subDays(20)->toDateTimeString())
                 ->whereNull('received_at');
         }
         $result = [];

+ 24 - 0
config/api_logistic.php

@@ -128,6 +128,30 @@ return [
             ],
         ]
     ],
+    'YTO' => [
+        'test' => [
+            'app-key' => 'sF1Jzn',
+            'app-secret' => '1QLlIZ',
+            'user_id' => 'YTOTEST',
+            'method' => 'yto.Marketing.WaybillTrace',
+            'format' => 'JSON',
+            'v' => '1.01',
+            'search' => [
+                'url' => 'http://opentestapi.yto.net.cn/service/waybill_query/v1/gy6h76',
+            ],
+        ],
+        'prod' => [
+            'app-key' => 'CWJVR1',
+            'app-secret' => 'ii3xrv',
+            'user_id' => 'gy6h76',
+            'method' => 'yto.Marketing.WaybillTrace',
+            'format' => 'JSON',
+            'v' => '1.01',
+            'search' => [
+                'url' => 'http://openapi.yto.net.cn/service/waybill_query/v1/gy6h76',
+            ],
+        ],
+    ],
     'init_date' => '2021-05-17 23:59:59',
     'days' => 2,
 ];

+ 35 - 0
tests/Services/LogisticYTOService/FormatTest.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace Tests\Services\LogisticYTOService;
+
+use App\Services\LogisticYDService;
+use Tests\TestCase;
+use App\LogisticYD;
+use App\Traits\TestMockSubServices;
+
+class FormatTest extends TestCase
+{
+    use TestMockSubServices;
+
+    /** @var LogisticYDService $service */
+    public $service;
+    private $data;
+    private $amount = 2;
+
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('LogisticYTOService');
+    }
+
+    /**
+     * @test
+     */
+    public function format_test()
+    {
+        $result = $this->service->format($this->service->query('YT5481469185320'));
+        dump($result);
+        $this->assertNotEmpty($result);
+
+    }
+}

+ 35 - 0
tests/Services/LogisticYTOService/QueryTest.php

@@ -0,0 +1,35 @@
+<?php
+
+
+namespace LogisticYTOService;
+
+use App\OrderPackage;
+use App\Services\LogisticYDService;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+class QueryTest extends TestCase
+{
+    /**
+     * @var $service LogisticYDService
+     */
+    private $service;
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->service = app('LogisticYTOService');
+
+    }
+
+    /**
+     * @test
+     */
+    public function prod_test()
+    {
+//        'YT5481469185320';YT3153735030684
+       $response = $this->service->query('YT5481469185320');
+       if (is_object($response))$this->assertEquals('1001', $response->code);
+       if (!is_object($response))$this->assertNotEmpty($response);
+    }
+}