JSON转换放在内置函数里

This commit is contained in:
2025-09-28 00:37:20 +08:00
parent 1c7e13b965
commit 3c8b91ff6a
2 changed files with 40 additions and 23 deletions

View File

@@ -2,14 +2,12 @@
package audit package audit
import ( import (
"encoding/json"
"time" "time"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs" "git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" "git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/datatypes"
) )
const ( const (
@@ -49,29 +47,23 @@ func (s *service) LogAction(c *gin.Context, actionType, description string, targ
return return
} }
// 将 targetResource 转换为 JSONB如果失败则记录错误但继续 log := &models.UserActionLog{
var targetResourceJSON datatypes.JSON Time: time.Now(),
if targetResource != nil { UserID: user.ID,
bytes, err := json.Marshal(targetResource) Username: user.Username, // 用户名快照
if err != nil { SourceIP: c.ClientIP(),
s.logger.Errorw("无法记录审计日志:序列化 targetResource 失败", "error", err) ActionType: actionType,
} else { Description: description,
targetResourceJSON = bytes Status: status,
} HTTPPath: c.Request.URL.Path,
HTTPMethod: c.Request.Method,
ResultDetails: resultDetails,
} }
log := &models.UserActionLog{ // 使用模型提供的方法来设置 TargetResource
Time: time.Now(), if err := log.SetTargetResource(targetResource); err != nil {
UserID: user.ID, s.logger.Errorw("无法记录审计日志:序列化 targetResource 失败", "error", err)
Username: user.Username, // 用户名快照 // 即使序列化失败,我们可能仍然希望记录操作本身,所以不在此处 return
SourceIP: c.ClientIP(),
ActionType: actionType,
TargetResource: targetResourceJSON,
Description: description,
Status: status,
HTTPPath: c.Request.URL.Path,
HTTPMethod: c.Request.Method,
ResultDetails: resultDetails,
} }
// 异步写入数据库,不阻塞当前请求 // 异步写入数据库,不阻塞当前请求

View File

@@ -1,6 +1,8 @@
package models package models
import ( import (
"encoding/json"
"errors"
"time" "time"
"gorm.io/datatypes" "gorm.io/datatypes"
@@ -170,3 +172,26 @@ type UserActionLog struct {
func (UserActionLog) TableName() string { func (UserActionLog) TableName() string {
return "user_action_logs" return "user_action_logs"
} }
// ParseTargetResource 解析 JSON 属性到一个具体的结构体中。
// 调用方需要传入一个指向目标结构体实例的指针。
func (l *UserActionLog) ParseTargetResource(v interface{}) error {
if l.TargetResource == nil {
return errors.New("目标资源为空,无法解析")
}
return json.Unmarshal(l.TargetResource, v)
}
// SetTargetResource 将任意结构体序列化为 JSON 并设置到 TargetResource 字段
func (l *UserActionLog) SetTargetResource(data interface{}) error {
if data == nil {
l.TargetResource = nil
return nil
}
bytes, err := json.Marshal(data)
if err != nil {
return err
}
l.TargetResource = bytes
return nil
}