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 @@
+