This commit is contained in:
杨志
2026-01-21 09:15:50 +08:00
parent a7a6d466d0
commit a562c95b10
2 changed files with 56 additions and 33 deletions

View File

@@ -41,25 +41,33 @@ class Crawler extends BaseController
*/
public function getUserConfig()
{
// 强制返回JSON设置响应头
header('Content-Type: application/json; charset=utf-8');
$username = Session::get('username', '');
try {
$username = Session::get('username', '');
if (empty($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', '');
try {
$username = Session::get('username', '');
if (empty($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);
}
/**

View File

@@ -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' => '请先登录',