fix request log

This commit is contained in:
2026-03-18 08:37:14 +08:00
parent cdc88ba1e4
commit 58afb7c385
7 changed files with 24 additions and 11 deletions
@@ -59,6 +59,14 @@ class OperationLogController extends AbstractDataController
return 'created_at'; return 'created_at';
} }
/**
* 操作日志表无 store_id/platform_id,跳过 DataScope 过滤
*/
protected function applyDataScope(\Hyperf\Database\Model\Builder $query): void
{
// 操作日志仅限 admin 访问,无需按店铺/平台过滤
}
/** /**
* 操作日志列表 * 操作日志列表
*/ */
@@ -6,7 +6,6 @@ namespace App\Controller\Api\V1;
use App\Controller\AbstractController; use App\Controller\AbstractController;
use App\Middleware\AuthMiddleware; use App\Middleware\AuthMiddleware;
use App\Middleware\RequestLogMiddleware;
use App\Model\User; use App\Model\User;
use App\Service\OperationLogService; use App\Service\OperationLogService;
use Carbon\Carbon; use Carbon\Carbon;
@@ -260,7 +259,7 @@ class AuthController extends AbstractController
target_type: 'user', target_type: 'user',
target_id: $user->id, target_id: $user->id,
description: "用户 {$user->username} 登录", description: "用户 {$user->username} 登录",
ip: RequestLogMiddleware::getClientIp($request), ip: OperationLogService::getRequestIp(),
); );
return [ return [
@@ -240,7 +240,7 @@ class DataScopeController extends AbstractController
$this->scopeTableManager->rebuildUserScope($id); $this->scopeTableManager->rebuildUserScope($id);
OperationLogService::log( OperationLogService::log(
user_id: OperationLogService::getCurrentUserId() ?? 0, user_id: OperationLogService::getCurrentUserId(),
action: 'scope.update', action: 'scope.update',
target_type: 'user', target_type: 'user',
target_id: $id, target_id: $id,
@@ -174,7 +174,7 @@ class RoleController extends AbstractController
$target_user->load('role'); $target_user->load('role');
OperationLogService::log( OperationLogService::log(
user_id: OperationLogService::getCurrentUserId() ?? 0, user_id: OperationLogService::getCurrentUserId(),
action: 'role.update', action: 'role.update',
target_type: 'user', target_type: 'user',
target_id: $id, target_id: $id,
@@ -232,7 +232,7 @@ class UserController extends AbstractController
]); ]);
OperationLogService::log( OperationLogService::log(
user_id: OperationLogService::getCurrentUserId() ?? 0, user_id: OperationLogService::getCurrentUserId(),
action: 'user.create', action: 'user.create',
target_type: 'user', target_type: 'user',
target_id: $user->id, target_id: $user->id,
@@ -442,7 +442,7 @@ class UserController extends AbstractController
$user->refresh(); $user->refresh();
OperationLogService::log( OperationLogService::log(
user_id: OperationLogService::getCurrentUserId() ?? 0, user_id: OperationLogService::getCurrentUserId(),
action: 'user.update', action: 'user.update',
target_type: 'user', target_type: 'user',
target_id: $user->id, target_id: $user->id,
@@ -531,7 +531,7 @@ class UserController extends AbstractController
$user->refresh(); $user->refresh();
OperationLogService::log( OperationLogService::log(
user_id: OperationLogService::getCurrentUserId() ?? 0, user_id: OperationLogService::getCurrentUserId(),
action: 'user.status_change', action: 'user.status_change',
target_type: 'user', target_type: 'user',
target_id: $user->id, target_id: $user->id,
+8 -2
View File
@@ -57,7 +57,10 @@ class OperationLogService
$container = \Hyperf\Context\ApplicationContext::getContainer(); $container = \Hyperf\Context\ApplicationContext::getContainer();
$request = $container->get(ServerRequestInterface::class); $request = $container->get(ServerRequestInterface::class);
return RequestLogMiddleware::getClientIp($request); return RequestLogMiddleware::getClientIp($request);
} catch (\Throwable) { } catch (\Throwable $e) {
Log::get()->warning('OperationLogService: 获取客户端 IP 失败', [
'error' => $e->getMessage(),
]);
return null; return null;
} }
} }
@@ -75,7 +78,10 @@ class OperationLogService
return $user->id; return $user->id;
} }
return $user?->getId(); return $user?->getId();
} catch (\Throwable) { } catch (\Throwable $e) {
Log::get()->warning('OperationLogService: 获取当前用户 ID 失败', [
'error' => $e->getMessage(),
]);
return null; return null;
} }
} }
@@ -187,7 +187,7 @@ class OperationLogControllerTest extends TestCase
* co-phpunit 下 Coroutine::defer 的回调要到顶层协程退出才执行。 * co-phpunit 下 Coroutine::defer 的回调要到顶层协程退出才执行。
* 将 HTTP 请求放入子协程并 join,确保 controller 中的 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)); $suffix = bin2hex(random_bytes(4));
$unique_name = 'ol_' . $suffix; $unique_name = 'ol_' . $suffix;
@@ -241,7 +241,7 @@ class OperationLogControllerTest extends TestCase
->delete(); ->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)); $suffix = bin2hex(random_bytes(4));
$unique_name = 'os_' . $suffix; $unique_name = 'os_' . $suffix;