Prechádzať zdrojové kódy

refactor(aliyun-wuliu): 重构物流接口实现

- 将物流查询逻辑从 ExtensionWuLiuEntity 移至独立的 LogisticsService 类
- ExtensionWuLiuEntity 不再实现 LogisticsInterface 接口- queryLogistics 方法由 private 改为 public 以支持服务调用
- 新增 LogisticsService 实现标准物流接口,负责处理物流信息查询
- 更新 Extension 类中的依赖绑定,使用新的 LogisticsService
- 在扩展启动时添加调试日志记录
runphp 5 mesiacov pred
rodič
commit
09c3046eae

+ 2 - 20
src/Entity/ExtensionWuLiuEntity.php

@@ -12,26 +12,8 @@ use think\facade\Cache;
 use think\Model;
 use function SixShop\Core\throw_logic_exception;
 
-class ExtensionWuLiuEntity extends BaseEntity implements LogisticsInterface
+class ExtensionWuLiuEntity extends BaseEntity
 {
-    public function logisticsInformation(string $number, string $type = ''): LogisticsInformationResponse
-    {
-        $entity = $this->where('number', $number)->when($type, function (Model $query, $type) {
-            $query->where('type', $type);
-        })->findOrEmpty();
-        if (!$entity->isEmpty() && ($entity->is_sign == 1 || $entity->update_time > time() - 600)) {
-            // 已签收或10分钟内更新过,直接返回
-        } else {
-            $entity = $this->queryLogistics($number, $type, $entity);
-        }
-        return new LogisticsInformationResponse(
-            Extension::EXTENSION_ID,
-            $entity->number,
-            $entity->type,
-            $entity->toArray()
-        );
-    }
-
     public function getWuliuInfo(int $userID, string $no, string $type = ''): array
     {
         $entity = $this->where('number', $no)->when($type, function (Model $query, $type) {
@@ -52,7 +34,7 @@ class ExtensionWuLiuEntity extends BaseEntity implements LogisticsInterface
         return $this->queryLogistics($no, $type, $entity)->toArray();
     }
 
-    private function queryLogistics(string $number, string $type, self $entity): self
+    public function queryLogistics(string $number, string $type, self $entity): self
     {
         $result = WuLiuClient::kdi($number, $type);
         $entity->number = $result->number;

+ 4 - 1
src/Extension.php

@@ -4,9 +4,11 @@ declare(strict_types=1);
 namespace SixShop\AliyunWuliu;
 
 use SixShop\AliyunWuliu\Entity\ExtensionWuLiuEntity;
+use SixShop\AliyunWuliu\Service\LogisticsService;
 use SixShop\Core\Contracts\LogisticsInterface;
 use SixShop\Core\ExtensionAbstract;
 use think\App;
+use think\facade\Log;
 
 class Extension extends ExtensionAbstract
 {
@@ -24,6 +26,7 @@ class Extension extends ExtensionAbstract
     public function boot(): void
     {
         parent::boot();
-        $this->app->bind(LogisticsInterface::class, ExtensionWuLiuEntity::class);
+        Log::debug('aliyun_wuliu boot');
+        $this->app->bind(LogisticsInterface::class, LogisticsService::class);
     }
 }

+ 34 - 0
src/Service/LogisticsService.php

@@ -0,0 +1,34 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\AliyunWuliu\Service;
+
+use SixShop\AliyunWuliu\Entity\ExtensionWuLiuEntity;
+use SixShop\AliyunWuliu\Extension;
+use SixShop\Core\Contracts\LogisticsInformationResponse;
+use SixShop\Core\Contracts\LogisticsInterface;
+use think\Model;
+
+class LogisticsService implements LogisticsInterface
+{
+    public function __construct(private ExtensionWuLiuEntity $extensionWuLiuEntity)
+    {
+    }
+
+    public function logisticsInformation(string $number, string $type = ''): LogisticsInformationResponse
+    {
+        $entity = $this->extensionWuLiuEntity->where('number', $number)->when($type, function (Model $query, $type) {
+            $query->where('type', $type);
+        })->findOrEmpty();
+        if (!$entity->isEmpty() && ($entity->is_sign == 1 || $entity->update_time > time() - 600)) {
+            // 已签收或10分钟内更新过,直接返回
+        } else {
+            $entity = $this->extensionWuLiuEntity->queryLogistics($number, $type, $entity);
+        }
+        return new LogisticsInformationResponse(
+            Extension::EXTENSION_ID,
+            $entity->number,
+            $entity->type,
+            $entity->toArray()
+        );
+    }
+}