- 新增日志管理模块:操作日志、登录日志、数据变更日志 - 新增敏感词过滤器 (sanitizer) - 新增日志异步写入管理器 - 新增日志定时清理服务 - 优化帖子相关服务和上传服务
87 lines
3.7 KiB
Go
87 lines
3.7 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// OperationType 操作类型
|
|
type OperationType string
|
|
|
|
const (
|
|
OpPostCreate OperationType = "post:create"
|
|
OpPostUpdate OperationType = "post:update"
|
|
OpPostDelete OperationType = "post:delete"
|
|
OpCommentCreate OperationType = "comment:create"
|
|
OpCommentDelete OperationType = "comment:delete"
|
|
OpUserRegister OperationType = "user:register"
|
|
OpUserLogin OperationType = "user:login"
|
|
OpUserLogout OperationType = "user:logout"
|
|
OpPasswordChange OperationType = "password:change"
|
|
OpPasswordReset OperationType = "password:reset"
|
|
OpProfileUpdate OperationType = "profile:update"
|
|
OpAvatarUpload OperationType = "avatar:upload"
|
|
OpEmailBind OperationType = "email:bind"
|
|
OpPhoneBind OperationType = "phone:bind"
|
|
OpAccountDelete OperationType = "account:delete"
|
|
OpDataExport OperationType = "data:export"
|
|
OpDataWithdraw OperationType = "data:withdraw"
|
|
OpAdminBanUser OperationType = "admin:ban_user"
|
|
OpAdminUnbanUser OperationType = "admin:unban_user"
|
|
OpAdminDeletePost OperationType = "admin:delete_post"
|
|
OpAdminModifyRole OperationType = "admin:modify_role"
|
|
OpAdminMuteUser OperationType = "admin:mute_user"
|
|
OpAdminUnmuteUser OperationType = "admin:unmute_user"
|
|
)
|
|
|
|
// OperationLog 操作日志实体
|
|
type OperationLog struct {
|
|
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
|
TraceID string `json:"trace_id" gorm:"type:varchar(64);index:idx_trace"`
|
|
UserID string `json:"user_id" gorm:"type:varchar(36);index:idx_user"`
|
|
UserName string `json:"user_name" gorm:"type:varchar(100)"`
|
|
NickName string `json:"nick_name" gorm:"type:varchar(100)"`
|
|
Operation string `json:"operation" gorm:"type:varchar(50);index:idx_operation"`
|
|
Module string `json:"module" gorm:"type:varchar(50)"`
|
|
TargetType string `json:"target_type" gorm:"type:varchar(50)"`
|
|
TargetID string `json:"target_id" gorm:"type:varchar(255);index:idx_target"`
|
|
Method string `json:"method" gorm:"type:varchar(10)"`
|
|
Path string `json:"path" gorm:"type:varchar(500)"`
|
|
QueryParams string `json:"query_params" gorm:"type:text"`
|
|
RequestBody string `json:"request_body" gorm:"type:text"`
|
|
ResponseCode int `json:"response_code" gorm:"index:idx_status"`
|
|
ResponseMsg string `json:"response_msg" gorm:"type:varchar(500)"`
|
|
IP string `json:"ip" gorm:"type:varchar(45);index:idx_ip"`
|
|
IPLocation string `json:"ip_location" gorm:"type:varchar(100)"`
|
|
UserAgent string `json:"user_agent" gorm:"type:varchar(500)"`
|
|
DeviceID string `json:"device_id" gorm:"type:varchar(100)"`
|
|
DeviceType string `json:"device_type" gorm:"type:varchar(20)"`
|
|
Duration int64 `json:"duration" gorm:"type:bigint"`
|
|
Referer string `json:"referer" gorm:"type:varchar(500)"`
|
|
Protocol string `json:"protocol" gorm:"type:varchar(10)"`
|
|
ServerIP string `json:"server_ip" gorm:"type:varchar(45)"`
|
|
ServerPort int `json:"server_port" gorm:"type:int"`
|
|
ClientPort int `json:"client_port" gorm:"type:int"`
|
|
OccurredAt time.Time `json:"occurred_at" gorm:"index:idx_created"`
|
|
Status string `json:"status" gorm:"type:varchar(20);index:idx_status"`
|
|
ErrorMsg string `json:"error_msg" gorm:"type:text"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"index:idx_created"`
|
|
}
|
|
|
|
// BeforeCreate 创建前生成TraceID
|
|
func (ol *OperationLog) BeforeCreate(tx *gorm.DB) error {
|
|
if ol.TraceID == "" {
|
|
ol.TraceID = uuid.New().String()
|
|
}
|
|
if ol.OccurredAt.IsZero() {
|
|
ol.OccurredAt = time.Now()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (OperationLog) TableName() string {
|
|
return "operation_logs"
|
|
}
|