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
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* 项目操作日志
*
* @author zhangdongying
* @date 2023-02-28
*/
declare(strict_types=1);
namespace Meibuyu\Common\GlobalLog;
use Hyperf\Contract\ConfigInterface;
use Hyperf\DbConnection\Model\Model;
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);
}
/**
* 将数组转换成JSON
*
* @param array $array 数组
* @return string
*/
public static function encodeArrayToJson(array $array): string
{
return json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
/**
* 获取客户端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 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;
}
}
/**
* 添加用户操作日志
*
* @param string $tableName 表名
* @param string $recordId 记录ID
* @param string $operateType 操作类型
* @param string|array|Model $param 参数
* @param string|array|Model $before 修改之前数据
* @param string|array|Model $after 修改之后数据
* @param string $remark 备注
* @return bool
* @throws \Exception
*/
public function addUserOperateLog(
string $tableName,
string $recordId,
string $operateType,
$param,
$before,
$after,
string $remark = ''
): bool
{
return $this->operateLogService->addOperateLog(
make(RequestInterface::class)->header('hwq-request-id', ''),
$this->config->get('app_name'),
Auth::id() ?? 0,
Auth::user()['name'] ?? '',
$this->getClientIp(),
make(RequestInterface::class)->url(),
$tableName,
$recordId,
$operateType,
$this->formatSourceData($param),
$this->formatSourceData($before),
$this->formatSourceData($after),
$remark
);
}
/**
* 添加系统操作日志
*
* @param string $action 方法全路径
* @param string $tableName 表名
* @param string $recordId 记录ID
* @param string $operateType 操作类型
* @param string|array|Model $param 参数
* @param string|array|Model $before 修改之前数据
* @param string|array|Model $after 修改之后数据
* @param string $remark 备注
* @return bool
* @throws \Exception
*/
public function addSystemOperateLog(
string $action,
string $tableName,
string $recordId,
string $operateType,
$param,
$before,
$after,
string $remark = ''
): bool
{
return $this->operateLogService->addOperateLog(
'',
$this->config->get('app_name'),
0,
'system',
'',
$action,
$tableName,
$recordId,
$operateType,
$this->formatSourceData($param),
$this->formatSourceData($before),
$this->formatSourceData($after),
$remark
);
}
}