浏览代码

请求次数

shish 8 月之前
父节点
当前提交
a75c009fa0
共有 2 个文件被更改,包括 31 次插入0 次删除
  1. 7 0
      app-ghs/controllers/CustomController.php
  2. 24 0
      common/components/util.php

+ 7 - 0
app-ghs/controllers/CustomController.php

@@ -694,6 +694,13 @@ class CustomController extends BaseController
     //从其它店同步客户 shizhongqi 2022.6.9
     public function actionCopyCustomers()
     {
+        //限制请求次数
+        $shopId = $this->shopId;
+        util::perDayCommitTimeLimit($shopId, 3);
+
+        //限制频繁请求
+        util::checkRepeatCommit($this->adminId, 6);
+
         $get = Yii::$app->request->get();
         $fromShopId = $get['fromShopId'] ?? 0;
         $fromShop = ShopClass::getById($fromShopId, true);

+ 24 - 0
common/components/util.php

@@ -417,6 +417,30 @@ class util
         Yii::$app->redis->executeCommand('SETEX', [$cacheKey, $second, 'has']);
     }
 
+    //每天限制请求次数
+    public static function perDayCommitTimeLimit($uniqueId, $num = 0)
+    {
+        if ($num == 0) {
+            //0表示不限制请求次数
+            return true;
+        }
+        $second = 86400;
+        $ctl = Yii::$app->controller->id;
+        $action = Yii::$app->controller->action->id;
+        $cacheKey = "perDayCommitTimeLimit_" . $ctl . "_" . $action . "_" . $uniqueId;
+        $hasNum = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+        if (!empty($hasNum) && $hasNum > 0) {
+            if ($hasNum > $num) {
+                util::fail("已拉取{$num}次,请明天再试");
+            }
+            $newNum = bcadd($hasNum, 1);
+            Yii::$app->redis->executeCommand('SETEX', [$cacheKey, $second, $newNum]);
+        } else {
+            Yii::$app->redis->executeCommand('SETEX', [$cacheKey, $second, 1]);
+        }
+        return true;
+    }
+
     /**
      * 用 mainId 验证操作权限
      * @param array $arr 模型数据数组