Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-common
Commits
ac71db42
Commit
ac71db42
authored
Feb 28, 2023
by
zhangdongying
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 全局日志组件编写
parent
dffa08df
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
315 additions
and
0 deletions
+315
-0
AppOperateLogService.php
src/GlobalLog/AppOperateLogService.php
+145
-0
OperateTypeEnum.php
src/GlobalLog/Enum/OperateTypeEnum.php
+0
-0
OperateLogService.php
src/GlobalLog/Service/OperateLogService.php
+113
-0
RedisQueueService.php
src/GlobalLog/Service/RedisQueueService.php
+57
-0
No files found.
src/GlobalLog/AppOperateLogService.php
0 → 100644
View file @
ac71db42
<?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
src/GlobalLog/Enum/OperateTypeEnum.php
0 → 100644
View file @
ac71db42
src/GlobalLog/Service/OperateLogService.php
0 → 100644
View file @
ac71db42
<?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
src/GlobalLog/Service/RedisQueueService.php
0 → 100644
View file @
ac71db42
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment