route()->getAction(); $controller = class_basename($action['controller']); list($controller, $action) = explode('@', $controller); $noLoginActs = ['LoginController']; $token = $request->headers->get('Authorization'); if (strpos($token, 'Bearer ') !== false) { $token = str_replace("Bearer ", null, $token); // JWT解密token $jwt = new Jwt(); $userId = $jwt->verifyToken($token); } else { $userId = 0; } if (!$userId && !in_array($controller, $noLoginActs)) { // 判断用户未登录就跳转至登录页面 // 在这里可以定制你想要的返回格式, 亦或者是 JSON 编码格式 return response()->json(message("请登录", false, null, 401)); } //如果已登录则执行正常的请求 return $response; } }