up
This commit is contained in:
@@ -20,16 +20,24 @@ class Auth
|
||||
// 检查是否已登录
|
||||
$username = session('username');
|
||||
|
||||
// 获取路径信息
|
||||
// 获取路径信息和方法名
|
||||
$pathinfo = $request->pathinfo();
|
||||
$controller = $request->controller(true);
|
||||
$action = $request->action(true);
|
||||
|
||||
// 判断是否为API请求(非index页面)
|
||||
$isApiRequest = $pathinfo !== 'crawler' && strpos($pathinfo, 'crawler/') === 0;
|
||||
// 判断是否为API请求
|
||||
// 1. 路径包含 /crawler/ 且不是 /crawler 本身
|
||||
// 2. 或者是 crawler 控制器的非 index 方法
|
||||
// 3. 或者是AJAX请求
|
||||
$isApiRequest = ($pathinfo !== 'crawler' && strpos($pathinfo, 'crawler/') === 0) ||
|
||||
($controller === 'crawler' && $action !== 'index') ||
|
||||
$request->isAjax() ||
|
||||
$request->header('X-Requested-With') === 'XMLHttpRequest';
|
||||
|
||||
if (empty($username)) {
|
||||
// 如果是API请求,返回JSON
|
||||
if ($isApiRequest || $request->isAjax()) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
if ($isApiRequest) {
|
||||
// 直接返回JSON,ThinkPHP的json()函数会自动设置响应头
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '请先登录',
|
||||
|
||||
Reference in New Issue
Block a user