diff --git a/app/controller/Admin.php b/app/controller/Admin.php index 15bd130..6564a13 100644 --- a/app/controller/Admin.php +++ b/app/controller/Admin.php @@ -29,23 +29,30 @@ class Admin extends BaseController */ public function getUsers() { - $service = new UserService(); - $users = $service->getAllUsers(); - - // 格式化数据 - $result = []; - foreach ($users as $user) { - $result[] = [ - 'username' => $user['username'] ?? '', - 'created_at' => $user['created_at'] ?? '', - ]; + try { + $service = new UserService(); + $users = $service->getAllUsers(); + + // 格式化数据 + $result = []; + foreach ($users as $user) { + $result[] = [ + 'username' => $user['username'] ?? '', + 'created_at' => $user['created_at'] ?? '', + ]; + } + + return json([ + 'code' => 1, + 'data' => $result, + 'msg' => '获取成功', + ]); + } catch (\Exception $e) { + return json([ + 'code' => 0, + 'msg' => '获取失败: ' . $e->getMessage(), + ]); } - - return json([ - 'code' => 1, - 'data' => $result, - 'msg' => '获取成功', - ]); } /** @@ -87,16 +94,23 @@ class Admin extends BaseController */ public function getBaseUrl() { - $service = new ConfigService(); - $baseUrl = $service->getBaseUrl(); - - return json([ - 'code' => 1, - 'data' => [ - 'base_url' => $baseUrl, - ], - 'msg' => '获取成功', - ]); + try { + $service = new ConfigService(); + $baseUrl = $service->getBaseUrl(); + + return json([ + 'code' => 1, + 'data' => [ + 'base_url' => $baseUrl, + ], + 'msg' => '获取成功', + ]); + } catch (\Exception $e) { + return json([ + 'code' => 0, + 'msg' => '获取失败: ' . $e->getMessage(), + ]); + } } /** diff --git a/app/middleware/Admin.php b/app/middleware/Admin.php index 55dbc2d..ab0a3d5 100644 --- a/app/middleware/Admin.php +++ b/app/middleware/Admin.php @@ -19,12 +19,18 @@ class Admin */ public function handle($request, \Closure $next) { + // 获取路径信息 + $pathinfo = $request->pathinfo(); + + // 判断是否为API请求(非index页面) + $isApiRequest = $pathinfo !== 'admin' && strpos($pathinfo, 'admin/') === 0; + // 检查是否已登录 $username = Session::get('username'); if (empty($username)) { - // 如果是AJAX请求,返回JSON - if ($request->isAjax()) { + // 如果是API请求,返回JSON + if ($isApiRequest || $request->isAjax()) { return json([ 'code' => 0, 'msg' => '请先登录', @@ -38,8 +44,8 @@ class Admin $isAdmin = Session::get('is_admin', false); if (!$isAdmin) { - // 如果是AJAX请求,返回JSON - if ($request->isAjax()) { + // 如果是API请求,返回JSON + if ($isApiRequest || $request->isAjax()) { return json([ 'code' => 0, 'msg' => '无权限访问,需要管理员权限', diff --git a/view/admin/index.html b/view/admin/index.html index ec526eb..47f56a5 100644 --- a/view/admin/index.html +++ b/view/admin/index.html @@ -232,6 +232,7 @@ method: 'GET', headers: { 'Content-Type': 'application/json', + 'X-Requested-With': 'XMLHttpRequest', } }) .then(response => response.json()) @@ -260,6 +261,7 @@ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', + 'X-Requested-With': 'XMLHttpRequest', }, body: `base_url=${encodeURIComponent(baseUrl)}` }) @@ -282,6 +284,7 @@ method: 'GET', headers: { 'Content-Type': 'application/json', + 'X-Requested-With': 'XMLHttpRequest', } }) .then(response => response.json()) @@ -345,6 +348,7 @@ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', + 'X-Requested-With': 'XMLHttpRequest', }, body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}` }) @@ -374,6 +378,7 @@ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', + 'X-Requested-With': 'XMLHttpRequest', }, body: `username=${encodeURIComponent(username)}` })