From e5396f5f83291e1945f8fd4d4357cb947f522a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=BF=97?= Date: Wed, 21 Jan 2026 09:00:18 +0800 Subject: [PATCH] UP --- app/controller/Admin.php | 53 ++++++++++++++++++++++++++++++++++++++++ route/app.php | 7 +++--- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/controller/Admin.php b/app/controller/Admin.php index 6564a13..fe6c6ed 100644 --- a/app/controller/Admin.php +++ b/app/controller/Admin.php @@ -7,6 +7,7 @@ use app\BaseController; use app\middleware\Admin as AdminMiddleware; use app\service\ConfigService; use app\service\UserService; +use think\facade\Session; use think\facade\View; /** @@ -16,6 +17,43 @@ class Admin extends BaseController { protected $middleware = [AdminMiddleware::class]; + /** + * 初始化方法 + */ + protected function initialize() + { + parent::initialize(); + + // 对于API方法,直接检查权限并设置响应头 + $action = $this->request->action(); + $apiActions = ['getUsers', 'getBaseUrl', 'addUser', 'deleteUser', 'setBaseUrl']; + + if (in_array($action, $apiActions)) { + // 检查登录状态 + if (!Session::has('username')) { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode([ + 'code' => 0, + 'msg' => '请先登录', + ], JSON_UNESCAPED_UNICODE); + exit; + } + + // 检查管理员权限 + if (!Session::get('is_admin', false)) { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode([ + 'code' => 0, + 'msg' => '无权限访问,需要管理员权限', + ], JSON_UNESCAPED_UNICODE); + exit; + } + + // 设置JSON响应头 + header('Content-Type: application/json; charset=utf-8'); + } + } + /** * 显示管理首页 */ @@ -29,6 +67,9 @@ class Admin extends BaseController */ public function getUsers() { + // 强制返回JSON,设置响应头 + header('Content-Type: application/json; charset=utf-8'); + try { $service = new UserService(); $users = $service->getAllUsers(); @@ -60,6 +101,9 @@ class Admin extends BaseController */ public function addUser() { + // 强制返回JSON,设置响应头 + header('Content-Type: application/json; charset=utf-8'); + $username = $this->request->param('username', ''); $password = $this->request->param('password', ''); @@ -74,6 +118,9 @@ class Admin extends BaseController */ public function deleteUser() { + // 强制返回JSON,设置响应头 + header('Content-Type: application/json; charset=utf-8'); + $username = $this->request->param('username', ''); if (empty($username)) { @@ -94,6 +141,9 @@ class Admin extends BaseController */ public function getBaseUrl() { + // 强制返回JSON,设置响应头 + header('Content-Type: application/json; charset=utf-8'); + try { $service = new ConfigService(); $baseUrl = $service->getBaseUrl(); @@ -118,6 +168,9 @@ class Admin extends BaseController */ public function setBaseUrl() { + // 强制返回JSON,设置响应头 + header('Content-Type: application/json; charset=utf-8'); + $baseUrl = $this->request->param('base_url', ''); $service = new ConfigService(); diff --git a/route/app.php b/route/app.php index 9eac206..949b56f 100644 --- a/route/app.php +++ b/route/app.php @@ -30,9 +30,10 @@ Route::post('crawler/batchGetPositionInfo', 'crawler/batchGetPositionInfo'); Route::post('crawler/fetchAllPositions', 'crawler/fetchAllPositions'); // 管理员路由(需要登录且为管理员) -Route::get('admin', 'admin/index'); +// 注意:API路由必须在index路由之前定义,确保优先匹配 Route::get('admin/getUsers', 'admin/getUsers'); +Route::get('admin/getBaseUrl', 'admin/getBaseUrl'); Route::post('admin/addUser', 'admin/addUser'); Route::post('admin/deleteUser', 'admin/deleteUser'); -Route::get('admin/getBaseUrl', 'admin/getBaseUrl'); -Route::post('admin/setBaseUrl', 'admin/setBaseUrl'); \ No newline at end of file +Route::post('admin/setBaseUrl', 'admin/setBaseUrl'); +Route::get('admin', 'admin/index'); \ No newline at end of file