2025-06-30 19:21:56 +08:00
|
|
|
package events
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"tyapi-server/internal/domains/user/entities"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// UserEventType 用户事件类型
|
|
|
|
|
type UserEventType string
|
|
|
|
|
|
|
|
|
|
const (
|
2025-07-02 16:17:59 +08:00
|
|
|
UserRegisteredEvent UserEventType = "user.registered"
|
2025-06-30 19:21:56 +08:00
|
|
|
UserLoggedInEvent UserEventType = "user.logged_in"
|
|
|
|
|
UserPasswordChangedEvent UserEventType = "user.password_changed"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// BaseUserEvent 用户事件基础结构
|
|
|
|
|
type BaseUserEvent struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
Timestamp time.Time `json:"timestamp"`
|
|
|
|
|
Source string `json:"source"`
|
|
|
|
|
AggregateID string `json:"aggregate_id"`
|
|
|
|
|
AggregateType string `json:"aggregate_type"`
|
|
|
|
|
Metadata map[string]interface{} `json:"metadata"`
|
|
|
|
|
Payload interface{} `json:"payload"`
|
|
|
|
|
|
|
|
|
|
// DDD特有字段
|
|
|
|
|
DomainVersion string `json:"domain_version"`
|
|
|
|
|
CausationID string `json:"causation_id"`
|
|
|
|
|
CorrelationID string `json:"correlation_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现 Event 接口
|
|
|
|
|
func (e *BaseUserEvent) GetID() string {
|
|
|
|
|
return e.ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetType() string {
|
|
|
|
|
return e.Type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetVersion() string {
|
|
|
|
|
return e.Version
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetTimestamp() time.Time {
|
|
|
|
|
return e.Timestamp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetPayload() interface{} {
|
|
|
|
|
return e.Payload
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetMetadata() map[string]interface{} {
|
|
|
|
|
return e.Metadata
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetSource() string {
|
|
|
|
|
return e.Source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetAggregateID() string {
|
|
|
|
|
return e.AggregateID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetAggregateType() string {
|
|
|
|
|
return e.AggregateType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetDomainVersion() string {
|
|
|
|
|
return e.DomainVersion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetCausationID() string {
|
|
|
|
|
return e.CausationID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) GetCorrelationID() string {
|
|
|
|
|
return e.CorrelationID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) Marshal() ([]byte, error) {
|
|
|
|
|
return json.Marshal(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *BaseUserEvent) Unmarshal(data []byte) error {
|
|
|
|
|
return json.Unmarshal(data, e)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:17:59 +08:00
|
|
|
// UserRegistered 用户注册事件
|
|
|
|
|
type UserRegistered struct {
|
2025-06-30 19:21:56 +08:00
|
|
|
*BaseUserEvent
|
|
|
|
|
User *entities.User `json:"user"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:17:59 +08:00
|
|
|
func NewUserRegisteredEvent(user *entities.User, correlationID string) *UserRegistered {
|
|
|
|
|
return &UserRegistered{
|
2025-06-30 19:21:56 +08:00
|
|
|
BaseUserEvent: &BaseUserEvent{
|
|
|
|
|
ID: uuid.New().String(),
|
2025-07-02 16:17:59 +08:00
|
|
|
Type: string(UserRegisteredEvent),
|
2025-06-30 19:21:56 +08:00
|
|
|
Version: "1.0",
|
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
|
Source: "user-service",
|
|
|
|
|
AggregateID: user.ID,
|
|
|
|
|
AggregateType: "User",
|
|
|
|
|
DomainVersion: "1.0",
|
|
|
|
|
CorrelationID: correlationID,
|
|
|
|
|
Metadata: map[string]interface{}{
|
2025-07-02 16:17:59 +08:00
|
|
|
"user_id": user.ID,
|
|
|
|
|
"phone": user.Phone,
|
2025-06-30 19:21:56 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
User: user,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:17:59 +08:00
|
|
|
func (e *UserRegistered) GetPayload() interface{} {
|
2025-06-30 19:21:56 +08:00
|
|
|
return e.User
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UserLoggedIn 用户登录事件
|
|
|
|
|
type UserLoggedIn struct {
|
|
|
|
|
*BaseUserEvent
|
|
|
|
|
UserID string `json:"user_id"`
|
2025-07-02 16:17:59 +08:00
|
|
|
Phone string `json:"phone"`
|
2025-06-30 19:21:56 +08:00
|
|
|
IPAddress string `json:"ip_address"`
|
|
|
|
|
UserAgent string `json:"user_agent"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:17:59 +08:00
|
|
|
func NewUserLoggedInEvent(userID, phone, ipAddress, userAgent, correlationID string) *UserLoggedIn {
|
2025-06-30 19:21:56 +08:00
|
|
|
return &UserLoggedIn{
|
|
|
|
|
BaseUserEvent: &BaseUserEvent{
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
Type: string(UserLoggedInEvent),
|
|
|
|
|
Version: "1.0",
|
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
|
Source: "user-service",
|
|
|
|
|
AggregateID: userID,
|
|
|
|
|
AggregateType: "User",
|
|
|
|
|
DomainVersion: "1.0",
|
|
|
|
|
CorrelationID: correlationID,
|
|
|
|
|
Metadata: map[string]interface{}{
|
|
|
|
|
"user_id": userID,
|
2025-07-02 16:17:59 +08:00
|
|
|
"phone": phone,
|
2025-06-30 19:21:56 +08:00
|
|
|
"ip_address": ipAddress,
|
|
|
|
|
"user_agent": userAgent,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
UserID: userID,
|
2025-07-02 16:17:59 +08:00
|
|
|
Phone: phone,
|
2025-06-30 19:21:56 +08:00
|
|
|
IPAddress: ipAddress,
|
|
|
|
|
UserAgent: userAgent,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UserPasswordChanged 用户密码修改事件
|
|
|
|
|
type UserPasswordChanged struct {
|
|
|
|
|
*BaseUserEvent
|
2025-07-02 16:17:59 +08:00
|
|
|
UserID string `json:"user_id"`
|
|
|
|
|
Phone string `json:"phone"`
|
2025-06-30 19:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:17:59 +08:00
|
|
|
func NewUserPasswordChangedEvent(userID, phone, correlationID string) *UserPasswordChanged {
|
2025-06-30 19:21:56 +08:00
|
|
|
return &UserPasswordChanged{
|
|
|
|
|
BaseUserEvent: &BaseUserEvent{
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
Type: string(UserPasswordChangedEvent),
|
|
|
|
|
Version: "1.0",
|
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
|
Source: "user-service",
|
|
|
|
|
AggregateID: userID,
|
|
|
|
|
AggregateType: "User",
|
|
|
|
|
DomainVersion: "1.0",
|
|
|
|
|
CorrelationID: correlationID,
|
|
|
|
|
Metadata: map[string]interface{}{
|
2025-07-02 16:17:59 +08:00
|
|
|
"user_id": userID,
|
|
|
|
|
"phone": phone,
|
2025-06-30 19:21:56 +08:00
|
|
|
},
|
|
|
|
|
},
|
2025-07-02 16:17:59 +08:00
|
|
|
UserID: userID,
|
|
|
|
|
Phone: phone,
|
2025-06-30 19:21:56 +08:00
|
|
|
}
|
|
|
|
|
}
|