1Panel/backend/app/api/v1/logs.go

92 lines
2.7 KiB
Go
Raw Normal View History

2022-08-16 23:30:23 +08:00
package v1
import (
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
2023-01-04 22:31:51 +08:00
"github.com/1Panel-dev/1Panel/backend/global"
2022-08-16 23:30:23 +08:00
"github.com/gin-gonic/gin"
)
2023-01-04 22:31:51 +08:00
// @Tags Logs
// @Summary Page login logs
// @Description 获取系统登录日志列表分页
// @Accept json
// @Param request body dto.PageInfo true "request"
2023-01-04 22:31:51 +08:00
// @Success 200 {object} dto.PageResult
// @Security ApiKeyAuth
// @Router /logs/login [post]
2022-11-15 17:20:57 +08:00
func (b *BaseApi) GetLoginLogs(c *gin.Context) {
2022-08-16 23:30:23 +08:00
var req dto.PageInfo
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
2022-11-15 17:20:57 +08:00
total, list, err := logService.PageLoginLog(req)
2022-08-16 23:30:23 +08:00
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, dto.PageResult{
Items: list,
Total: total,
})
}
2023-01-04 22:31:51 +08:00
// @Tags Logs
// @Summary Page operation logs
// @Description 获取系统操作日志列表分页
// @Accept json
// @Param request body dto.PageInfo true "request"
2023-01-04 22:31:51 +08:00
// @Success 200 {object} dto.PageResult
// @Security ApiKeyAuth
// @Router /logs/operation [post]
2022-11-15 17:20:57 +08:00
func (b *BaseApi) GetOperationLogs(c *gin.Context) {
var req dto.PageInfo
2022-08-16 23:30:23 +08:00
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
2022-11-15 17:20:57 +08:00
total, list, err := logService.PageOperationLog(req)
if err != nil {
2022-08-16 23:30:23 +08:00
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
2022-11-15 17:20:57 +08:00
helper.SuccessWithData(c, dto.PageResult{
Items: list,
Total: total,
})
2022-08-16 23:30:23 +08:00
}
2022-11-15 18:44:50 +08:00
2023-01-04 22:31:51 +08:00
// @Tags Logs
// @Summary Clean operation logs
// @Description 清空操作日志
// @Accept json
// @Param request body dto.CleanLog true "request"
// @Success 200 {object} dto.PageResult
// @Security ApiKeyAuth
// @Router /logs/clean [post]
// @x-panel-log {"bodyKeys":["logType"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"清空 [logType] 日志信息","formatEN":"Clean the [logType] log information"}
2022-11-15 18:44:50 +08:00
func (b *BaseApi) CleanLogs(c *gin.Context) {
2023-01-04 22:31:51 +08:00
var req dto.CleanLog
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := global.VALID.Struct(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
2022-11-15 18:44:50 +08:00
return
}
2023-01-04 22:31:51 +08:00
if err := logService.CleanLogs(req.LogType); err != nil {
2022-11-15 18:44:50 +08:00
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}