迁移
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/service"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func idsFromQuery(c *gin.Context) []uint {
|
||||
values := c.QueryArray("IDs[]")
|
||||
if len(values) == 0 {
|
||||
values = c.QueryArray("ids[]")
|
||||
}
|
||||
ids := make([]uint, 0, len(values))
|
||||
for _, v := range values {
|
||||
id, _ := strconv.ParseUint(v, 10, 64)
|
||||
if id > 0 {
|
||||
ids = append(ids, uint(id))
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
func registerAuditRoutes(group, public *gin.RouterGroup, svc *service.AuditService) {
|
||||
operations := group.Group("/sysOperationRecord")
|
||||
operations.GET("/getSysOperationRecordList", func(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||
status, _ := strconv.Atoi(c.Query("status"))
|
||||
userID, _ := strconv.ParseUint(c.Query("user_id"), 10, 64)
|
||||
items, total, err := svc.Operations(c.Request.Context(), page, size, &biz.OperationRecord{Path: c.Query("path"), Method: c.Query("method"), Status: status, IP: c.Query("ip"), UserID: uint(userID)})
|
||||
if err != nil {
|
||||
fail(c, "获取失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
|
||||
})
|
||||
operations.GET("/findSysOperationRecord", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
item, err := svc.Operation(c.Request.Context(), uint(id))
|
||||
if err != nil {
|
||||
fail(c, "查询失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, gin.H{"resysOperationRecord": item}, "查询成功")
|
||||
})
|
||||
operations.DELETE("/deleteSysOperationRecord", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
if err := svc.DeleteOperations(c.Request.Context(), []uint{uint(id)}); err != nil {
|
||||
fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
operations.DELETE("/deleteSysOperationRecordByIds", func(c *gin.Context) {
|
||||
if err := svc.DeleteOperations(c.Request.Context(), idsFromQuery(c)); err != nil {
|
||||
fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
logins := group.Group("/sysLoginLog")
|
||||
logins.GET("/getLoginLogList", func(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||
items, total, err := svc.Logins(c.Request.Context(), page, size, &biz.LoginLog{Username: c.Query("username"), IP: c.Query("ip")})
|
||||
if err != nil {
|
||||
fail(c, "获取失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
|
||||
})
|
||||
logins.GET("/findLoginLog", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
item, err := svc.Login(c.Request.Context(), uint(id))
|
||||
if err != nil {
|
||||
fail(c, "查询失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, item, "查询成功")
|
||||
})
|
||||
logins.DELETE("/deleteLoginLog", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
if err := svc.DeleteLogins(c.Request.Context(), []uint{uint(id)}); err != nil {
|
||||
fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
logins.DELETE("/deleteLoginLogByIds", func(c *gin.Context) {
|
||||
if err := svc.DeleteLogins(c.Request.Context(), idsFromQuery(c)); err != nil {
|
||||
fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
access := group.Group("/dataAccessLog")
|
||||
access.POST("/getDataAccessLogList", func(c *gin.Context) {
|
||||
var req struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
EventType string `json:"eventType"`
|
||||
TargetTable string `json:"targetTable"`
|
||||
UserID uint `json:"userId"`
|
||||
}
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
items, total, err := svc.DataAccess(c.Request.Context(), req.Page, req.PageSize, &biz.DataAccessLog{EventType: req.EventType, TargetTable: req.TargetTable, UserID: req.UserID})
|
||||
if err != nil {
|
||||
fail(c, "获取失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
||||
})
|
||||
access.DELETE("/deleteDataAccessLogByIds", func(c *gin.Context) {
|
||||
var req struct {
|
||||
IDs []uint `json:"ids"`
|
||||
}
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := svc.DeleteDataAccess(c.Request.Context(), req.IDs); err != nil {
|
||||
fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
viewer := group.Group("/logViewer")
|
||||
viewer.GET("/dates", func(c *gin.Context) {
|
||||
data, err := svc.LogDates(c.Request.Context(), c.Query("month"))
|
||||
if err != nil {
|
||||
fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, data, "获取成功")
|
||||
})
|
||||
viewer.GET("/files", func(c *gin.Context) {
|
||||
data, err := svc.LogFiles(c.Request.Context(), c.Query("date"))
|
||||
if err != nil {
|
||||
fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, data, "获取成功")
|
||||
})
|
||||
viewer.GET("/content", func(c *gin.Context) {
|
||||
var cursor *int64
|
||||
if raw := c.Query("cursor"); raw != "" {
|
||||
value, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || value < 0 {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
cursor = &value
|
||||
}
|
||||
data, err := svc.LogContent(c.Request.Context(), c.Query("date"), c.Query("path"), cursor)
|
||||
if err != nil {
|
||||
fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, data, "获取成功")
|
||||
})
|
||||
errorsRoute := group.Group("/sysError")
|
||||
errorsRoute.DELETE("/deleteSysError", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
if err := svc.DeleteErrors(c.Request.Context(), []uint{uint(id)}); err != nil {
|
||||
fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
errorsRoute.DELETE("/deleteSysErrorByIds", func(c *gin.Context) {
|
||||
if err := svc.DeleteErrors(c.Request.Context(), idsFromQuery(c)); err != nil {
|
||||
fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
errorsRoute.PUT("/updateSysError", func(c *gin.Context) {
|
||||
var req struct {
|
||||
ID uint `json:"ID"`
|
||||
Form string `json:"form"`
|
||||
Info string `json:"info"`
|
||||
Level string `json:"level"`
|
||||
Solution string `json:"solution"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := svc.UpdateError(c.Request.Context(), &biz.ErrorRecord{ID: req.ID, Form: req.Form, Info: req.Info, Level: req.Level, Solution: req.Solution, Status: req.Status}); err != nil {
|
||||
fail(c, "更新失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
errorsRoute.GET("/findSysError", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
item, err := svc.Error(c.Request.Context(), uint(id))
|
||||
if err != nil {
|
||||
fail(c, "查询失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, gin.H{"resysError": item}, "查询成功")
|
||||
})
|
||||
errorsRoute.GET("/getSysErrorList", func(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||
items, total, err := svc.Errors(c.Request.Context(), page, size, &biz.ErrorRecord{Form: c.Query("form"), Level: c.Query("level"), Status: c.Query("status")})
|
||||
if err != nil {
|
||||
fail(c, "获取失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: page, PageSize: size}, "获取成功")
|
||||
})
|
||||
errorsRoute.GET("/getSysErrorSolution", func(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
||||
item, err := svc.Error(c.Request.Context(), uint(id))
|
||||
if err != nil {
|
||||
fail(c, "查询失败")
|
||||
return
|
||||
}
|
||||
writeResult(c, codeSuccess, gin.H{"solution": item["solution"]}, "获取成功")
|
||||
})
|
||||
public.Group("/sysError").POST("/createSysError", func(c *gin.Context) {
|
||||
var req struct {
|
||||
Form string `json:"form"`
|
||||
Info string `json:"info"`
|
||||
Level string `json:"level"`
|
||||
RequestID string `json:"request_id"`
|
||||
TraceID string `json:"trace_id"`
|
||||
}
|
||||
if c.ShouldBindJSON(&req) != nil || req.Form == "" {
|
||||
fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := svc.CreateError(c.Request.Context(), &biz.ErrorRecord{Form: req.Form, Info: req.Info, Level: req.Level, RequestID: req.RequestID, TraceID: req.TraceID}); err != nil {
|
||||
fail(c, "创建失败")
|
||||
return
|
||||
}
|
||||
ok(c)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user