增加日志请求头和日志清空功能 (#342)
* feat: 增加操作日志请求头查询和清空日志功能 * fix: r 变量未使用 * 增加数据库初始化数据 * fix: 移除r变量未使用
This commit is contained in:
parent
e2c2ea6bfd
commit
e825dd7bb4
|
@ -24,3 +24,11 @@ func (m *OperationLogController) Delete(c *gin.Context) {
|
||||||
return logic.OperationLog.Delete(c, req)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func (l OperationLogLogic) List(c *gin.Context, req interface{}) (data interface
|
||||||
return nil, ReqAssertErr
|
return nil, ReqAssertErr
|
||||||
}
|
}
|
||||||
_ = c
|
_ = c
|
||||||
|
fmt.Println(r)
|
||||||
// 获取数据列表
|
// 获取数据列表
|
||||||
logs, err := isql.OperationLog.List(r)
|
logs, err := isql.OperationLog.List(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -72,3 +72,16 @@ func (l OperationLogLogic) Delete(c *gin.Context, req interface{}) (data interfa
|
||||||
}
|
}
|
||||||
return nil, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ type OperationLogListReq struct {
|
||||||
Username string `json:"username" form:"username"`
|
Username string `json:"username" form:"username"`
|
||||||
Ip string `json:"ip" form:"ip"`
|
Ip string `json:"ip" form:"ip"`
|
||||||
Path string `json:"path" form:"path"`
|
Path string `json:"path" form:"path"`
|
||||||
|
Method string `json:"method" form:"method"`
|
||||||
Status int `json:"status" form:"status"`
|
Status int `json:"status" form:"status"`
|
||||||
PageNum int `json:"pageNum" form:"pageNum"`
|
PageNum int `json:"pageNum" form:"pageNum"`
|
||||||
PageSize int `json:"pageSize" form:"pageSize"`
|
PageSize int `json:"pageSize" form:"pageSize"`
|
||||||
|
|
|
@ -649,6 +649,13 @@ func InitData() {
|
||||||
Remark: "批量删除操作日志",
|
Remark: "批量删除操作日志",
|
||||||
Creator: "系统",
|
Creator: "系统",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Method: "DELETE",
|
||||||
|
Path: "/log/operation/clean",
|
||||||
|
Category: "log",
|
||||||
|
Remark: "清空操作日志",
|
||||||
|
Creator: "系统",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 将角色绑定给菜单
|
// 5. 将角色绑定给菜单
|
||||||
|
|
|
@ -17,6 +17,7 @@ func InitOperationLogRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
|
||||||
{
|
{
|
||||||
operation_log.GET("/operation/list", controller.OperationLog.List)
|
operation_log.GET("/operation/list", controller.OperationLog.List)
|
||||||
operation_log.POST("/operation/delete", controller.OperationLog.Delete)
|
operation_log.POST("/operation/delete", controller.OperationLog.Delete)
|
||||||
|
operation_log.DELETE("/operation/clean", controller.OperationLog.Clean)
|
||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ import (
|
||||||
|
|
||||||
type OperationLogService struct{}
|
type OperationLogService struct{}
|
||||||
|
|
||||||
//var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个
|
// var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个
|
||||||
//处理OperationLogChan将日志记录到数据库
|
// 处理OperationLogChan将日志记录到数据库
|
||||||
func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.OperationLog) {
|
func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.OperationLog) {
|
||||||
// 只会在线程开启的时候执行一次
|
// 只会在线程开启的时候执行一次
|
||||||
Logs := make([]model.OperationLog, 0)
|
Logs := make([]model.OperationLog, 0)
|
||||||
|
@ -62,6 +62,10 @@ func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.Op
|
||||||
if path != "" {
|
if path != "" {
|
||||||
db = db.Where("path LIKE ?", fmt.Sprintf("%%%s%%", 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
|
status := req.Status
|
||||||
if status != 0 {
|
if status != 0 {
|
||||||
db = db.Where("status = ?", status)
|
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 {
|
func (s OperationLogService) Delete(operationLogIds []uint) error {
|
||||||
return common.DB.Where("id IN (?)", operationLogIds).Unscoped().Delete(&model.OperationLog{}).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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue