mongo开发工具类增加增删改操作日志写入redis消息逻辑

This commit is contained in:
2025-12-30 18:18:56 +08:00
parent 5ded2b08a2
commit 8c4918e4ed
6 changed files with 106 additions and 138 deletions

View File

@@ -9,7 +9,6 @@ import (
logEntity "gitee.com/red-future---jilin-g/common/log/model/entity"
"gitee.com/red-future---jilin-g/common/utils"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
)
@@ -19,18 +18,18 @@ type operationLog struct{}
var OperationLog = &operationLog{}
// RecordCreate 记录创建操作
func (s *operationLog) RecordCreate(ctx context.Context, module, service, resource, resourceID, description string, afterData map[string]interface{}) error {
return s.record(ctx, module, service, string(consts.OperationCreate), resource, resourceID, description, nil, afterData, nil)
func (s *operationLog) RecordCreate(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationCreate), data)
}
// RecordUpdate 记录更新操作
func (s *operationLog) RecordUpdate(ctx context.Context, module, service, resource, resourceID, description string, beforeData, afterData map[string]interface{}) error {
return s.record(ctx, module, service, string(consts.OperationUpdate), resource, resourceID, description, beforeData, afterData, nil)
func (s *operationLog) RecordUpdate(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationUpdate), data)
}
// RecordDelete 记录删除操作
func (s *operationLog) RecordDelete(ctx context.Context, module, service, resource, resourceID, description string, beforeData map[string]interface{}) error {
return s.record(ctx, module, service, string(consts.OperationDelete), resource, resourceID, description, beforeData, nil, nil)
func (s *operationLog) RecordDelete(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationDelete), data)
}
// BatchRecordCreate 批量记录创建操作
@@ -46,22 +45,22 @@ func (s *operationLog) GetByID(ctx context.Context, id string) (*dto.OperationLo
}
var logInfo dto.OperationLogInfo
gconv.Struct(log, &logInfo)
if err := gconv.Struct(log, &logInfo); err != nil {
return nil, err
}
logInfo.ID = log.Id.Hex()
logInfo.CreatedAt = gtime.New(log.CreatedAt).Format("Y-m-d H:i:s")
logInfo.UpdatedAt = gtime.New(log.UpdatedAt).Format("Y-m-d H:i:s")
return &logInfo, nil
}
// List 查询操作日志列表
func (s *operationLog) List(ctx context.Context, filter interface{}, sortFields ...string) ([]dto.OperationLogInfo, int64, error) {
func (s *operationLog) List(ctx context.Context, filter *dto.ListLogsReq, sortFields ...string) ([]dto.OperationLogInfo, int64, error) {
logs, total, err := dao.Log.List(ctx, filter, sortFields...)
if err != nil {
return nil, 0, err
}
var logInfos []dto.OperationLogInfo
err = gconv.Slice(logs, &logInfos)
err = gconv.Structs(logs, &logInfos)
if err != nil {
return nil, 0, err
}
@@ -69,15 +68,13 @@ func (s *operationLog) List(ctx context.Context, filter interface{}, sortFields
// 处理特殊字段
for i, log := range logs {
logInfos[i].ID = log.Id.Hex()
logInfos[i].CreatedAt = gtime.New(log.CreatedAt).Format("Y-m-d H:i:s")
logInfos[i].UpdatedAt = gtime.New(log.UpdatedAt).Format("Y-m-d H:i:s")
}
return logInfos, total, nil
}
// record 记录操作日志的通用方法
func (s *operationLog) record(ctx context.Context, module, service, operation, resource, resourceID, description string, beforeData, afterData, extraData map[string]interface{}) error {
func (s *operationLog) record(ctx context.Context, serviceName, collection, collectionID, operation string, data map[string]interface{}) error {
// 获取用户信息
user, err := utils.GetUserInfo(ctx)
if err != nil {
@@ -85,7 +82,7 @@ func (s *operationLog) record(ctx context.Context, module, service, operation, r
}
// 获取请求信息
ipAddress, userAgent := getHTTPRequestInfo(ctx)
ipAddress := getHTTPRequestInfo(ctx)
var userName string
if user.UserName != nil {
@@ -93,29 +90,23 @@ func (s *operationLog) record(ctx context.Context, module, service, operation, r
}
log := &logEntity.OperationLog{
Module: module,
Service: service,
Operation: operation,
Resource: resource,
ResourceID: resourceID,
UserID: user.UserName,
UserName: userName,
IPAddress: ipAddress,
UserAgent: userAgent,
Description: description,
BeforeData: beforeData,
AfterData: afterData,
ExtraData: extraData,
ServiceName: serviceName,
Collection: collection,
CollectionID: collectionID,
Operation: operation,
UserName: userName,
IPAddress: ipAddress,
Data: data,
}
return dao.Log.Create(ctx, log)
}
// getHTTPRequestInfo 从上下文中获取HTTP请求信息
func getHTTPRequestInfo(ctx context.Context) (ipAddress, userAgent string) {
func getHTTPRequestInfo(ctx context.Context) string {
request := g.RequestFromCtx(ctx)
if request != nil {
return request.GetClientIp(), request.Header.Get("User-Agent")
return request.GetClientIp()
}
return "", ""
return ""
}