This commit is contained in:
70
app/Middleware/RequestLogsMiddleware.php
Normal file
70
app/Middleware/RequestLogsMiddleware.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/6/3
|
||||
* Time: 下午10:43
|
||||
* Description:
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Middleware;
|
||||
|
||||
use App\Service\QueueService;
|
||||
use App\Service\SysService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/6/3
|
||||
* Time: 下午10:44
|
||||
* Description: 请求日志中间件
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
class RequestLogsMiddleware implements MiddlewareInterface
|
||||
{
|
||||
#[Inject]
|
||||
protected QueueService $queueService;
|
||||
|
||||
#[Inject]
|
||||
protected SysService $sysService;
|
||||
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$method = $request->getMethod(); // 请求方法
|
||||
$uri = $request->getUri()->getPath(); // 请求的URI路径
|
||||
$params = $request->getMethod() === 'POST' ? $request->getParsedBody() : $request->getQueryParams();
|
||||
|
||||
$clientIp = $this->sysService->getClientIpInfo($request);
|
||||
|
||||
$data = [
|
||||
'params' => $params,
|
||||
'method' => $method,
|
||||
'uri' => $uri,
|
||||
'client_ip' => $clientIp,
|
||||
];
|
||||
|
||||
// 添加请求日志,存储到队列中
|
||||
$this->queueService->make($data)->writeRequestLogs();
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user