Commit e94d391c authored by zhangdongying's avatar zhangdongying

feat: 新增调用示例

parent fa0a82bf
......@@ -10,6 +10,7 @@ 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;
......@@ -74,15 +75,32 @@ class AppOperateLogService
}
}
/**
* 格式化原始数据
*
* @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 $param 参数
* @param string|array $before 修改之前数据
* @param string|array $after 修改之后数据
* @param string|array|Model $param 参数
* @param string|array|Model $before 修改之前数据
* @param string|array|Model $after 修改之后数据
* @param string $remark 备注
* @return bool
* @throws \Exception
......@@ -107,9 +125,9 @@ class AppOperateLogService
$tableName,
$recordId,
$operateType,
is_array($param) ? self::encodeArrayToJson($param) : (string)$param,
is_array($before) ? self::encodeArrayToJson($before) : (string)$before,
is_array($after) ? self::encodeArrayToJson($after) : (string)$after,
$this->formatSourceData($param),
$this->formatSourceData($before),
$this->formatSourceData($after),
$remark
);
}
......@@ -121,9 +139,9 @@ class AppOperateLogService
* @param string $tableName 表名
* @param string $recordId 记录ID
* @param string $operateType 操作类型
* @param string|array $param 参数
* @param string|array $before 修改之前数据
* @param string|array $after 修改之后数据
* @param string|array|Model $param 参数
* @param string|array|Model $before 修改之前数据
* @param string|array|Model $after 修改之后数据
* @param string $remark 备注
* @return bool
* @throws \Exception
......@@ -149,9 +167,9 @@ class AppOperateLogService
$tableName,
$recordId,
$operateType,
is_array($param) ? self::encodeArrayToJson($param) : (string)$param,
is_array($before) ? self::encodeArrayToJson($before) : (string)$before,
is_array($after) ? self::encodeArrayToJson($after) : (string)$after,
$this->formatSourceData($param),
$this->formatSourceData($before),
$this->formatSourceData($after),
$remark
);
}
......
<?php
/**
* 操作类型枚举
*
* @author zhangdongying
* @date 2023-03-01
*/
declare(strict_types=1);
namespace Meibuyu\Common\GlobalLog\Enum;
class OperateTypeEnum
{
// 新增
const ADD = 'ADD';
// 修改
const UPDATE = 'UPDATE';
// 删除
const DELETE = 'DELETE';
}
\ No newline at end of file
<?php
/**
* 操作日志示例
*
* @author zhangdongying
* @date 2023-03-01
*/
declare(strict_types=1);
namespace Meibuyu\Common\GlobalLog\Example;
use Meibuyu\Common\GlobalLog\AppOperateLogService;
use Meibuyu\Common\GlobalLog\Enum\OperateTypeEnum;
use Psr\Container\ContainerInterface;
class AppOperateLogExample
{
/**
* 容器实例
*/
protected $container;
/**
* 初始化
*
* @param ContainerInterface $container 容器实例
* @throws \Throwable
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* addUserOperateLog 调用示例
*
* @return void
* @throws \Throwable
*/
public function addUserOperateLogExample()
{
$this->container->get(AppOperateLogService::class)->addUserOperateLog(
'products',
'1',
OperateTypeEnum::UPDATE,
['product_id' => 1, 'name' => 'bbb'],
['id' => 1, 'name' => 'aaa'],
['id' => 2, 'name' => 'bbb']
);
}
/**
* addSystemOperateLog 调用示例
*
* @return void
* @throws \Throwable
*/
public function addSystemOperateLogExample()
{
$this->container->get(AppOperateLogService::class)->addSystemOperateLog(
'product_data_update',
'products',
'1',
OperateTypeEnum::UPDATE,
['product_id' => 1, 'name' => 'bbb'],
['id' => 1, 'name' => 'aaa'],
['id' => 2, 'name' => 'bbb']
);
}
}
\ 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