feat : admin log

This commit is contained in:
2025-09-16 14:30:12 +08:00
parent a6d6738ab2
commit 2613b031ae
18 changed files with 662 additions and 10 deletions

View File

@@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
namespace App\Aspect;
use App\Lib\Log\Logger;
use App\Model\AdminUserLoginLog;
use App\Repository\AdminUserRepository;
use App\Service\Admin\Login\LoginService;
use App\Trait\ClientIpTrait;
use App\Trait\ClientOsTrait;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Exception\Exception;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Throwable;
#[Aspect]
class AdminLoginLogAspect extends AbstractAspect
{
use ClientIpTrait;
use ClientOsTrait;
/**
* 切入类
* @var array|\class-string[]
*/
public array $classes = [
LoginService::class,
];
/**
* @var bool
*/
private bool $loginSuccess = false;
/**
* @var string
*/
private string $loginMsg = '';
/**
* @param Logger $logger
* @param RequestInterface $request
* @param AdminUserRepository $adminUserRepository
* @param AdminUserLoginLog $adminUserLoginLogModel
*/
public function __construct(
protected readonly Logger $logger,
protected readonly RequestInterface $request,
protected readonly AdminUserRepository $adminUserRepository,
protected readonly AdminUserLoginLog $adminUserLoginLogModel,
) {}
/**
* @param ProceedingJoinPoint $proceedingJoinPoint
* @return mixed
* @throws Exception
* @throws Throwable
*/
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
// 写日志
try {
$res = $proceedingJoinPoint->process();
$this->loginSuccess = true;
$this->loginMsg = 'success';
} catch (Throwable $throwable) {
$this->loginSuccess = false;
$this->loginMsg = $throwable->getMessage();
throw $throwable;
} finally {
$this->writeLoginLog();
}
// 返回
return $res;
}
/**
* @return void
*/
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',
];
$this->logger->request()->info('admin_login_log', $context);
$this->adminUserLoginLogModel->save([
'admin_user_id' => $userInfo?->id ?? 0,
'username' => $userInfo?->username ?? '',
'ip' => $context['ip'],
'os' => $context['os'],
'browser' => $context['browser'] ?? '',
'status' => $this->loginSuccess ? 1 : 2,
'message' => $this->loginMsg,
]);
});
}
}

View File

@@ -6,6 +6,10 @@ namespace App\Aspect;
use App\Lib\Log\Logger;
use App\Lib\Return\AdminReturn;
use App\Model\AdminUser;
use App\Model\AdminUserOperationLog;
use App\Service\Admin\Login\LoginService;
use App\Trait\AdminUserTrait;
use App\Trait\ClientIpTrait;
use Hyperf\Context\Context;
use Hyperf\Coroutine\Coroutine;
@@ -16,9 +20,10 @@ use Psr\Container\ContainerInterface;
use Hyperf\Di\Aop\ProceedingJoinPoint;
#[Aspect]
class AdminReturnAspect extends AbstractAspect
class AdminReturnLogAspect extends AbstractAspect
{
use ClientIpTrait;
use AdminUserTrait;
/**
* 切入类
@@ -33,15 +38,23 @@ class AdminReturnAspect extends AbstractAspect
*/
protected int $adminId = 0;
/**
* @var ?AdminUser
*/
protected ?AdminUser $adminUserInfo = null;
/**
* @param Logger $logger
* @param RequestInterface $request
* @param AdminUserOperationLog $adminUserOperationLogModel
*/
public function __construct(
protected readonly Logger $logger,
protected readonly RequestInterface $request,
protected readonly AdminUserOperationLog $adminUserOperationLogModel,
) {
$this->adminId = Context::get('current_admin_id',0);
if ($this->adminId > 0) $this->adminUserInfo = $this->getAdminUserInfo($this->adminId);
}
public function process(ProceedingJoinPoint $proceedingJoinPoint)
@@ -53,7 +66,7 @@ class AdminReturnAspect extends AbstractAspect
$responseData = $proceedingJoinPoint->process();
// 写日志
$this->writeLog($requestData, $responseData);
$this->writeOperationLog($requestData, $responseData);
// 返回
return $requestData;
@@ -64,7 +77,7 @@ class AdminReturnAspect extends AbstractAspect
* @param array $responseData
* @return void
*/
private function writeLog(array $requestData = [], array $responseData = []): void
private function writeOperationLog(array $requestData = [], array $responseData = []): void
{
Coroutine::create(function () use ($requestData, $responseData) {
$context = [
@@ -77,6 +90,15 @@ class AdminReturnAspect extends AbstractAspect
];
$this->logger->request()->info('admin_request_log', $context);
$this->adminUserOperationLogModel->save([
'admin_user_id' => $this->adminId,
'username' => $this->adminUserInfo?->username ?? '',
'method' => $context['method'],
'router' => $context['router'],
'service_name' => $context['service_name'] ?? '',
'ip' => $context['ip'],
]);
});
}
}