Просмотр исходного кода

圆通快递同步添加日志
阿里公共快递接口

hu hao 4 лет назад
Родитель
Сommit
6250dd9daa

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

@@ -1510,4 +1510,12 @@ 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);
+    }
 }

+ 5 - 1
app/Jobs/LogisticYTOSync.php

@@ -53,6 +53,10 @@ class LogisticYTOSync implements ShouldQueue
         $nativeResponse = $this->logisticYTOService->query($this->logistic_number);
         $formattedData = $this->logisticYTOService->format($nativeResponse);
         $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
-        $this->orderPackageReceivedSyncService->update([$formattedData]);
+        if ($formattedData['logistic_number']){
+            $this->orderPackageReceivedSyncService->update([$formattedData]);
+        }else{
+            LogService::log(LogisticYTOService::class, "YTO快递无快递异常", $formattedData);
+        }
     }
 }

+ 4 - 2
app/Providers/AppServiceProvider.php

@@ -127,6 +127,7 @@ use App\Services\OrderPackageExceptionTypeCountingRecordService;
 use App\Services\LogisticYDService;
 use App\Services\ForeignZhenCangService;
 use App\Services\StorageService;
+use App\Services\LogisticAliJiSuApiService;
 
 class AppServiceProvider extends ServiceProvider
 {
@@ -214,17 +215,17 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('FeatureService',FeatureService::class);
         app()->singleton('ForeignHaiRoboticsService',ForeignHaiRoboticsService::class);
         app()->singleton('ForeignZhenCangService',ForeignZhenCangService::class);
-        app()->singleton('LogisticZopService', LogisticZopService::class);
         app()->singleton('InventoryAccountMissionService', InventoryAccountMissionService::class);
         app()->singleton('InventoryCompareService', InventoryCompareService::class);
         app()->singleton('InventoryDailyLogService', InventoryDailyLogService::class);
         app()->singleton('LaborReportsCountingRecordService', LaborReportsCountingRecordService::class);
-        app()->singleton('OrderPackageReceivedSyncRecordService', OrderPackageReceivedSyncRecordService::class);
         app()->singleton('LogService', LogService::class);
+        app()->singleton('LogisticAliJiSuApiService',LogisticAliJiSuApiService::class);
         app()->singleton('LogisticSFService', LogisticSFService::class);
         app()->singleton('LogisticService', LogisticService::class);
         app()->singleton('LogisticYDService', LogisticYDService::class);
         app()->singleton('LogisticYTOService', LogisticYTOService::class);
+        app()->singleton('LogisticZopService', LogisticZopService::class);
         app()->singleton('MaterialBoxService', MaterialBoxService::class);
         app()->singleton('OracleActAllocationDetailService', OracleActAllocationDetailService::class);
         app()->singleton('OracleBasCustomerService', OracleBasCustomerService::class);
@@ -243,6 +244,7 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('OrderPackageCommoditiesService', OrderPackageCommoditiesService::class);
         app()->singleton('OrderPackageCommoditySerialNumberService', OrderPackageCommoditySerialNumberService::class);
         app()->singleton('OrderPackageExceptionTypeCountingRecordService', OrderPackageExceptionTypeCountingRecordService::class);
+        app()->singleton('OrderPackageReceivedSyncRecordService', OrderPackageReceivedSyncRecordService::class);
         app()->singleton('OrderPackageReceivedSyncService', OrderPackageReceivedSyncService::class);
         app()->singleton('OrderPackageService', OrderPackageService::class);
         app()->singleton('OrderService', OrderService::class);

+ 124 - 0
app/Services/LogisticAliJiSuApiService.php

@@ -0,0 +1,124 @@
+<?php
+
+namespace App\Services;
+
+use App\Traits\ServiceAppAop;
+use App\LogisticAliJiSuApi;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\Http;
+
+class LogisticAliJiSuApiService
+{
+    use ServiceAppAop;
+
+    public function query($logistic_number)
+    {
+        $app_code = config('api_logistic.AliJiSu.prod.app-code');
+        $type = config('api_logistic.AliJiSu.prod.type');
+        $host = config('api_logistic.AliJiSu.prod.search.host');
+        $path = config('api_logistic.AliJiSu.prod.search.path');
+        $method = config('api_logistic.AliJiSu.prod.method');
+        $headers = array();
+        array_push($headers, "Authorization:APPCODE " . $app_code);
+        array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
+        $query='number='.$logistic_number.'&type='.$type;
+        $bodys = "null";
+        $url = $host . $path . "?" . $query;
+//        $response = Http::withHeaders($headers)->get($url);
+//        return json_decode($response->body());
+
+        $curl = curl_init();
+        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
+        curl_setopt($curl, CURLOPT_URL, $url);
+        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
+        curl_setopt($curl, CURLOPT_FAILONERROR, false);
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+//        curl_setopt($curl, CURLOPT_HEADER, true);
+        if (1 == strpos("$".$host, "https://"))
+        {
+            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
+            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
+        }
+        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
+        return json_decode(curl_exec($curl));
+    }
+
+    public function format($response): array
+    {
+        $result = [];
+        if (!isset($response)) {return [];}
+        else {
+            try {
+                if ($response->result->number??false)$result['logistic_number'] = $response->result->number;
+            } catch (\Exception $e) {
+                LogService::log(LogisticYTOService::class, "AliJiSu快递信息异常", $response);
+            }
+            $list=$response->result->list;
+            if (!empty($list) && is_array($list)) {
+                $lastNativeRoute = $list[0];
+                $result['status'] = $this->getStatus($response);
+                if ($result['status'] == '已收件') $result['received_at'] = $lastNativeRoute->time;
+                $result['transfer_status'] = $this->getTransferStatus($list);
+                $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
+                $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
+                $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastNativeRoute ? $lastNativeRoute->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'] = '否';}
+            return $result;
+        }
+    }
+    /**
+     * @param $nativeData
+     * @return string
+     */
+    private function getStatus($nativeData): string
+    {
+        $status = null;
+        switch ($nativeData->result->deliverystatus) {
+            case '1':
+                $status = '在途';
+                break;
+            case '2':
+                $status = '派送中';
+                break;
+            case '3':
+                $status = '已收件';
+                break;
+            case '4':
+                $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->time;
+            $item['accept_address'] = $nativeRoute->status;
+            $item['remark'] = "";
+            $transferStatus[] = $item;
+        }
+        return $transferStatus;
+    }
+
+
+}

+ 13 - 0
config/api_logistic.php

@@ -152,6 +152,19 @@ return [
             ],
         ],
     ],
