增加日志请求头和日志清空功能 (#342)

* feat: 增加操作日志请求头查询和清空日志功能

* fix: r 变量未使用

* 增加数据库初始化数据

* fix: 移除r变量未使用
This commit is contained in:
南宫乘风 2024-05-16 22:12:42 +08:00 committed by GitHub
parent e2c2ea6bfd
commit e825dd7bb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 42 additions and 3 deletions

View File

@ -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)
})
}

View File

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

View File

@ -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"`

View File

@ -649,6 +649,13 @@ func InitData() {
Remark: "批量删除操作日志",
Creator: "系统",
},
{
Method: "DELETE",
Path: "/log/operation/clean",
Category: "log",
Remark: "清空操作日志",
Creator: "系统",
},
}
// 5. 将角色绑定给菜单

View File

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

View File

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