CheckAuthMiddleware.php 899 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Enum\ErrorEnum;
  5. use App\Http\Utils\BaseUtil;
  6. use Closure;
  7. use Tool\ShanTaoTool\BaseAuth\AdminAuth;
  8. class CheckAuthMiddleware
  9. {
  10. /**
  11. * Handle an incoming request.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @param \Closure $next
  15. * @return mixed
  16. */
  17. public function handle($request, Closure $next)
  18. {
  19. $token = $request->header("wechatToken");
  20. //判断tocken是否存在
  21. if(!$token){
  22. throw new CommonException(ErrorEnum::VALIDATE_TOKEN);
  23. }
  24. //判断缓存中是否存在
  25. $userId = BaseUtil::getCacheFromRedis($token);
  26. if(!$userId){
  27. throw new CommonException(ErrorEnum::VALIDATE_TOKEN);
  28. }
  29. $request->headers->set("user_id",$userId);
  30. return $next($request);
  31. }
  32. }