feat: 添加记录操作日志功能
This commit is contained in:
@@ -51,6 +51,7 @@ class ExceptionHandle extends Handle
|
||||
public function render($request, Throwable $e): Response
|
||||
{
|
||||
// 添加自定义异常处理机制
|
||||
$request->exception_info = $e;
|
||||
|
||||
// 其他错误交给系统处理
|
||||
return parent::render($request, $e);
|
||||
|
||||
@@ -4,4 +4,5 @@ return [
|
||||
// 登录验证
|
||||
app\admin\middleware\v1\Auth::class,
|
||||
app\admin\middleware\v1\RequestId::class,
|
||||
app\admin\middleware\v1\OperateLog::class,
|
||||
];
|
||||
|
||||
53
app/admin/middleware/v1/OperateLog.php
Normal file
53
app/admin/middleware/v1/OperateLog.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\middleware\v1;
|
||||
|
||||
use app\admin\model\v1\SysOperateLog;
|
||||
|
||||
class OperateLog
|
||||
{
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
$response = $next($request);
|
||||
|
||||
$log = [
|
||||
'user_id' => $request->uid,
|
||||
'title' => '操作日志',
|
||||
'version' => $request->layer(),
|
||||
'method' => $request->method(),
|
||||
'controller' => $request->controller(base: true),
|
||||
'action' => $request->action(),
|
||||
'url' => $request->url(),
|
||||
'ip' => $request->ip(),
|
||||
'params' => json_encode($request->param()),
|
||||
'status' => $response->getCode()
|
||||
];
|
||||
|
||||
if (empty($request->exception_info)) {
|
||||
// 记录正常返回信息
|
||||
$contentDisposition = $response->getHeader('Content-Disposition');
|
||||
if (!empty($contentDisposition) && \think\helper\Str::startsWith($contentDisposition, 'attachment')) {
|
||||
$log['message'] = $contentDisposition;
|
||||
} else {
|
||||
$log['message'] = $response->getData();
|
||||
if (is_array($log['message'])) {
|
||||
$log['message'] = json_encode($log['message'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 记录异常返回信息
|
||||
$log['message'] = (string)$request->exception_info;
|
||||
}
|
||||
SysOperateLog::create($log);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
35
app/admin/model/v1/SysOperateLog.php
Normal file
35
app/admin/model/v1/SysOperateLog.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysOperateLog extends Model
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'sys_operate_log';
|
||||
|
||||
// 主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 字段信息
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'user_id' => 'int',
|
||||
'title' => 'string',
|
||||
'version' => 'string',
|
||||
'method' => 'string',
|
||||
'controller' => 'string',
|
||||
'action' => 'string',
|
||||
'url' => 'string',
|
||||
'ip' => 'string',
|
||||
'params' => 'string',
|
||||
'status' => 'int',
|
||||
'message' => 'string',
|
||||
'created_at' => 'datetime'
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user