diff --git a/app/controller/Crawler.php b/app/controller/Crawler.php index 4ba5f03..cf80288 100644 --- a/app/controller/Crawler.php +++ b/app/controller/Crawler.php @@ -41,25 +41,33 @@ class Crawler extends BaseController */ public function getUserConfig() { + // 强制返回JSON,设置响应头 header('Content-Type: application/json; charset=utf-8'); - $username = Session::get('username', ''); - - if (empty($username)) { + try { + $username = Session::get('username', ''); + + if (empty($username)) { + return json([ + 'code' => 0, + 'msg' => '未登录', + ]); + } + + $configService = new UserConfigService(); + $config = $configService->getUserConfig($username); + + return json([ + 'code' => 1, + 'data' => $config, + 'msg' => '获取成功', + ]); + } catch (\Exception $e) { return json([ 'code' => 0, - 'msg' => '未登录', + 'msg' => '获取失败: ' . $e->getMessage(), ]); } - - $configService = new UserConfigService(); - $config = $configService->getUserConfig($username); - - return json([ - 'code' => 1, - 'data' => $config, - 'msg' => '获取成功', - ]); } /** @@ -67,30 +75,38 @@ class Crawler extends BaseController */ public function saveUserConfig() { + // 强制返回JSON,设置响应头 header('Content-Type: application/json; charset=utf-8'); - $username = Session::get('username', ''); - - if (empty($username)) { + try { + $username = Session::get('username', ''); + + if (empty($username)) { + return json([ + 'code' => 0, + 'msg' => '未登录', + ]); + } + + $config = [ + 'jsessionid1' => $this->request->param('jsessionid1', ''), + 'jsessionid2' => $this->request->param('jsessionid2', ''), + 'serverid' => $this->request->param('serverid', ''), + 'examid' => $this->request->param('examid', ''), + 'bmid' => $this->request->param('bmid', ''), + 'userid' => $this->request->param('userid', ''), + ]; + + $configService = new UserConfigService(); + $result = $configService->saveUserConfig($username, $config); + + return json($result); + } catch (\Exception $e) { return json([ 'code' => 0, - 'msg' => '未登录', + 'msg' => '保存失败: ' . $e->getMessage(), ]); } - - $config = [ - 'jsessionid1' => $this->request->param('jsessionid1', ''), - 'jsessionid2' => $this->request->param('jsessionid2', ''), - 'serverid' => $this->request->param('serverid', ''), - 'examid' => $this->request->param('examid', ''), - 'bmid' => $this->request->param('bmid', ''), - 'userid' => $this->request->param('userid', ''), - ]; - - $configService = new UserConfigService(); - $result = $configService->saveUserConfig($username, $config); - - return json($result); } /** diff --git a/app/middleware/Auth.php b/app/middleware/Auth.php index d720be5..d4c83f6 100644 --- a/app/middleware/Auth.php +++ b/app/middleware/Auth.php @@ -20,9 +20,16 @@ class Auth // 检查是否已登录 $username = session('username'); + // 获取路径信息 + $pathinfo = $request->pathinfo(); + + // 判断是否为API请求(非index页面) + $isApiRequest = $pathinfo !== 'crawler' && strpos($pathinfo, 'crawler/') === 0; + if (empty($username)) { - // 如果是AJAX请求,返回JSON - if ($request->isAjax()) { + // 如果是API请求,返回JSON + if ($isApiRequest || $request->isAjax()) { + header('Content-Type: application/json; charset=utf-8'); return json([ 'code' => 0, 'msg' => '请先登录',