<?php

declare (strict_types=1);

namespace app\common\middleware;

/**
 * Description of Auth
 *
 * @author sgq
 */
class Auth {

    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next) {
        $controller = $request->controller();
        if (strtolower($controller) != "auth" && empty(session('user'))) {
            if ($request->isJson())
                return json(["msg" => "登录已失效"]);
            $redirect_url = getHostWithProtocol() . $_SERVER["REQUEST_URI"];
            return redirect('/index/auth/login?redirect=' . urlencode($redirect_url));
        }
        return $next($request);
    }

}
