AdminAuthMiddleware.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Servers\PermissionServer;
  4. use Closure;
  5. class AdminAuthMiddleware
  6. {
  7. private $noAuth = [
  8. 'adminApi.token',
  9. 'adminApi.index',
  10. 'adminApi.login',
  11. 'adminApi.logout',
  12. 'adminApi.check_code',
  13. 'adminApi.reset',
  14. ];
  15. /**
  16. * Handle an incoming request.
  17. *
  18. * @param \Illuminate\Http\Request $request
  19. * @param \Closure $next
  20. * @return mixed
  21. */
  22. public function handle($request, Closure $next)
  23. {
  24. $clientRoute = request()->route()->getName();//获取当前路由
  25. if (in_array($clientRoute, $this->noAuth)) {//不需要验证的路由
  26. return $next($request);
  27. }
  28. //获取当前用户
  29. $user = app('auth')->user();
  30. if(!$user){
  31. return response()->json([
  32. 'msg' => '请先登录',
  33. 'code' => 2,
  34. 'data' => []
  35. ]);
  36. }
  37. //进行路由验证
  38. $ret = PermissionServer::verifyAuth($user->{'roles_id'}, $clientRoute);
  39. if (empty($ret) && $user->{'id'} != 1) {
  40. return response()->json([
  41. 'msg' => '暂无权限',
  42. 'code' => 0,
  43. 'data' => []
  44. ]);
  45. }
  46. return $next($request);
  47. }
  48. }