Commit ac71db42 authored by zhangdongying's avatar zhangdongying

feat: 全局日志组件编写

parent dffa08df
<?php
/**
* 项目操作日志
*
* @author zhangdongying
* @date 2023-02-28
*/
declare(strict_types=1);
namespace Meibuyu\Common\GlobalLog;
use Hyperf\Contract\ConfigInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use Meibuyu\Common\GlobalLog\Service\OperateLogService;
use Meibuyu\Micro\Model\Auth;
use Psr\Container\ContainerInterface;
class AppOperateLogService
{
/**
* 配置
*/
protected $config;
/**
* 队列服务
*/
protected $operateLogService;
/**
* 初始化
*
* @param ContainerInterface $container 容器实例
* @throws \Throwable
*/
public function __construct(ContainerInterface $container)
{
$this->config = $container->get(ConfigInterface::class);
$this->operateLogService = $container->get(OperateLogService::class);
}
/**
* 获取客户端IP
*
* @return string
*/
public function getClientIp(): string
{
$request = make(RequestInterface::class);
$header = $request->getHeaders();
if (isset($header['http_client_ip'][0])) {
return $header['http_client_ip'][0];
} elseif (isset($header['x-real-ip'][0])) {
return $header['x-real-ip'][0];
} elseif (isset($header['x-forwarded-for'][0])) {
return $header['x-forwarded-for'][0];
} elseif (isset($header['http_x_forwarded_for'][0])) {
return $header['http_x_forwarded_for'][0];
} else {
$server = $request->getServerParams();
return $server['remote_addr'] ?? '';
}
}
/**
* 添加用户操作日志
*
* @param string $tableName 表名
* @param string $recordId 记录ID
* @param string $operateType 操作类型
* @param string $param 参数
* @param string $before 修改之前数据
* @param string $after 修改之后数据
* @param string $remark 备注
* @return bool
* @throws \Exception
*/
public function addUserOperateLog(
string $tableName,
string $recordId,
string $operateType,
string $param,
string $before,
string $after,
string $remark = ''
)
{
return $this->operateLogService->addOperateLog(
$this->config->get('app_name'),
Auth::id() ?? 0,
Auth::user()['name'] ?? '',
$this->getClientIp(),
make(RequestInterface::class)->fullUrl(),
$tableName,
$recordId,
$operateType,
$param,
$before,
$after,
$remark
);
}
/**
* 添加系统操作日志
*
* @param string $action 方法全路径
* @param string $tableName 表名
* @param string $recordId 记录ID
* @param string $operateType 操作类型
* @param string $param 参数
* @param string $before 修改之前数据
* @param string $after 修改之后数据
* @param string $remark 备注
* @return bool
* @throws \Exception
*/
public function addSystemOperateLog(
string $action,
string $tableName,
string $recordId,
string $operateType,
string $param,
string $before,
string $after,
string $remark = ''
)
{
return $this->operateLogService->addOperateLog(
$this->config->get('app_name'),
0,
'system',
'',
$action,
$tableName,
$recordId,
$operateType,
$param,
$before,
$after,
$remark
);
}
}
\ No newline at end of file
<?php
/**
* 操作日志
*
* @author zhangdongying
* @date 2023-02-28
*/
declare(strict_types=1);
namespace Meibuyu\Common\GlobalLog\Service;
use Psr\Container\ContainerInterface;
class OperateLogService
{
/**
* 队列服务
*/
protected $queueService;
/**
* 操作日志队列名称
*
* @var string
*/
protected $queue = 'QUEUE:GLOBAL_OPERATE_LOG';
/**
* 初始化
*
* @param ContainerInterface $container 容器实例
* @throws \Throwable
*/
public function __construct(ContainerInterface $container)
{
$this->queueService = $container->get(RedisQueueService::class);
}
/**
* 设置队列名称
*
* @param string $name 队列名称
* @return OperateLogService
*/
public function setQueueName(string $name): OperateLogService
{
$this->queue = $name;
return $this;
}
/**
* 将数组转换成JSON
*
* @param array $array 数组
* @return string
*/
public static function encodeArrayToJson(array $array): string
{
return json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
/**
* 添加操作日志
*
* @param string $appName 项目名称
* @param int $operatorId 操作人ID
* @param string $operatorName 操作人名称
* @param string $clientIp 客户端IP
* @param string $url 请求链接
* @param string $tableName 表名
* @param string $recordId 记录ID
* @param string $operateType 操作类型
* @param string $param 参数
* @param string $before 修改之前数据
* @param string $after 修改之后数据
* @param string $remark 备注
* @return bool
* @throws \Exception
*/
public function addOperateLog(
string $appName,
int $operatorId,
string $operatorName,
string $clientIp,
string $url,
string $tableName,
string $recordId,
string $operateType,
string $param,
string $before,
string $after,
string $remark
): bool
{
$data = [
'app_name' => $appName,
'operator_id' => $operatorId,
'operator_name' => $operatorName,
'client_ip' => $clientIp,
'url' => $url,
'table_name' => $tableName,
'record_id' => $recordId,
'operate_type' => $operateType,
'param' => $param,
'before' => $before,
'after' => $after,
'remark' => $remark,
'created_at' => date('Y-m-d H:i:s'),
];
return (bool)$this->queueService->push($this->queue, self::encodeArrayToJson($data));
}
}
\ No newline at end of file
<?php
/**
* REDIS队列服务
*
* @author zhangdongying
* @date 2023-02-28
*/
declare(strict_types=1);
namespace Meibuyu\Common\GlobalLog\Service;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerInterface;
class RedisQueueService
{
/**
* REDIS实例
*/
protected $redis;
/**
* 初始化
*
* @param ContainerInterface $container 容器实例
* @throws \Throwable
*/
public function __construct(ContainerInterface $container)
{
$this->redis = $container->get(Redis::class);
}
/**
* 添加到队列中
*
* @param string $queue 队列名称
* @param string $data 数据
* @return mixed
* @throws \Exception
*/
public function push(string $queue, string $data)
{
return $this->redis->lPush($queue, $data);
}
/**
* 从队列阻塞拉取
*
* @param string $queue 队列名称
* @return int $timeout 超时秒数
* @throws \Exception
*/
public function blockPop(string $queue, int $timeout)
{
return $this->redis->brPop($queue, $timeout);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment