Auth.php 757 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\middleware;
  4. /**
  5. * Description of Auth
  6. *
  7. * @author sgq
  8. */
  9. class Auth {
  10. /**
  11. * 处理请求
  12. *
  13. * @param \think\Request $request
  14. * @param \Closure $next
  15. * @return Response
  16. */
  17. public function handle($request, \Closure $next) {
  18. $controller = $request->controller();
  19. if (strtolower($controller) != "auth" && empty(session('user'))) {
  20. if ($request->isJson())
  21. return json(["msg" => "登录已失效"]);
  22. $redirect_url = getHostWithProtocol() . $_SERVER["REQUEST_URI"];
  23. return redirect('/index/auth/login?redirect=' . urlencode($redirect_url));
  24. }
  25. return $next($request);
  26. }
  27. }