From eed3540f3d885d1d0c9063084f3662fa45fe46eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=BF=97?= Date: Wed, 21 Jan 2026 09:18:19 +0800 Subject: [PATCH] up --- app/controller/Crawler.php | 26 ++++++++++++++++++++------ app/middleware/Auth.php | 18 +++++++++++++----- route/app.php | 3 ++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/controller/Crawler.php b/app/controller/Crawler.php index cf80288..4299a66 100644 --- a/app/controller/Crawler.php +++ b/app/controller/Crawler.php @@ -16,6 +16,26 @@ use think\facade\View; class Crawler extends BaseController { protected $middleware = [Auth::class]; + + /** + * 初始化方法 + */ + protected function initialize() + { + parent::initialize(); + + // 对于API方法,直接设置响应头 + $action = $this->request->action(true); + $apiActions = ['getUserConfig', 'saveUserConfig', 'getDsdmOptions', 'getZwdmList', 'getPositionInfo', 'batchGetPositionInfo', 'fetchAllPositions']; + + if (in_array($action, $apiActions)) { + // 设置JSON响应头并禁用视图渲染 + header('Content-Type: application/json; charset=utf-8'); + // 禁用视图自动输出 + $this->app->view->config('auto_render', false); + } + } + /** * 显示爬虫工具首页 */ @@ -41,9 +61,6 @@ class Crawler extends BaseController */ public function getUserConfig() { - // 强制返回JSON,设置响应头 - header('Content-Type: application/json; charset=utf-8'); - try { $username = Session::get('username', ''); @@ -75,9 +92,6 @@ class Crawler extends BaseController */ public function saveUserConfig() { - // 强制返回JSON,设置响应头 - header('Content-Type: application/json; charset=utf-8'); - try { $username = Session::get('username', ''); diff --git a/app/middleware/Auth.php b/app/middleware/Auth.php index d4c83f6..47c4826 100644 --- a/app/middleware/Auth.php +++ b/app/middleware/Auth.php @@ -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' => '请先登录', diff --git a/route/app.php b/route/app.php index 694812e..d02f8cb 100644 --- a/route/app.php +++ b/route/app.php @@ -22,7 +22,7 @@ Route::post('auth/doLogin', 'auth/doLogin'); Route::get('auth/logout', 'auth/logout'); // 爬虫工具路由(需要登录) -Route::get('crawler', 'crawler/index'); +// 注意:API路由必须在index路由之前定义,确保优先匹配 Route::get('crawler/getUserConfig', 'crawler/getUserConfig'); Route::post('crawler/saveUserConfig', 'crawler/saveUserConfig'); Route::post('crawler/getDsdmOptions', 'crawler/getDsdmOptions'); @@ -30,6 +30,7 @@ Route::post('crawler/getZwdmList', 'crawler/getZwdmList'); Route::post('crawler/getPositionInfo', 'crawler/getPositionInfo'); Route::post('crawler/batchGetPositionInfo', 'crawler/batchGetPositionInfo'); Route::post('crawler/fetchAllPositions', 'crawler/fetchAllPositions'); +Route::get('crawler', 'crawler/index'); // 管理员路由(需要登录且为管理员) // 注意:API路由必须在index路由之前定义,确保优先匹配