AuthCheck.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * 权限检查
  4. *
  5. * @author fzs
  6. * @Time: 2017/07/14 15:57
  7. * @version 1.0 版本号
  8. */
  9. namespace App\Http\Middleware;
  10. use Illuminate\Http\JsonResponse;
  11. use Closure;
  12. use Illuminate\Contracts\Auth\Guard;
  13. class AuthCheck
  14. {
  15. /**
  16. * The Guard implementation.
  17. *
  18. * @var Guard
  19. */
  20. protected $auth;
  21. /**
  22. * Create a new middleware instance.
  23. *
  24. * @param Guard $auth
  25. * @return void
  26. */
  27. public function __construct(Guard $auth)
  28. {
  29. $this->auth = $auth;
  30. }
  31. /**
  32. * Handle an incoming request.
  33. *
  34. * @param \Illuminate\Http\Request $request
  35. * @param \Closure $next
  36. * @return mixed
  37. */
  38. public function handle($request, Closure $next)
  39. {
  40. if($request->path() == 'logout') {
  41. $this->auth->logout();
  42. return redirect('/');
  43. }
  44. if ($this->auth->guest()) {
  45. if ($request->ajax()) {
  46. return new JsonResponse(['msg'=>trans('fzs.common.no_permission'),'status'=>0], 200);
  47. } else {
  48. return redirect()->guest('/login');
  49. }
  50. }
  51. return $next($request);
  52. }
  53. }