From e825dd7bb40ea2a1bc5bd31827a196a6db5ceb4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E5=AE=AB=E4=B9=98=E9=A3=8E?= <46562911+nangongchengfeng@users.noreply.github.com> Date: Thu, 16 May 2024 22:12:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=A4=B4=E5=92=8C=E6=97=A5=E5=BF=97=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20(#342)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 增加操作日志请求头查询和清空日志功能 * fix: r 变量未使用 * 增加数据库初始化数据 * fix: 移除r变量未使用 --- controller/operation_log_controller.go | 8 ++++++++ logic/operation_log_logic.go | 15 ++++++++++++++- model/request/operation_log_req.go | 1 + public/common/init_mysql_data.go | 7 +++++++ routes/operation_log_routes.go | 1 + service/isql/operation_log_isql.go | 13 +++++++++++-- 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/controller/operation_log_controller.go b/controller/operation_log_controller.go index 8856043..652f03b 100644 --- a/controller/operation_log_controller.go +++ b/controller/operation_log_controller.go @@ -24,3 +24,11 @@ func (m *OperationLogController) Delete(c *gin.Context) { return logic.OperationLog.Delete(c, req) }) } + +// Clean 清空记录 +func (m *OperationLogController) Clean(c *gin.Context) { + req := new(request.OperationLogListReq) + Run(c, req, func() (interface{}, interface{}) { + return logic.OperationLog.Clean(c, req) + }) +} diff --git a/logic/operation_log_logic.go b/logic/operation_log_logic.go index bac22ce..b59da37 100644 --- a/logic/operation_log_logic.go +++ b/logic/operation_log_logic.go @@ -21,7 +21,7 @@ func (l OperationLogLogic) List(c *gin.Context, req interface{}) (data interface return nil, ReqAssertErr } _ = c - + fmt.Println(r) // 获取数据列表 logs, err := isql.OperationLog.List(r) if err != nil { @@ -72,3 +72,16 @@ func (l OperationLogLogic) Delete(c *gin.Context, req interface{}) (data interfa } return nil, nil } + +func (l OperationLogLogic) Clean(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) { + _, ok := req.(*request.OperationLogListReq) + if !ok { + return nil, ReqAssertErr + } + _ = c + err := isql.OperationLog.Clean() + if err != nil { + return err, nil + } + return "操作日志情况完成", nil +} diff --git a/model/request/operation_log_req.go b/model/request/operation_log_req.go index ebf23d1..3487010 100644 --- a/model/request/operation_log_req.go +++ b/model/request/operation_log_req.go @@ -5,6 +5,7 @@ type OperationLogListReq struct { Username string `json:"username" form:"username"` Ip string `json:"ip" form:"ip"` Path string `json:"path" form:"path"` + Method string `json:"method" form:"method"` Status int `json:"status" form:"status"` PageNum int `json:"pageNum" form:"pageNum"` PageSize int `json:"pageSize" form:"pageSize"` diff --git a/public/common/init_mysql_data.go b/public/common/init_mysql_data.go index 01754c5..cb59bcd 100644 --- a/public/common/init_mysql_data.go +++ b/public/common/init_mysql_data.go @@ -649,6 +649,13 @@ func InitData() { Remark: "批量删除操作日志", Creator: "系统", }, + { + Method: "DELETE", + Path: "/log/operation/clean", + Category: "log", + Remark: "清空操作日志", + Creator: "系统", + }, } // 5. 将角色绑定给菜单 diff --git a/routes/operation_log_routes.go b/routes/operation_log_routes.go index 372e543..266f89a 100644 --- a/routes/operation_log_routes.go +++ b/routes/operation_log_routes.go @@ -17,6 +17,7 @@ func InitOperationLogRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle { operation_log.GET("/operation/list", controller.OperationLog.List) operation_log.POST("/operation/delete", controller.OperationLog.Delete) + operation_log.DELETE("/operation/clean", controller.OperationLog.Clean) } return r } diff --git a/service/isql/operation_log_isql.go b/service/isql/operation_log_isql.go index f1aff85..739b206 100644 --- a/service/isql/operation_log_isql.go +++ b/service/isql/operation_log_isql.go @@ -16,8 +16,8 @@ import ( type OperationLogService struct{} -//var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个 -//处理OperationLogChan将日志记录到数据库 +// var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个 +// 处理OperationLogChan将日志记录到数据库 func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.OperationLog) { // 只会在线程开启的时候执行一次 Logs := make([]model.OperationLog, 0) @@ -62,6 +62,10 @@ func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.Op if path != "" { db = db.Where("path LIKE ?", fmt.Sprintf("%%%s%%", path)) } + method := strings.TrimSpace(req.Method) + if method != "" { + db = db.Where("method LIKE ?", fmt.Sprintf("%%%s%%", method)) + } status := req.Status if status != 0 { db = db.Where("status = ?", status) @@ -95,3 +99,8 @@ func (s OperationLogService) Exist(filter map[string]interface{}) bool { func (s OperationLogService) Delete(operationLogIds []uint) error { return common.DB.Where("id IN (?)", operationLogIds).Unscoped().Delete(&model.OperationLog{}).Error } + +// Clean 清空日志 +func (s OperationLogService) Clean() error { + return common.DB.Exec("TRUNCATE TABLE operation_logs").Error +}