From 58afb7c3851d5e0e9d22bad99a8366dc67d6fcef Mon Sep 17 00:00:00 2001 From: Nick Zeng Date: Wed, 18 Mar 2026 08:37:14 +0800 Subject: [PATCH] fix request log --- .../app/Controller/Api/V1/OperationLogController.php | 8 ++++++++ backend/app/Controller/api/v1/AuthController.php | 3 +-- backend/app/Controller/api/v1/DataScopeController.php | 2 +- backend/app/Controller/api/v1/RoleController.php | 2 +- backend/app/Controller/api/v1/UserController.php | 6 +++--- backend/app/Service/OperationLogService.php | 10 ++++++++-- .../Integration/System/OperationLogControllerTest.php | 4 ++-- 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/backend/app/Controller/Api/V1/OperationLogController.php b/backend/app/Controller/Api/V1/OperationLogController.php index a9118e0..60df3b6 100644 --- a/backend/app/Controller/Api/V1/OperationLogController.php +++ b/backend/app/Controller/Api/V1/OperationLogController.php @@ -59,6 +59,14 @@ class OperationLogController extends AbstractDataController return 'created_at'; } + /** + * 操作日志表无 store_id/platform_id,跳过 DataScope 过滤 + */ + protected function applyDataScope(\Hyperf\Database\Model\Builder $query): void + { + // 操作日志仅限 admin 访问,无需按店铺/平台过滤 + } + /** * 操作日志列表 */ diff --git a/backend/app/Controller/api/v1/AuthController.php b/backend/app/Controller/api/v1/AuthController.php index 867c8a7..3bc6cee 100644 --- a/backend/app/Controller/api/v1/AuthController.php +++ b/backend/app/Controller/api/v1/AuthController.php @@ -6,7 +6,6 @@ namespace App\Controller\Api\V1; use App\Controller\AbstractController; use App\Middleware\AuthMiddleware; -use App\Middleware\RequestLogMiddleware; use App\Model\User; use App\Service\OperationLogService; use Carbon\Carbon; @@ -260,7 +259,7 @@ class AuthController extends AbstractController target_type: 'user', target_id: $user->id, description: "用户 {$user->username} 登录", - ip: RequestLogMiddleware::getClientIp($request), + ip: OperationLogService::getRequestIp(), ); return [ diff --git a/backend/app/Controller/api/v1/DataScopeController.php b/backend/app/Controller/api/v1/DataScopeController.php index c421fae..bac7d51 100644 --- a/backend/app/Controller/api/v1/DataScopeController.php +++ b/backend/app/Controller/api/v1/DataScopeController.php @@ -240,7 +240,7 @@ class DataScopeController extends AbstractController $this->scopeTableManager->rebuildUserScope($id); OperationLogService::log( - user_id: OperationLogService::getCurrentUserId() ?? 0, + user_id: OperationLogService::getCurrentUserId(), action: 'scope.update', target_type: 'user', target_id: $id, diff --git a/backend/app/Controller/api/v1/RoleController.php b/backend/app/Controller/api/v1/RoleController.php index f4e7cba..da1f101 100644 --- a/backend/app/Controller/api/v1/RoleController.php +++ b/backend/app/Controller/api/v1/RoleController.php @@ -174,7 +174,7 @@ class RoleController extends AbstractController $target_user->load('role'); OperationLogService::log( - user_id: OperationLogService::getCurrentUserId() ?? 0, + user_id: OperationLogService::getCurrentUserId(), action: 'role.update', target_type: 'user', target_id: $id, diff --git a/backend/app/Controller/api/v1/UserController.php b/backend/app/Controller/api/v1/UserController.php index ffb500b..f75f088 100644 --- a/backend/app/Controller/api/v1/UserController.php +++ b/backend/app/Controller/api/v1/UserController.php @@ -232,7 +232,7 @@ class UserController extends AbstractController ]); OperationLogService::log( - user_id: OperationLogService::getCurrentUserId() ?? 0, + user_id: OperationLogService::getCurrentUserId(), action: 'user.create', target_type: 'user', target_id: $user->id, @@ -442,7 +442,7 @@ class UserController extends AbstractController $user->refresh(); OperationLogService::log( - user_id: OperationLogService::getCurrentUserId() ?? 0, + user_id: OperationLogService::getCurrentUserId(), action: 'user.update', target_type: 'user', target_id: $user->id, @@ -531,7 +531,7 @@ class UserController extends AbstractController $user->refresh(); OperationLogService::log( - user_id: OperationLogService::getCurrentUserId() ?? 0, + user_id: OperationLogService::getCurrentUserId(), action: 'user.status_change', target_type: 'user', target_id: $user->id, diff --git a/backend/app/Service/OperationLogService.php b/backend/app/Service/OperationLogService.php index d335b95..b458d5a 100644 --- a/backend/app/Service/OperationLogService.php +++ b/backend/app/Service/OperationLogService.php @@ -57,7 +57,10 @@ class OperationLogService $container = \Hyperf\Context\ApplicationContext::getContainer(); $request = $container->get(ServerRequestInterface::class); return RequestLogMiddleware::getClientIp($request); - } catch (\Throwable) { + } catch (\Throwable $e) { + Log::get()->warning('OperationLogService: 获取客户端 IP 失败', [ + 'error' => $e->getMessage(), + ]); return null; } } @@ -75,7 +78,10 @@ class OperationLogService return $user->id; } return $user?->getId(); - } catch (\Throwable) { + } catch (\Throwable $e) { + Log::get()->warning('OperationLogService: 获取当前用户 ID 失败', [ + 'error' => $e->getMessage(), + ]); return null; } } diff --git a/backend/test/Cases/Integration/System/OperationLogControllerTest.php b/backend/test/Cases/Integration/System/OperationLogControllerTest.php index 2a88d29..4bc631e 100644 --- a/backend/test/Cases/Integration/System/OperationLogControllerTest.php +++ b/backend/test/Cases/Integration/System/OperationLogControllerTest.php @@ -187,7 +187,7 @@ class OperationLogControllerTest extends TestCase * co-phpunit 下 Coroutine::defer 的回调要到顶层协程退出才执行。 * 将 HTTP 请求放入子协程并 join,确保 controller 中的 defer 回调完成后再断言。 */ - public function test_user_create_generates_operation_log(): void + public function test_operation_log_service_writes_record_for_user_create(): void { $suffix = bin2hex(random_bytes(4)); $unique_name = 'ol_' . $suffix; @@ -241,7 +241,7 @@ class OperationLogControllerTest extends TestCase ->delete(); } - public function test_user_status_change_generates_operation_log(): void + public function test_operation_log_service_writes_record_for_status_change(): void { $suffix = bin2hex(random_bytes(4)); $unique_name = 'os_' . $suffix;