소스 검색

feat(backend): 优化阿里云物流扩展

- 重构 Config 类,添加 app_code 属性
- 在 Extension 类中定义 EXTENSION_ID 常量
- 更新 WuLiuClient 类:
  - 修改 kdi 方法返回类型为 object  - 添加 getExpressList 方法获取快递公司列表
- 更新测试用例
runphp 6 달 전
부모
커밋
0102fb5cde
4개의 변경된 파일38개의 추가작업 그리고 12개의 파일을 삭제
  1. 7 2
      src/Config.php
  2. 1 0
      src/Extension.php
  3. 23 9
      src/WuLiuClient.php
  4. 7 1
      test/WuLiuClientTest.php

+ 7 - 2
src/Config.php

@@ -5,12 +5,17 @@ namespace SixShop\AliyunWuliu;
 
 use SixShop\System\Trait\ConfigTrait;
 
+/**
+ * @property string $app_key
+ * @property string $app_secret
+ * @property string $app_code
+ */
 class Config
 {
     use ConfigTrait;
 
-    public function getAppCode(): string
+    public function getExtensionID(): string
     {
-        return $this->getConfig('app_code');
+        return Extension::EXTENSION_ID;
     }
 }

+ 1 - 0
src/Extension.php

@@ -7,6 +7,7 @@ use SixShop\Core\ExtensionAbstract;
 
 class Extension extends ExtensionAbstract
 {
+    public const EXTENSION_ID = 'aliyun_wuliu';
     protected function getBaseDir(): string
     {
         return dirname(__DIR__);

+ 23 - 9
src/WuLiuClient.php

@@ -3,9 +3,9 @@ declare(strict_types=1);
 
 namespace SixShop\AliyunWuliu;
 
-use SixShop\Core\Helper;
 use Symfony\Component\HttpClient\HttpClient;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
+use function SixShop\Core\throw_logic_exception;
 
 class WuLiuClient
 {
@@ -19,18 +19,17 @@ class WuLiuClient
         $this->httpClient = HttpClient::create([
             'base_uri' => self::GATEWAY,
             'headers' => [
-                'Authorization' => 'APPCODE ' . $this->config->getAppCode(),
+                'Authorization' => 'APPCODE ' . $this->config->app_code,
             ]
         ]);
     }
 
     /**
      * 获取物流信息
-     * @param string $no 快递单号
+     * @param string $no 快递单号 快递单号 【顺丰和丰网、中通等请输入单号 : 收件人或寄件人手机号后四位。例如:123456789:1234】
      * @param string $type 快递公司编码
-     * @return array
      */
-    public function kdi(string $no, string $type = ''): array
+    public function kdi(string $no, string $type = ''): object
     {
         $response = $this->httpClient->request('GET', 'kdi', [
             'query' => [
@@ -38,11 +37,26 @@ class WuLiuClient
                 'type' => $type,
             ]
         ]);
-        $result = json_decode($response->getContent(), true);
-        if ($result['status'] == 0) {
-            return $result['result'];
+        $result = json_decode($response->getContent());
+        if ($result->status == 0) {
+            return $result->result;
         } else {
-            Helper::throw_logic_exception($result['msg']);
+            // 201:快递单号错误 203:快递公司不存在 204:快递公司识别失败 205:没有信息 207:该单号被限制,错误单号
+            throw_logic_exception($result->msg);
+        }
+    }
+
+    /**
+     * 获取快递公司列表
+     */
+    public function getExpressList(): object
+    {
+        $response = $this->httpClient->request('GET', 'getExpressList');
+        $result = json_decode($response->getContent());
+        if ($result->status == 200) {
+            return $result->result;
+        } else {
+            throw_logic_exception($result->msg);
         }
     }
 }

+ 7 - 1
test/WuLiuClientTest.php

@@ -16,6 +16,12 @@ class WuLiuClientTest extends TestCase
     public function testKdi(): void
     {
         $result = $this->client->kdi('DPK202576722639');
-        $this->assertIsArray($result);
+        dump($result);
+    }
+
+    public function testGetExpressList(): void
+    {
+        $result = $this->client->getExpressList();
+        dump($result);
     }
 }