This commit introduces several architectural improvements and optimizations across the codebase: - **Performance & Reliability**: - Implemented Redis pipelining in `ConversationCache.CacheMessage` to reduce network round-trips. - Added a circuit breaker to the JPush client to prevent cascading failures. - Introduced batch deletion and batch member addition capabilities in repositories. - Added message idempotency support using `client_msg_id` and a Redis-based cache. - Optimized WebSocket handling with connection limits (total and per-user) and improved error logging. - **Code Refactoring**: - Refactored `Router` to use a `RouterDeps` struct, simplifying the constructor and improving maintainability. - Unified model ID generation logic using new `id_helper.go` (supporting UUID and Snowflake). - Standardized JSON serialization/deserialization in models using `json_helper.go`. - Refactored DTO conversion logic, specifically for `UserResponse` (using functional options) and `Report` responses. - Removed redundant/deprecated DTOs like `PostDetailResponse` and `TradeItemDetailResponse`. - **Cache Improvements**: - Enhanced `LayeredCache` with `SetRaw` to avoid double-encoding when promoting values from Redis to local cache. - Added `DeleteBatch` support to the cache interface. - **Other Changes**: - Cleaned up `config.go` by removing redundant default values and explicit environment variable overrides. - Improved WebSocket registration flow to handle connection limits gracefully.
84 lines
3.6 KiB
Go
84 lines
3.6 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
|
|
"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 {
|
|
SetUUIDIfEmpty(&ol.TraceID)
|
|
if ol.OccurredAt.IsZero() {
|
|
ol.OccurredAt = time.Now()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (OperationLog) TableName() string {
|
|
return "operation_logs"
|
|
}
|