From 21331655f9e625ce8cfb9d6012c6ccc21418f870 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Tue, 16 Sep 2025 18:07:10 +0800 Subject: [PATCH] fix : common request --- app/Aspect/AdminLoginLogAspect.php | 29 +++++++-------- app/Aspect/AdminReturnLogAspect.php | 35 ++++++++++++------- app/Common/Trait/ClientIpTrait.php | 2 +- app/Common/Trait/ClientOsTrait.php | 2 +- app/Middleware/Admin/PermissionMiddleware.php | 2 +- app/Model/AdminUserLoginLog.php | 2 +- app/Model/AdminUserOperationLog.php | 2 +- .../Admin/AdminUser/PermissionService.php | 3 ++ app/Service/Admin/AdminUser/UserService.php | 2 +- 9 files changed, 46 insertions(+), 33 deletions(-) diff --git a/app/Aspect/AdminLoginLogAspect.php b/app/Aspect/AdminLoginLogAspect.php index 3fbc5c9..cb75518 100644 --- a/app/Aspect/AdminLoginLogAspect.php +++ b/app/Aspect/AdminLoginLogAspect.php @@ -50,7 +50,7 @@ class AdminLoginLogAspect extends AbstractAspect */ public function __construct( protected readonly Logger $logger, - protected readonly RequestInterface $request, + protected RequestInterface $request, protected readonly AdminUserRepository $adminUserRepository, protected readonly AdminUserLoginLog $adminUserLoginLogModel, ) {} @@ -61,7 +61,7 @@ class AdminLoginLogAspect extends AbstractAspect * @throws Exception * @throws Throwable */ - public function process(ProceedingJoinPoint $proceedingJoinPoint) + public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed { // 写日志 @@ -87,23 +87,24 @@ class AdminLoginLogAspect extends AbstractAspect */ private function writeLoginLog(): void { - Coroutine::create(function () { - $userInfo = $this->adminUserRepository->findByUserName($this->request->input('username')); - $context = [ - 'username' => $this->request->input('username',''), - 'password' => $this->request->input('password',''), - 'user_info' => $userInfo?->toArray() ?? [], - 'ip' => $this->getClientIp(), - 'os' => $this->getClientOs(), - 'browser' => $this->request->header('User-Agent') ?: 'unknown', - ]; + $userInfo = $this->adminUserRepository->findByUserName($this->request->input('username')); + $context = [ + 'username' => $this->request->input('username',''), + 'password' => $this->request->input('password',''), + 'user_info' => $userInfo?->toArray() ?? [], + 'ip' => $this->getClientIp(), + 'os' => $this->getClientOs(), + 'browser' => $this->request->header('User-Agent') ?: 'unknown', + ]; + + Coroutine::create(function () use ($userInfo, $context) { $this->logger->request()->info('admin_login_log', $context); - $this->adminUserLoginLogModel->save([ + $this->adminUserLoginLogModel->create([ 'admin_user_id' => $userInfo?->id ?? 0, 'username' => $userInfo?->username ?? '', - 'ip' => $context['ip'], + 'ip' => current($context['ip']) ?: '0.0.0.0', 'os' => $context['os'], 'browser' => $context['browser'] ?? '', 'status' => $this->loginSuccess ? 1 : 2, diff --git a/app/Aspect/AdminReturnLogAspect.php b/app/Aspect/AdminReturnLogAspect.php index 5a1dde4..0230a3b 100644 --- a/app/Aspect/AdminReturnLogAspect.php +++ b/app/Aspect/AdminReturnLogAspect.php @@ -15,6 +15,7 @@ use Hyperf\Coroutine\Coroutine; use Hyperf\Di\Annotation\Aspect; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; +use Hyperf\Di\Exception\Exception; use Hyperf\HttpServer\Contract\RequestInterface; #[Aspect] @@ -48,13 +49,18 @@ class AdminReturnLogAspect extends AbstractAspect */ public function __construct( protected readonly Logger $logger, - protected readonly RequestInterface $request, + protected RequestInterface $request, protected readonly AdminUserOperationLog $adminUserOperationLogModel, ) { $this->adminId = Context::get('current_admin_id',0); if ($this->adminId > 0) $this->adminUserInfo = $this->getAdminUserInfo($this->adminId); } + /** + * @param ProceedingJoinPoint $proceedingJoinPoint + * @return mixed + * @throws Exception + */ public function process(ProceedingJoinPoint $proceedingJoinPoint) { // 直接从方法参数获取请求数据 @@ -63,11 +69,14 @@ class AdminReturnLogAspect extends AbstractAspect // 执行原方法并获取返回值 $responseData = $proceedingJoinPoint->process(); + // 没登录不记录日志 + if ($this->adminId <= 0) return $responseData; + // 写日志 $this->writeOperationLog($requestData, $responseData); // 返回 - return $requestData; + return $responseData; } /** @@ -77,25 +86,25 @@ class AdminReturnLogAspect extends AbstractAspect */ private function writeOperationLog(array $requestData = [], array $responseData = []): void { - Coroutine::create(function () use ($requestData, $responseData) { - $context = [ - 'user_id' => $this->adminId, - 'method' => $this->request->getMethod(), - 'router' => $this->request->getUri(), - 'ip' => $this->getClientIp(), - 'request_data' => $requestData, - 'response_data' => $responseData, - ]; + $context = [ + 'user_id' => $this->adminId, + 'method' => $this->request->getMethod(), + 'router' => $this->request->getUri(), + 'ip' => $this->getClientIp(), + 'request_data' => $requestData, + 'response_data' => $responseData, + ]; + Coroutine::create(function () use ($requestData, $responseData, $context) { $this->logger->request()->info('admin_request_log', $context); - $this->adminUserOperationLogModel->save([ + $this->adminUserOperationLogModel->create([ 'admin_user_id' => $this->adminId, 'username' => $this->adminUserInfo?->username ?? '', 'method' => $context['method'], 'router' => $context['router'], 'service_name' => $context['service_name'] ?? '', - 'ip' => $context['ip'], + 'ip' => current($context['ip']) ?: '0.0.0.0', ]); }); } diff --git a/app/Common/Trait/ClientIpTrait.php b/app/Common/Trait/ClientIpTrait.php index 671d63b..457a461 100644 --- a/app/Common/Trait/ClientIpTrait.php +++ b/app/Common/Trait/ClientIpTrait.php @@ -15,7 +15,7 @@ trait ClientIpTrait * @var RequestInterface */ #[Inject] - protected readonly RequestInterface $request; + protected RequestInterface $request; /** * @var string[] diff --git a/app/Common/Trait/ClientOsTrait.php b/app/Common/Trait/ClientOsTrait.php index a501bce..f4b8efb 100644 --- a/app/Common/Trait/ClientOsTrait.php +++ b/app/Common/Trait/ClientOsTrait.php @@ -11,7 +11,7 @@ trait ClientOsTrait * @var RequestInterface */ #[Inject] - protected readonly RequestInterface $request; + protected RequestInterface $request; /** * @return string diff --git a/app/Middleware/Admin/PermissionMiddleware.php b/app/Middleware/Admin/PermissionMiddleware.php index 5e85084..ccd3c16 100644 --- a/app/Middleware/Admin/PermissionMiddleware.php +++ b/app/Middleware/Admin/PermissionMiddleware.php @@ -45,7 +45,7 @@ class PermissionMiddleware implements MiddlewareInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - $adminId = $this->getToken()?->claims()?->get(RegisteredClaims::ID) ?? 0; + $adminId = (int) $this->getToken()?->claims()?->get(RegisteredClaims::ID) ?? 0; if ($adminId <= 0) throw new ErrException('账户不存在'); $this->adminUserInfo = $this->getAdminUserInfo($adminId); diff --git a/app/Model/AdminUserLoginLog.php b/app/Model/AdminUserLoginLog.php index 833e4a4..b848d95 100644 --- a/app/Model/AdminUserLoginLog.php +++ b/app/Model/AdminUserLoginLog.php @@ -34,7 +34,7 @@ class AdminUserLoginLog extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['id', 'username', 'ip', 'os', 'browser', 'status', 'message', 'login_time', 'remark']; + protected array $fillable = ['id','admin_user_id', 'username', 'ip', 'os', 'browser', 'status', 'message', 'login_time', 'remark']; /** * The attributes that should be cast to native types. diff --git a/app/Model/AdminUserOperationLog.php b/app/Model/AdminUserOperationLog.php index b0d4606..94cfe34 100644 --- a/app/Model/AdminUserOperationLog.php +++ b/app/Model/AdminUserOperationLog.php @@ -28,7 +28,7 @@ class AdminUserOperationLog extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['id', 'username', 'method', 'router', 'service_name', 'ip', 'ip_location', 'created_at', 'updated_at', 'remark']; + protected array $fillable = ['id', 'admin_user_id','username', 'method', 'router', 'service_name', 'ip', 'ip_location', 'created_at', 'updated_at', 'remark']; /** * The attributes that should be cast to native types. diff --git a/app/Service/Admin/AdminUser/PermissionService.php b/app/Service/Admin/AdminUser/PermissionService.php index 2afb401..fc3166f 100644 --- a/app/Service/Admin/AdminUser/PermissionService.php +++ b/app/Service/Admin/AdminUser/PermissionService.php @@ -19,6 +19,7 @@ use App\Constants\ResultCode; use App\Exception\ErrException; use App\Service\Admin\BaseAdminService; use Hyperf\Collection\Arr; +use Hyperf\Di\Annotation\Inject; class PermissionService extends BaseAdminService { @@ -27,11 +28,13 @@ class PermissionService extends BaseAdminService /** * @var AdminRoleRepository */ + #[Inject] protected AdminRoleRepository $adminRoleRepository; /** * @var AdminMenuRepository */ + #[Inject] protected AdminMenuRepository $adminMenuRepository; /** diff --git a/app/Service/Admin/AdminUser/UserService.php b/app/Service/Admin/AdminUser/UserService.php index 771d7b2..ff35970 100644 --- a/app/Service/Admin/AdminUser/UserService.php +++ b/app/Service/Admin/AdminUser/UserService.php @@ -58,7 +58,7 @@ class UserService extends BaseAdminService return $this->adminReturn->success( 'success', Arr::only( - $this->getAdminUserInfo($this->adminId) ?: [], + $this->getAdminUserInfo($this->adminId)?->toArray() ?: [], ['username', 'nickname', 'avatar', 'signed', 'backend_setting', 'phone', 'email'] ) );