Browse Source

接口限制

lizhen_gitee 7 months ago
parent
commit
3b145cb5c4

+ 24 - 3
addons/unishop/controller/Base.php

@@ -30,13 +30,22 @@ class Base extends Api
      */
     protected $frequently = [];
 
-    public function _initialize()
+    /*public function _initialize()
     {
         parent::_initialize();
 
         $this->loadUniShopLang();
 
         $this->limitVisit();
+    }*/
+
+    public function __construct()
+    {
+        parent::__construct();
+
+        $this->loadUniShopLang();
+
+        $this->limitVisit();
     }
 
     /**
@@ -44,8 +53,8 @@ class Base extends Api
      * @param int $millisecond
      * @ApiInternal
      */
-    public function limitVisit($millisecond = 200) {
-        $millisecond = $this->request->request('millisecond', $millisecond);
+    public function limitVisit_bak($millisecond = 200) {
+        //$millisecond = $this->request->request('millisecond', $millisecond);
 
         // 限制200毫秒 防止1秒两刀 (双击甚至三击,同一时间导致接口请求两次以上)
         $action = $this->request->action();
@@ -60,6 +69,18 @@ class Base extends Api
         }
     }
 
+    public function limitVisit($millisecond = 200) {
+
+        $action = $this->request->action();
+        if (!in_array($action, $this->frequently) && $this->auth && $this->auth->isLogin() && $millisecond > 0) {
+            $apilimit = $this->apiLimit();
+            if(!$apilimit){
+                $this->error('操作过于频繁');
+            }
+
+        }
+    }
+
     /**
      * 加载语言文件
      */

+ 1 - 0
addons/unishop/controller/Category.php

@@ -10,6 +10,7 @@ use think\Db;
 class Category extends Api
 {
 
+    protected $frequently = ['all','menu','inlist'];
     protected $noNeedLogin = ['all','menu','inlist'];
     protected $noNeedRight = ['*'];
 

+ 1 - 1
addons/unishop/controller/Order.php

@@ -35,7 +35,7 @@ class Order extends Base
      * 允许频繁访问的接口
      * @var array
      */
-    protected $frequently = ['getorders'];
+    protected $frequently = ['getorders','detail'];
 
     protected $noNeedLogin = ['count'];
 

+ 1 - 0
addons/unishop/controller/Product.php

@@ -17,6 +17,7 @@ use think\Db;
 class Product extends Base
 {
     protected $noNeedLogin = ['detail', 'lists'];
+    protected $frequently = ['detail', 'lists'];
 
     /**
      * @ApiTitle    (产品详情)

+ 1 - 1
application/api/controller/Hexiao.php

@@ -9,7 +9,7 @@ use app\common\controller\Api;
  */
 class Hexiao extends Api
 {
-    protected $noNeedLogin = ['*'];
+    protected $noNeedLogin = [];
     protected $noNeedRight = ['*'];