优化代码
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/audit"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -35,6 +36,20 @@ func AuditLogMiddleware(auditService audit.Service) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 从 Gin Context 中获取用户对象
|
||||
userCtx, userExists := c.Get(repository.ContextUserKey)
|
||||
var user *models.User
|
||||
if userExists {
|
||||
user, _ = userCtx.(*models.User)
|
||||
}
|
||||
|
||||
// 构建 RequestContext
|
||||
reqCtx := audit.RequestContext{
|
||||
ClientIP: c.ClientIP(),
|
||||
HTTPPath: c.Request.URL.Path,
|
||||
HTTPMethod: c.Request.Method,
|
||||
}
|
||||
|
||||
// 获取其他审计信息
|
||||
description, _ := c.Get(repository.ContextAuditDescription)
|
||||
targetResource, _ := c.Get(repository.ContextAuditTargetResource)
|
||||
@@ -63,7 +78,8 @@ func AuditLogMiddleware(auditService audit.Service) gin.HandlerFunc {
|
||||
|
||||
// 调用审计服务记录日志(异步)
|
||||
auditService.LogAction(
|
||||
c,
|
||||
user,
|
||||
reqCtx,
|
||||
actionType.(string),
|
||||
description.(string),
|
||||
targetResource,
|
||||
|
||||
Reference in New Issue
Block a user