From 32d94bb3e01c77d3dcb965832a74a0659c577127 Mon Sep 17 00:00:00 2001 From: wolfcode <37436228+wolf-leo@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:07:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(log):=20=E6=96=B0=E5=A2=9E=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=88=A0=E9=99=A4=E9=83=A8=E5=88=86=E6=97=A5=E5=BF=97?= =?UTF-8?q?=20add=20function=20to=20delete=20logs=20older=20than=20specifi?= =?UTF-8?q?ed=20months?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add deleteMonthLog functionality to log system - Create new route and controller method for handling log deletion - Implement frontend UI and logic for selecting and confirming log deletion-Add necessary database queries to safely delete logs --- app/admin/controller/system/Log.php | 39 +++++++++++++++++++ .../view/system/log/delete_month_log.html | 23 +++++++++++ public/static/admin/js/system/log.js | 27 ++++++++++++- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 app/admin/view/system/log/delete_month_log.html diff --git a/app/admin/controller/system/Log.php b/app/admin/controller/system/Log.php index 6500901..118b6aa 100644 --- a/app/admin/controller/system/Log.php +++ b/app/admin/controller/system/Log.php @@ -87,6 +87,45 @@ class Log extends AdminController return Excel::exportData($list, $header, $fileName, 'xlsx'); } + + #[NodeAnnotation(title: '删除指定日志', auth: true)] + public function deleteMonthLog(Request $request) + { + if (!$request->isAjax()) { + return $this->fetch(); + } + + if ($this->isDemo) $this->error('演示环境下不允许操作'); + + $monthsAgo = $request->param('month/d', 0); + if ($monthsAgo < 1) $this->error('月份错误'); + + $currentDate = new \DateTime(); + $currentDate->modify("-$monthsAgo months"); + + $dbPrefix = env('DB_PREFIX'); + $dbLike = "{$dbPrefix}system_log_"; + $tables = Db::query("SHOW TABLES LIKE '$dbLike%'"); + $threshold = date('Ym', strtotime("-$monthsAgo month")); + $tableNames = []; + try { + foreach ($tables as $table) { + $tableName = current($table); + if (!preg_match("/^$dbLike\d{6}$/", $tableName)) continue; + $datePart = substr($tableName, -6); + $issetTable = Db::query("SHOW TABLES LIKE '$tableName'"); + if (!$issetTable) continue; + if ($datePart - $threshold <= 0) { + Db::execute("DROP TABLE `$tableName`"); + $tableNames[] = $tableName; + } + } + }catch (PDOException) { + } + if (empty($tableNames)) $this->error('没有需要删除的表'); + $this->success('操作成功 - 共删除 ' . count($tableNames) . ' 张表
' . implode('
', $tableNames)); + } + #[MiddlewareAnnotation(ignore: MiddlewareAnnotation::IGNORE_LOG)] #[NodeAnnotation(title: '框架日志', auth: true, ignore: NodeAnnotation::IGNORE_NODE)] public function record(): Json|string diff --git a/app/admin/view/system/log/delete_month_log.html b/app/admin/view/system/log/delete_month_log.html new file mode 100644 index 0000000..6935c28 --- /dev/null +++ b/app/admin/view/system/log/delete_month_log.html @@ -0,0 +1,23 @@ +
+ +
+ +
+ +
+
删除
+ +
个月前的日志
+
+ +
+ +
+
+ +
+
+ +
\ No newline at end of file diff --git a/public/static/admin/js/system/log.js b/public/static/admin/js/system/log.js index 7c24b6e..6c5776a 100644 --- a/public/static/admin/js/system/log.js +++ b/public/static/admin/js/system/log.js @@ -6,6 +6,7 @@ define(["jquery", "easy-admin"], function ($, ea) { table_render_id: 'currentTableRenderId', index_url: 'system.log/index', export_url: 'system.log/export', + deleteMonthLog_url: 'system.log/deleteMonthLog', }; return { @@ -23,8 +24,15 @@ define(["jquery", "easy-admin"], function ($, ea) { class: 'layui-btn layui-btn-sm', icon: 'fa fa-book', extend: 'data-width="95%" data-height="95%"' - }, - ] + }, { + text: '删除部分日志', + url: 'system.log/deleteMonthLog', + method: 'open', + auth: 'record', + class: 'layui-btn layui-btn-sm layui-btn-danger', + icon: 'fa fa-remove', + extend: 'data-width="35%" data-height="42%"' + },] ], cols: [[ {field: 'id', width: 80, title: 'ID', search: false}, @@ -65,5 +73,20 @@ define(["jquery", "easy-admin"], function ($, ea) { }); ea.listen(); }, + deleteMonthLog: function () { + layui.form.on('submit(submit)', function (data) { + let field = data.field + let options = { + url: ea.url(init.deleteMonthLog_url), + data: field, + } + ea.msg.confirm('确认执行该操作?重要数据请先做好相关备份!', function () { + ea.request.post(options, function (rs) { + let msg = rs.msg || '未知~' + layer.msg(msg.replace(/\n/g, '
'), {shade: 0.3, shadeClose: true, time: 2000}) + }) + }) + }) + } }; });