ソースを参照

feat(aliyun-wuliu): 增加物流查询权限控制和频率限制

-为物流查询接口添加 auth 中间件权限验证
- 在 getWuliuInfo 方法中增加用户 ID 参数以支持用户级查询统计- 实现基于 Redis 缓存的每日查询次数限制(每人每天最多 100 次)
- 查询超频时抛出逻辑异常提示用户次日再试
- 更新控制器以传递用户 ID 至实体方法
- 调整方法签名以确保类型安全和参数一致性
runphp 5 ヶ月 前
コミット
97cb1a6d7a
3 ファイル変更12 行追加3 行削除
  1. 1 1
      route/api.php
  2. 1 1
      src/Controller/IndexController.php
  3. 10 1
      src/Entity/ExtensionWuLiuEntity.php

+ 1 - 1
route/api.php

@@ -4,4 +4,4 @@ declare(strict_types=1);
 use SixShop\AliyunWuliu\Controller\IndexController;
 use think\facade\Route;
 
-Route::get('',  [IndexController::class, 'index']);
+Route::get('',  [IndexController::class, 'index'])->middleware(['auth']);

+ 1 - 1
src/Controller/IndexController.php

@@ -18,6 +18,6 @@ class IndexController
             'no' => 'require|alphaNum',
             'type' => 'alphaNum'
         ])->check($params);
-        return success_response($entity->getWuliuInfo($params['no'], $params['type']));
+        return success_response($entity->getWuliuInfo((int)$request->userID,$params['no'], $params['type']));
     }
 }

+ 10 - 1
src/Entity/ExtensionWuLiuEntity.php

@@ -5,12 +5,14 @@ namespace SixShop\AliyunWuliu\Entity;
 
 use SixShop\AliyunWuliu\Facade\WuLiuClient;
 use SixShop\Core\Entity\BaseEntity;
+use think\facade\Cache;
 use think\Model;
+use function SixShop\Core\throw_logic_exception;
 
 class ExtensionWuLiuEntity extends BaseEntity
 {
 
-    public function getWuliuInfo(string $no, string $type = ''): array
+    public function getWuliuInfo(int $userID, string $no, string $type = ''): array
     {
         $entity = $this->where('number', $no)->when($type, function (Model $query, $type) {
             $query->where('type', $type);
@@ -19,6 +21,13 @@ class ExtensionWuLiuEntity extends BaseEntity
             // 已签收或一小时内更新过,直接返回
             return $entity->toArray();
         }
+        // 一个人一天最多查询100次
+        $times = Cache::remember('wuliu_'.date('Ymd').'_' . $userID, function () {
+            return Cache::get('wuliu_'.date('Ymd').'_' . $userID, 0) + 1;
+        }, 86400);
+        if ($times > 100) {
+            throw_logic_exception('查询太过频繁,请明天再试');
+        }
         $result = WuLiuClient::kdi($no, $type);
         $entity->number = $result->number;
         $entity->type = $result->type;