OperateLogService.php 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?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
     */
26
    protected $queue = 'APP:QUEUE:GLOBAL_OPERATE_LOG';
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    /**
     * 初始化
     *
     * @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;
    }

    /**
52
     * 生成唯一ID
53 54 55
     *
     * @return string
     */
56
    public static function generateUniqueId(): string
57
    {
58
        return sha1(uniqid('', true) . mt_rand(10000, 99999));
59 60 61 62 63
    }

    /**
     * 添加操作日志
     *
64
     * @param string $requestId 请求ID
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
     * @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(
81
        string $requestId,
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        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 = [
97 98
            'log_sn'        => self::generateUniqueId(),
            'request_id'    => $requestId,
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            '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'),
        ];

114
        return (bool)$this->queueService->push($this->queue, json_encode($data));
115 116
    }
}