+    'AliJiSu' => [
+        'prod' => [
+            'app-key' => '203946797',
+            'app-secret' => '5jlb4OfKkVFAbEXa7ebvnSQt5wCLQqWb',
+            'app-code' => '531dca65c4df41bfaa0c58cb52f444db',
+            'type' => 'auto',
+            'method' => 'GET',
+            'search' => [
+                'host' => 'https://jisukdcx.market.alicloudapi.com',
+                'path' => '/express/query',
+            ],
+        ],
+    ],
     'init_date' => '2021-05-17 23:59:59',
     'days' => 2,
 ];

+ 40 - 0
tests/Services/LogisticAliJiSuApiService/FormatTest.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace Tests\Services\LogisticAliJiSuApiService;
+
+use App\Services\LogisticYDService;
+use App\Services\LogisticYTOService;
+use BeyondCode\DumpServer\DumpServerServiceProvider;
+use Tests\TestCase;
+use App\LogisticYD;
+use App\Traits\TestMockSubServices;
+
+class FormatTest extends TestCase
+{
+    use TestMockSubServices;
+
+    /** @var LogisticYTOService $service */
+    public $service;
+    private $data;
+    private $amount = 2;
+
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('LogisticAliJiSuApiService');
+    }
+
+    /**
+     * @test
+     */
+    public function format_test()
+    {
+        $response=$this->service->query('9882947888708');
+        dump($response);
+        if ($response && $response->status==0){
+            $result = $this->service->format($response);
+            dd($result);
+        }
+
+    }
+}

+ 36 - 0
tests/Services/LogisticAliJiSuApiService/QueryTest.php

@@ -0,0 +1,36 @@
+<?php
+
+
+namespace Tests\Services\LogisticAliJiSuApiService;
+
+use App\OrderPackage;
+use App\Services\LogisticYDService;
+use App\Services\LogisticYTOService;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+class QueryTest extends TestCase
+{
+    /**
+     * @var LogisticYTOService $service
+     */
+    private $service;
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->service = app('LogisticAliJiSuApiService');
+
+    }
+
+    /**
+     * @test
+     */
+    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);
+    }
+}