123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace app\http\middleware\api;
- use app\Request;
- use crmeb\exceptions\ApiException;
- use crmeb\interfaces\MiddlewareInterface;
- use crmeb\services\CacheService;
- class BlockerMiddleware implements MiddlewareInterface
- {
-
- public function handle(Request $request, \Closure $next)
- {
- $uid = $request->uid();
- $key = md5($request->rule()->getRule() . $uid);
- if (!CacheService::setMutex($key)) {
- throw new ApiException('请求太过频繁,请稍后再试');
- }
- $response = $next($request);
- $this->after($response, $key);
- return $response;
- }
-
- public function after($response, $key)
- {
- CacheService::delMutex($key);
- }
- }
|