From 77930b32322c6c7ba0176e5c2467e362434e5254 Mon Sep 17 00:00:00 2001 From: GiveMePast <1248298477@qq.com> Date: Mon, 20 Jul 2026 14:59:16 +0800 Subject: [PATCH] wip --- app/BaseController.php | 10 +-- app/ExceptionHandle.php | 19 +++++- app/admin/controller/CompanyAndOperators.php | 2 - app/admin/middleware/Auth.php | 33 +++++++++- app/admin/route/app.php | 4 +- app/common/Trait/ApiResponse.php | 64 +++++++------------- app/index/controller/Login.php | 11 ++-- app/middleware/CheckLogin.php | 2 - config/database.php | 27 +++------ 9 files changed, 94 insertions(+), 78 deletions(-) diff --git a/app/BaseController.php b/app/BaseController.php index 025ed07..429f24f 100644 --- a/app/BaseController.php +++ b/app/BaseController.php @@ -1,17 +1,21 @@ failException(true)->check($data); } - } diff --git a/app/ExceptionHandle.php b/app/ExceptionHandle.php index 453d126..767b9b1 100644 --- a/app/ExceptionHandle.php +++ b/app/ExceptionHandle.php @@ -1,4 +1,5 @@ error($e->getError(), 422); // 验证器没通过 + }; + + // 请求异常 + if ($e instanceof HttpException) { + return $this->error($e->getMessage(), $e->getStatusCode()); + }; + + // 处理【自定义业务异常】(在 Service 层主动 throw 的错误) + if ($e instanceof \think\Exception) { + return $this->error($e->getMessage(), $e->getCode() ?: 400); + } // 其他错误交给系统处理 return parent::render($request, $e); diff --git a/app/admin/controller/CompanyAndOperators.php b/app/admin/controller/CompanyAndOperators.php index 1ba6770..14d5ef6 100644 --- a/app/admin/controller/CompanyAndOperators.php +++ b/app/admin/controller/CompanyAndOperators.php @@ -13,9 +13,7 @@ class CompanyAndOperators extends BaseController */ public function getAllOperators(TicketService $ticketService) { - $result = $ticketService->getAllOperators(); - return json(['code' => 200, 'msg' => '查询成功', 'data' => $result]); } diff --git a/app/admin/middleware/Auth.php b/app/admin/middleware/Auth.php index 0053905..47420e0 100644 --- a/app/admin/middleware/Auth.php +++ b/app/admin/middleware/Auth.php @@ -6,6 +6,28 @@ use think\facade\Session; class Auth { + /** + * 无需 admin 角色即可访问的路由名称(方法名小写) + */ + protected $publicActions = []; + + /** + * 需要 admin 角色才能访问的路由名称(方法名小写) + */ + protected $adminActions = [ + 'deleteoperator', + 'deletecompany', + 'addcompany', + 'addoperator', + 'getallusers', + 'addaccount', + 'updateaccountstatus', + 'deleteaccount', + 'getexportdata', + 'deleteticket', + 'batchupdate', + ]; + public function handle($request, \Closure $next) { if (!Session::has('user_info')) { @@ -13,9 +35,18 @@ class Auth } $user = Session::get('user_info'); + $role = $user['role'] ?? 'user'; $request->login_user_id = $user['id'] ?? null; - $request->login_role = $user['role'] ?? null; + $request->login_role = $role; + + // 获取当前请求的路由方法名 + $action = strtolower($request->action()); + + // admin 专用接口 + if (in_array($action, $this->adminActions) && $role !== 'admin') { + return json(['code' => 403, 'msg' => '权限不足,仅管理员可执行此操作']); + } return $next($request); } diff --git a/app/admin/route/app.php b/app/admin/route/app.php index 13d85ad..a1c5b8e 100644 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -2,6 +2,7 @@ use think\facade\Route; use app\admin\middleware\Auth; +use app\middleware\CheckLogin; Route::group(function () { Route::get('ticketsfind', 'Ticket/ticketsFind'); @@ -11,7 +12,6 @@ Route::group(function () { Route::put('tickets/batch-update', 'Ticket/batchUpdate'); Route::get('getticketdashboardstats', 'Ticket/getTicketDashboardStats'); - Route::get('getUserStaffList', 'CompanyAndOperators/getStaffList'); Route::get('operators', 'CompanyAndOperators/getAllOperators'); Route::get('getOperatorList', 'CompanyAndOperators/TestgetAllOperators'); @@ -26,4 +26,4 @@ Route::group(function () { Route::delete('deleteAccount/:id', 'AccountManagement/deleteAccount'); Route::post('getExportData', 'Utill/getExportData'); -})->middleware(Auth::class); +})->middleware([CheckLogin::class, Auth::class]); diff --git a/app/common/Trait/ApiResponse.php b/app/common/Trait/ApiResponse.php index 77e772e..a362c10 100644 --- a/app/common/Trait/ApiResponse.php +++ b/app/common/Trait/ApiResponse.php @@ -1,66 +1,46 @@ $code, - 'msg' => $msg, - 'data' => $data - ], $httpCode); + return $this->jsonResponse($code, $message, $data); } /** - * 错误响应 - * - * @param string $msg 错误消息 - * @param mixed $data 错误数据 - * @param int $code 错误码 - * @param int $httpCode HTTP状态码 - * @return Response + * 失败响应 + * @param string $message 错误提示信息 + * @param int $code 自定义业务状态码 + * @param mixed $data 额外的错误数据(如验证未通过的具体字段) + * @return Json */ - protected function errorResponse($msg = '失败', $data = null, $code = 400, $httpCode = 400) + protected function error(int $code = 400, string $message = '操作失败', mixed $data = []): Json { - return json([ - 'code' => $code, - 'msg' => $msg, - 'data' => $data - ], $httpCode); + return $this->jsonResponse($code, $message, $data); } /** - * 自定义响应 - * - * @param int $code 响应码 - * @param string $msg 响应消息 - * @param mixed $data 响应数据 - * @param int $httpCode HTTP状态码 - * @return Response + * 统一 JSON 返回格式 */ - protected function customResponse($code, $msg, $data = [], $httpCode = 200) + private function jsonResponse(int $code, string $message, mixed $data): Json { - return json([ + $result = [ 'code' => $code, - 'msg' => $msg, - 'data' => $data - ], $httpCode); + 'msg' => $message, + 'data' => $data, + ]; + + return json($result); } } diff --git a/app/index/controller/Login.php b/app/index/controller/Login.php index 29935bd..8c61fb5 100644 --- a/app/index/controller/Login.php +++ b/app/index/controller/Login.php @@ -9,8 +9,7 @@ use think\facade\Session; // 已完成 class Login extends BaseController -{ - +{ /* 处理登录请求(已完成) */ @@ -20,14 +19,14 @@ class Login extends BaseController // 验证数据 if (empty($params['username']) || empty($params['password'])) { - return json(['code' => 400, 'message' => '用户名和密码不能为空']); + return $this->error(400, '用户名和密码不能为空'); } try { $result = $authService->login($params['username'], $params['password']); - return json(['code' => 200, 'message' => '登录成功', 'data' => $result]); + return $this->success(200, '登录成功', $result); } catch (\Exception $e) { - return json(['code' => 400, 'message' => $e->getMessage(), 'data' => []]); + return $this->error(400, $e->getMessage()); } } @@ -37,6 +36,6 @@ class Login extends BaseController public function logout() { Session::clear(); - return json(['code' => 200, 'message' => '退出成功']); + return $this->success(200, '退出成功'); } } diff --git a/app/middleware/CheckLogin.php b/app/middleware/CheckLogin.php index 68bba46..937b5de 100644 --- a/app/middleware/CheckLogin.php +++ b/app/middleware/CheckLogin.php @@ -7,7 +7,6 @@ class CheckLogin { /** * 处理请求 - * * @param \think\Request $request * @param \Closure $next * @return Response @@ -17,7 +16,6 @@ class CheckLogin if (!Session::has('user_info')) { return json(['code' => 401, 'message' => '未登录']); } - return $next($request); } } diff --git a/config/database.php b/config/database.php index 679d618..544e307 100644 --- a/config/database.php +++ b/config/database.php @@ -58,24 +58,15 @@ return [ 'fields_cache' => false, ], 'remote' => [ - // 数据库类型 - 'type' => env('REMOTE_DB_TYPE', 'mysql'), - // 服务器地址 - 'hostname' => env('REMOTE_DB_HOST', '127.0.0.1'), - // 数据库名 - 'database' => env('REMOTE_DB_NAME', ''), - // 用户名 - 'username' => env('REMOTE_DB_USER', 'root'), - // 密码 - 'password' => env('REMOTE_DB_PASS', ''), - // 端口 - 'hostport' => env('REMOTE_DB_PORT', '3306'), - // 数据库编码 - 'charset' => env('REMOTE_DB_CHARSET', 'utf8mb4'), - // 数据库表前缀 - 'prefix' => '', - // 远程连接建议开启断线重连 - 'break_reconnect' => true, + 'type' => 'mysql', + 'hostname' => '112.5.15.136', + 'database' => '112_5_15_136_255', + 'username' => '112_5_15_136_255', + 'password' => 'xz1YX1jswSWzkE4K', + 'hostport' => '3306', + 'charset' => 'utf8mb4', + 'prefix' => '', + 'break_reconnect' => true, ], // 更多的数据库配置信息