优化后台管理日志记录

pull/462/MERGE
Karson 2024-03-25 22:28:43 +08:00
parent 448eaad5f5
commit 9f2c08414a
4 changed files with 13 additions and 10 deletions

View File

@ -4,7 +4,7 @@ namespace app\admin\behavior;
class AdminLog class AdminLog
{ {
public function run(&$params) public function run(&$response)
{ {
//只记录POST请求的日志 //只记录POST请求的日志
if (request()->isPost() && config('fastadmin.auto_record_log')) { if (request()->isPost() && config('fastadmin.auto_record_log')) {

View File

@ -53,6 +53,7 @@ class Adminlog extends Backend
$query->where('admin_id', 'in', $childrenAdminIds); $query->where('admin_id', 'in', $childrenAdminIds);
} }
}) })
->field('content,useragent', true)
->order($sort, $order) ->order($sort, $order)
->paginate($limit); ->paginate($limit);

View File

@ -41,8 +41,8 @@ class AdminLog extends Model
/** /**
* 记录日志 * 记录日志
* @param string $title * @param string $title 日志标题
* @param string $content * @param string $content 日志内容
*/ */
public static function record($title = '', $content = '') public static function record($title = '', $content = '')
{ {
@ -50,6 +50,9 @@ class AdminLog extends Model
$admin_id = $auth->isLogin() ? $auth->id : 0; $admin_id = $auth->isLogin() ? $auth->id : 0;
$username = $auth->isLogin() ? $auth->username : __('Unknown'); $username = $auth->isLogin() ? $auth->username : __('Unknown');
// 设置过滤函数
request()->filter('trim,strip_tags,htmlspecialchars');
$controllername = Loader::parseName(request()->controller()); $controllername = Loader::parseName(request()->controller());
$actionname = strtolower(request()->action()); $actionname = strtolower(request()->action());
$path = str_replace('.', '/', $controllername) . '/' . $actionname; $path = str_replace('.', '/', $controllername) . '/' . $actionname;
@ -60,12 +63,12 @@ class AdminLog extends Model
} }
} }
} }
$content = $content ? $content : self::$content; $content = $content ?: self::$content;
if (!$content) { if (!$content) {
$content = request()->param('', null, 'trim,strip_tags,htmlspecialchars'); $content = request()->param('') ?: file_get_contents("php://input");
$content = self::getPureContent($content); $content = self::getPureContent($content);
} }
$title = $title ? $title : self::$title; $title = $title ?: self::$title;
if (!$title) { if (!$title) {
$title = []; $title = [];
$breadcrumb = Auth::instance()->getBreadcrumb($path); $breadcrumb = Auth::instance()->getBreadcrumb($path);
@ -77,18 +80,18 @@ class AdminLog extends Model
self::create([ self::create([
'title' => $title, 'title' => $title,
'content' => !is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content, 'content' => !is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
'url' => substr(request()->url(), 0, 1500), 'url' => substr(xss_clean(strip_tags(request()->url())), 0, 1500),
'admin_id' => $admin_id, 'admin_id' => $admin_id,
'username' => $username, 'username' => $username,
'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255), 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
'ip' => request()->ip() 'ip' => xss_clean(strip_tags(request()->ip()))
]); ]);
} }
/** /**
* 获取已屏蔽关键信息的数据 * 获取已屏蔽关键信息的数据
* @param $content * @param $content
* @return false|string * @return array
*/ */
protected static function getPureContent($content) protected static function getPureContent($content)
{ {

View File

@ -29,7 +29,6 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
{field: 'title', title: __('Title'), operate: 'LIKE %...%', placeholder: '模糊搜索'}, {field: 'title', title: __('Title'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
{field: 'url', title: __('Url'), formatter: Table.api.formatter.url}, {field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
{field: 'ip', title: __('IP'), events: Table.api.events.ip, formatter: Table.api.formatter.search}, {field: 'ip', title: __('IP'), events: Table.api.events.ip, formatter: Table.api.formatter.search},
{field: 'browser', title: __('Browser'), operate: false, formatter: Controller.api.formatter.browser},
{field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true}, {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
{ {
field: 'operate', title: __('Operate'), table: table, field: 'operate', title: __('Operate'), table: table,