Эх сурвалжийг харах

顺丰快递查询接入完成

ANG YU 5 жил өмнө
parent
commit
36bb21ba64

+ 59 - 25
app/Services/LogisticQiaoSFService.php

@@ -4,48 +4,82 @@
 namespace App\Services;
 
 
-use Exception;
 use Illuminate\Support\Facades\Http;
 
 class LogisticQiaoSFService
 {
     public function get($logisticNums)
     {
-        //TODO 将$logisticNums以10个位单位进行分割,返回二维数组
-
-        //TODO 遍历二维数组批量查询顺丰接口
-
-        //TODO 将查询到的结果整合到一起,更新order_packages.received_at
         $url = config('api_logistic.SF.url');
         $head = config('api_logistic.SF.head');
         $checkWord = config('api_logistic.SF.checkWord');
+        $maxSize = config('api_logistic.SF.maxSize');
+
+        // 将$logisticNums以10个位单位进行分割,返回二维数组
+        $logisticNums_size_10 = array_chunk($logisticNums, $maxSize, true);
+
+        // 遍历二维数组批量查询顺丰接口
+        //将查询到的结果整合到一起,更新order_packages.received_at
+        $result = [];
+        foreach ($logisticNums_size_10 as $numbers) {
+            $numbers = implode(',', $logisticNums);
+            $result_10 =  $this->getResultFromSF($head, $numbers, $checkWord, $url);
+            $result =  array_merge($result_10);
+        }
+        return $result;
+    }
+
+    /**
+     * @param string $head
+     * @param string $number
+     * @param string $checkWord
+     * @param string $url
+     * @return array
+     */
+    public function getResultFromSF(string $head, string $number, string $checkWord, string $url): array
+    {
+        $xml = $this->bulidXmlStr($head, $number);
+        $checkingJson = $xml . $checkWord;
+        $verifyCode = base64_encode(md5($checkingJson, true));
+        $response = Http::withHeaders(['Content-Type' => 'text/xml'])->get($url, ['xml' => $xml, 'verifyCode' => $verifyCode]);
+        $routeResponses = get_object_vars(simplexml_load_string($response)->Body)['RouteResponse'];
+        $result = [];
+        foreach ($routeResponses as $routeResponse) {
+            $routeResponse = get_object_vars($routeResponse);
+            $data['mailno'] = $routeResponse['@attributes']['mailno'];
+            $data['Route'] = [];
+            $routes = $routeResponse['Route'];
+            foreach ($routes as $route) {
+                $route = get_object_vars($route)['@attributes'];
+                if ($route['opcode'] == 50) {
+                    $data['Route']['transfer_status'] = $route['remark'];
+                    $data['Route']['status'] = '已揽收';
+                    $data['Route']['received_at'] = $route['accept_time'];
+                }
+            }
+            $result[] = $data;
+        }
+        return $result;
+    }
+
+    /**
+     * @param string $head
+     * @param string $number
+     * @return string
+     */
+    public function bulidXmlStr(string $head, string $number): string
+    {
         $xml = <<<xml
 <?xml version="1.0" encoding="utf-8" ?>
     <Request service='RouteService' lang='zh-CN'>
     <Head>$head</Head>
     <Body>
     <RouteRequest
-    tracking_type='1'
-    method_type='1'
-    tracking_number='XJFS_071100251'/>
+    tracking_number="{$number}" tracking_type='1' method_type='1'
+    />
     </Body>
     </Request>
 xml;
-        $checkingJson=$xml.$checkWord;
-        $verifyCode=base64_encode(md5($checkingJson,true));
-        $response = Http::withHeaders(['Content-Type'=>'text/xml'])->get($url, ['xml' => $xml, 'verifyCode' => $verifyCode]);
-        //return $response->body();
-        preg_match('/<Route (.*)\/>/',$response->body(),$match);
-        if (count($match) < 2) throw new Exception('');
-        $arr = explode('" ',$match[1]);
-        $result = [];
-        foreach ($arr as $item) {
-            $strArr = explode('"',$item);
-            if (count($strArr) < 2)throw new Exception('');
-            $column = $strArr[0];
-            unset($strArr[0]);
-            $result[] = [rtrim($column,"=")=>implode("",$strArr)];
-        }
-        return $result;
+        return $xml;
     }
 }

+ 2 - 1
config/api_logistic.php

@@ -1,8 +1,9 @@
 <?php
 return [
     'SF' => [
-        'url' =>  'https://sfapi-sbox.sf-express.com/sfexpressService',
+        'url' =>  'https://bspsw.sf-express.com/sfexpressService',
         'head' =>  'BSGYLGL',
         'checkWord' =>  'DcauTQEavq5qqpFaYmltzR7gzVCTjvtz',
+        'maxSize' => 10,
     ],
 ];

+ 1 - 1
tests/Services/LogisticQiaoSFService/LogisticQiaoSFServiceTest.php

@@ -21,7 +21,7 @@ class LogisticQiaoSFServiceTest extends TestCase
      */
     public function testGet()
     {
-        $response = $this->logisticQiaoSFService->get([]);
+        $response = $this->logisticQiaoSFService->get(['SF1038651915891','SF1038651413847','SF1038611050071']);
         dd($response);
     }
 }