mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 15:57:50 +08:00
116 lines
3.1 KiB
PHP
116 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Aspect;
|
|
|
|
use App\Common\Repository\AdminUserRepository;
|
|
use App\Common\Trait\ClientIpTrait;
|
|
use App\Common\Trait\ClientOsTrait;
|
|
use App\Lib\Log\Logger;
|
|
use App\Model\AdminUserLoginLog;
|
|
use App\Service\Admin\Login\LoginService;
|
|
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;
|
|
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 RequestInterface $request,
|
|
protected readonly AdminUserRepository $adminUserRepository,
|
|
protected readonly AdminUserLoginLog $adminUserLoginLogModel,
|
|
) {}
|
|
|
|
/**
|
|
* @param ProceedingJoinPoint $proceedingJoinPoint
|
|
* @return mixed
|
|
* @throws Exception
|
|
* @throws Throwable
|
|
*/
|
|
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
|
|
{
|
|
// 写日志
|
|
|
|
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
|
|
{
|
|
$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->create([
|
|
'admin_user_id' => $userInfo?->id ?? 0,
|
|
'username' => $userInfo?->username ?? '',
|
|
'ip' => current($context['ip']) ?: '0.0.0.0',
|
|
'os' => $context['os'],
|
|
'browser' => $context['browser'] ?? '',
|
|
'status' => $this->loginSuccess ? 1 : 2,
|
|
'message' => $this->loginMsg,
|
|
]);
|
|
});
|
|
}
|
|
}
|