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';
}
/**
* 操作日志表无 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\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 [
@@ -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,
@@ -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,
@@ -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,
+8 -2
View File
@@ -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;
}
}