69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package wevent
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// 和前端交流的事件机制
|
|
type EventType string
|
|
|
|
const (
|
|
UserEmailRegister EventType = "E_USER_EMAIL_REGISTER" // 用户注册
|
|
UserResetToken EventType = "E_USER_RESET_TOKEN" // 用户重置密码成功通知
|
|
)
|
|
|
|
// WebsocketEvent 所有事件都必须继承这个结构体
|
|
type WebsocketEvent struct {
|
|
Type EventType `json:"event_type"` // 事件
|
|
SenderTime time.Time `json:"sender_time"` // 发送的时间, 可能用来统计时间
|
|
TraceId string `json:"trace_id"` // 链路ID
|
|
Code int `json:"code"` // 状态码
|
|
Message string `json:"msg"` // 描述
|
|
Data any `json:"data"` // 关注的数据
|
|
}
|
|
|
|
// NewWebsocketEvent 创建一个Websocket事件
|
|
func NewWebsocketEvent(etype EventType, TraceId string) *WebsocketEvent {
|
|
return &WebsocketEvent{
|
|
Type: etype,
|
|
SenderTime: time.Now().UTC(),
|
|
TraceId: TraceId,
|
|
}
|
|
}
|
|
|
|
// NewWebsocketEventSuccess 创建一个Websocket事件伴随Code(200)
|
|
func NewWebsocketEventSuccess(etype EventType, TraceId string) *WebsocketEvent {
|
|
return &WebsocketEvent{
|
|
Type: etype,
|
|
SenderTime: time.Now().UTC(),
|
|
TraceId: TraceId,
|
|
Code: 200,
|
|
Message: "success",
|
|
}
|
|
}
|
|
|
|
func (event *WebsocketEvent) WithData(data any) *WebsocketEvent {
|
|
event.Data = data
|
|
return event
|
|
}
|
|
|
|
func (event *WebsocketEvent) WithMessgae(msg string) *WebsocketEvent {
|
|
event.Message = msg
|
|
return event
|
|
}
|
|
|
|
func (event *WebsocketEvent) WithCode(code int) *WebsocketEvent {
|
|
event.Code = code
|
|
return event
|
|
}
|
|
|
|
func (event *WebsocketEvent) WithTraceId(traceId string) *WebsocketEvent {
|
|
event.TraceId = traceId
|
|
return event
|
|
}
|
|
|
|
func (event *WebsocketEvent) WithSenderTime(senderTime time.Time) *WebsocketEvent {
|
|
event.SenderTime = senderTime
|
|
return event
|
|
}
|