12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Http\Middleware;
- use Closure;
- use Illuminate\Cache\RateLimiter;
- use Illuminate\Cache\RateLimiting\Unlimited;
- use Illuminate\Http\Exceptions\HttpResponseException;
- use Illuminate\Http\Exceptions\ThrottleRequestsException;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Middleware\ThrottleRequests;
- use Illuminate\Support\Arr;
- use Illuminate\Support\InteractsWithTime;
- use Illuminate\Support\Str;
- use Symfony\Component\HttpFoundation\Response;
- class LimitRequestMiddleware extends ThrottleRequests
- {
- /**
- * The rate limiter instance.
- *
- * @var \Illuminate\Cache\RateLimiter
- */
- protected $limiter;
- /**
- * Create a new request throttler.
- *
- * @param \Illuminate\Cache\RateLimiter $limiter
- * @return void
- */
- public function __construct(RateLimiter $limiter)
- {
- $this->limiter = $limiter;
- }
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @param int|string $maxAttempts
- * @param float|int $decayMinutes
- * @param string $prefix
- * @return \Symfony\Component\HttpFoundation\Response
- *
- * @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
- */
- public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
- {
- try {
- if (is_string($maxAttempts)
- && func_num_args() === 3
- && ! is_null($limiter = $this->limiter->limiter($maxAttempts))) {
- return $this->handleRequestUsingNamedLimiter($request, $next, $maxAttempts, $limiter);
- }
- return $this->handleRequest(
- $request,
- $next,
- [
- (object) [
- 'key' => $prefix.$this->resolveRequestSignature($request),
- 'maxAttempts' => $this->resolveMaxAttempts($request, $maxAttempts),
- 'decayMinutes' => $decayMinutes,
- 'responseCallback' => null,
- ],
- ]
- );
- }catch (\Exception $exception){
- return response()->json([
- 'code'=>50000,
- 'status' => 'fail',
- 'message' => '验证码已发送,请在一分钟后请求',
- 'data' =>null,
- 'error' => null,
- ]);
- }
- }
- }
|