CheckAuthMiddleware.php 906 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Enum\ErrorEnum;
  5. use Closure;
  6. use Tool\ShanTaoTool\BaseAuth\AdminAuth;
  7. class CheckAuthMiddleware
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @param \Closure $next
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next)
  17. {
  18. //判断用户权限
  19. $path = $request->path();
  20. $token = $request->header("apiToken");
  21. //判断tocken是否存在
  22. if(!$token){
  23. throw new CommonException(ErrorEnum::VALIDATE_TOKEN);
  24. }
  25. $data = AdminAuth::checkUserAuth($path,$token);
  26. //获取到用户ID塞入header头
  27. $request->headers->set("userId",$data["id"]);
  28. $request->headers->set("userAccount",$data["user_name"]);
  29. return $next($request);
  30. }
  31. }