AppOperateLogService.php 4.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * 项目操作日志
 *
 * @author zhangdongying
 * @date   2023-02-28
 */
declare(strict_types=1);

namespace Meibuyu\Common\GlobalLog;

use Hyperf\Contract\ConfigInterface;
13
use Hyperf\DbConnection\Model\Model;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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);
    }

zhangdongying's avatar
zhangdongying committed
43 44 45 46 47 48 49 50 51 52 53
    /**
     * 将数组转换成JSON
     *
     * @param array $array 数组
     * @return string
     */
    public static function encodeArrayToJson(array $array): string
    {
        return json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    /**
     * 获取客户端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'] ?? '';
        }
    }

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    /**
     * 格式化原始数据
     *
     * @param mixed $data 原始数据
     * @return string
     */
    public function formatSourceData($data): string
    {
        if ($data instanceof Model) {
            return self::encodeArrayToJson($data->toArray());
        } elseif (is_array($data)) {
            return self::encodeArrayToJson($data);
        } else {
            return (string)$data;
        }
    }

95 96 97 98
    /**
     * 添加用户操作日志
     *
     * @param string $tableName 表名
99
     * @param string|int $recordId 记录ID
zhangdongying's avatar
zhangdongying committed
100
     * @param string $operateType 操作类型,例如:{主菜单}-{子菜单}-{操作},产品管理-产品列表-产品删除
101 102 103
     * @param string|array|Model $param 参数
     * @param string|array|Model $before 修改之前数据
     * @param string|array|Model $after 修改之后数据
104 105 106 107 108 109
     * @param string $remark 备注
     * @return bool
     * @throws \Exception
     */
    public function addUserOperateLog(
        string $tableName,
110
        $recordId,
111
        string $operateType,
zhangdongying's avatar
zhangdongying committed
112 113 114
        $param,
        $before,
        $after,
115
        string $remark = ''
zhangdongying's avatar
zhangdongying committed
116
    ): bool
117 118
    {
        return $this->operateLogService->addOperateLog(
119
            make(RequestInterface::class)->header('hwq-request-id', ''),
120 121 122 123
            $this->config->get('app_name'),
            Auth::id() ?? 0,
            Auth::user()['name'] ?? '',
            $this->getClientIp(),
zhangdongying's avatar
zhangdongying committed
124
            make(RequestInterface::class)->url(),
125
            $tableName,
126
            (string)$recordId,
127
            $operateType,
128 129 130
            $this->formatSourceData($param),
            $this->formatSourceData($before),
            $this->formatSourceData($after),
131 132 133 134 135 136 137 138 139
            $remark
        );
    }

    /**
     * 添加系统操作日志
     *
     * @param string $action 方法全路径
     * @param string $tableName 表名
140
     * @param string|int $recordId 记录ID
zhangdongying's avatar
zhangdongying committed
141
     * @param string $operateType 操作类型,例如:物流运单推送物流服务商
142 143 144
     * @param string|array|Model $param 参数
     * @param string|array|Model $before 修改之前数据
     * @param string|array|Model $after 修改之后数据
145 146 147 148 149 150 151
     * @param string $remark 备注
     * @return bool
     * @throws \Exception
     */
    public function addSystemOperateLog(
        string $action,
        string $tableName,
152
        $recordId,
153
        string $operateType,
zhangdongying's avatar
zhangdongying committed
154 155 156
        $param,
        $before,
        $after,
157
        string $remark = ''
zhangdongying's avatar
zhangdongying committed
158
    ): bool
159 160
    {
        return $this->operateLogService->addOperateLog(
161
            '',
162 163 164 165 166 167
            $this->config->get('app_name'),
            0,
            'system',
            '',
            $action,
            $tableName,
168
            (string)$recordId,
169
            $operateType,
170 171 172
            $this->formatSourceData($param),
            $this->formatSourceData($before),
            $this->formatSourceData($after),
173 174 175 176
            $remark
        );
    }
}