temp
This commit is contained in:
526
internal/domains/certification/events/certification_events.go
Normal file
526
internal/domains/certification/events/certification_events.go
Normal file
@@ -0,0 +1,526 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"tyapi-server/internal/domains/certification/entities"
|
||||
)
|
||||
|
||||
// 认证事件类型常量
|
||||
const (
|
||||
EventTypeCertificationCreated = "certification.created"
|
||||
EventTypeCertificationSubmitted = "certification.submitted"
|
||||
EventTypeLicenseUploaded = "certification.license.uploaded"
|
||||
EventTypeOCRCompleted = "certification.ocr.completed"
|
||||
EventTypeEnterpriseInfoConfirmed = "certification.enterprise.confirmed"
|
||||
EventTypeFaceVerifyInitiated = "certification.face_verify.initiated"
|
||||
EventTypeFaceVerifyCompleted = "certification.face_verify.completed"
|
||||
EventTypeContractRequested = "certification.contract.requested"
|
||||
EventTypeContractGenerated = "certification.contract.generated"
|
||||
EventTypeContractSigned = "certification.contract.signed"
|
||||
EventTypeCertificationApproved = "certification.approved"
|
||||
EventTypeCertificationRejected = "certification.rejected"
|
||||
EventTypeWalletCreated = "certification.wallet.created"
|
||||
EventTypeCertificationCompleted = "certification.completed"
|
||||
EventTypeCertificationFailed = "certification.failed"
|
||||
)
|
||||
|
||||
// BaseCertificationEvent 认证事件基础结构
|
||||
type BaseCertificationEvent 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"`
|
||||
}
|
||||
|
||||
// 实现 Event 接口
|
||||
func (e *BaseCertificationEvent) GetID() string { return e.ID }
|
||||
func (e *BaseCertificationEvent) GetType() string { return e.Type }
|
||||
func (e *BaseCertificationEvent) GetVersion() string { return e.Version }
|
||||
func (e *BaseCertificationEvent) GetTimestamp() time.Time { return e.Timestamp }
|
||||
func (e *BaseCertificationEvent) GetSource() string { return e.Source }
|
||||
func (e *BaseCertificationEvent) GetAggregateID() string { return e.AggregateID }
|
||||
func (e *BaseCertificationEvent) GetAggregateType() string { return e.AggregateType }
|
||||
func (e *BaseCertificationEvent) GetPayload() interface{} { return e.Payload }
|
||||
func (e *BaseCertificationEvent) GetMetadata() map[string]interface{} { return e.Metadata }
|
||||
func (e *BaseCertificationEvent) Marshal() ([]byte, error) { return json.Marshal(e) }
|
||||
func (e *BaseCertificationEvent) Unmarshal(data []byte) error { return json.Unmarshal(data, e) }
|
||||
func (e *BaseCertificationEvent) GetDomainVersion() string { return e.Version }
|
||||
func (e *BaseCertificationEvent) GetCausationID() string { return e.ID }
|
||||
func (e *BaseCertificationEvent) GetCorrelationID() string { return e.ID }
|
||||
|
||||
// NewBaseCertificationEvent 创建基础认证事件
|
||||
func NewBaseCertificationEvent(eventType, aggregateID string, payload interface{}) *BaseCertificationEvent {
|
||||
return &BaseCertificationEvent{
|
||||
ID: generateEventID(),
|
||||
Type: eventType,
|
||||
Version: "1.0",
|
||||
Timestamp: time.Now(),
|
||||
Source: "certification-domain",
|
||||
AggregateID: aggregateID,
|
||||
AggregateType: "certification",
|
||||
Metadata: make(map[string]interface{}),
|
||||
Payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
// CertificationCreatedEvent 认证创建事件
|
||||
type CertificationCreatedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewCertificationCreatedEvent 创建认证创建事件
|
||||
func NewCertificationCreatedEvent(certification *entities.Certification) *CertificationCreatedEvent {
|
||||
event := &CertificationCreatedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeCertificationCreated,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// CertificationSubmittedEvent 认证提交事件
|
||||
type CertificationSubmittedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewCertificationSubmittedEvent 创建认证提交事件
|
||||
func NewCertificationSubmittedEvent(certification *entities.Certification) *CertificationSubmittedEvent {
|
||||
event := &CertificationSubmittedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeCertificationSubmitted,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// LicenseUploadedEvent 营业执照上传事件
|
||||
type LicenseUploadedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
FileURL string `json:"file_url"`
|
||||
FileName string `json:"file_name"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewLicenseUploadedEvent 创建营业执照上传事件
|
||||
func NewLicenseUploadedEvent(certification *entities.Certification, record *entities.LicenseUploadRecord) *LicenseUploadedEvent {
|
||||
event := &LicenseUploadedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeLicenseUploaded,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.FileURL = record.FileURL
|
||||
event.Data.FileName = record.OriginalFileName
|
||||
event.Data.FileSize = record.FileSize
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// OCRCompletedEvent OCR识别完成事件
|
||||
type OCRCompletedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
OCRResult map[string]interface{} `json:"ocr_result"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewOCRCompletedEvent 创建OCR识别完成事件
|
||||
func NewOCRCompletedEvent(certification *entities.Certification, ocrResult map[string]interface{}, confidence float64) *OCRCompletedEvent {
|
||||
event := &OCRCompletedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeOCRCompleted,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.OCRResult = ocrResult
|
||||
event.Data.Confidence = confidence
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// EnterpriseInfoConfirmedEvent 企业信息确认事件
|
||||
type EnterpriseInfoConfirmedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
EnterpriseInfo map[string]interface{} `json:"enterprise_info"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewEnterpriseInfoConfirmedEvent 创建企业信息确认事件
|
||||
func NewEnterpriseInfoConfirmedEvent(certification *entities.Certification, enterpriseInfo map[string]interface{}) *EnterpriseInfoConfirmedEvent {
|
||||
event := &EnterpriseInfoConfirmedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeEnterpriseInfoConfirmed,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.EnterpriseInfo = enterpriseInfo
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// FaceVerifyInitiatedEvent 人脸识别初始化事件
|
||||
type FaceVerifyInitiatedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
VerifyToken string `json:"verify_token"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewFaceVerifyInitiatedEvent 创建人脸识别初始化事件
|
||||
func NewFaceVerifyInitiatedEvent(certification *entities.Certification, verifyToken string) *FaceVerifyInitiatedEvent {
|
||||
event := &FaceVerifyInitiatedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeFaceVerifyInitiated,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.VerifyToken = verifyToken
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// FaceVerifyCompletedEvent 人脸识别完成事件
|
||||
type FaceVerifyCompletedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
VerifyToken string `json:"verify_token"`
|
||||
Success bool `json:"success"`
|
||||
Score float64 `json:"score"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewFaceVerifyCompletedEvent 创建人脸识别完成事件
|
||||
func NewFaceVerifyCompletedEvent(certification *entities.Certification, record *entities.FaceVerifyRecord) *FaceVerifyCompletedEvent {
|
||||
event := &FaceVerifyCompletedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeFaceVerifyCompleted,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.VerifyToken = record.CertifyID
|
||||
event.Data.Success = record.IsSuccess()
|
||||
event.Data.Score = record.VerifyScore
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// ContractRequestedEvent 合同申请事件
|
||||
type ContractRequestedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewContractRequestedEvent 创建合同申请事件
|
||||
func NewContractRequestedEvent(certification *entities.Certification) *ContractRequestedEvent {
|
||||
event := &ContractRequestedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeContractRequested,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// ContractGeneratedEvent 合同生成事件
|
||||
type ContractGeneratedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
ContractURL string `json:"contract_url"`
|
||||
ContractID string `json:"contract_id"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewContractGeneratedEvent 创建合同生成事件
|
||||
func NewContractGeneratedEvent(certification *entities.Certification, record *entities.ContractRecord) *ContractGeneratedEvent {
|
||||
event := &ContractGeneratedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeContractGenerated,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.ContractURL = record.ContractURL
|
||||
event.Data.ContractID = record.ID
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// ContractSignedEvent 合同签署事件
|
||||
type ContractSignedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
ContractID string `json:"contract_id"`
|
||||
SignedAt string `json:"signed_at"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewContractSignedEvent 创建合同签署事件
|
||||
func NewContractSignedEvent(certification *entities.Certification, record *entities.ContractRecord) *ContractSignedEvent {
|
||||
event := &ContractSignedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeContractSigned,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.ContractID = record.ID
|
||||
event.Data.SignedAt = record.SignedAt.Format(time.RFC3339)
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// CertificationApprovedEvent 认证审核通过事件
|
||||
type CertificationApprovedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
AdminID string `json:"admin_id"`
|
||||
ApprovedAt string `json:"approved_at"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewCertificationApprovedEvent 创建认证审核通过事件
|
||||
func NewCertificationApprovedEvent(certification *entities.Certification, adminID string) *CertificationApprovedEvent {
|
||||
event := &CertificationApprovedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeCertificationApproved,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.AdminID = adminID
|
||||
event.Data.ApprovedAt = time.Now().Format(time.RFC3339)
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// CertificationRejectedEvent 认证审核拒绝事件
|
||||
type CertificationRejectedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
AdminID string `json:"admin_id"`
|
||||
RejectReason string `json:"reject_reason"`
|
||||
RejectedAt string `json:"rejected_at"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewCertificationRejectedEvent 创建认证审核拒绝事件
|
||||
func NewCertificationRejectedEvent(certification *entities.Certification, adminID, rejectReason string) *CertificationRejectedEvent {
|
||||
event := &CertificationRejectedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeCertificationRejected,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.AdminID = adminID
|
||||
event.Data.RejectReason = rejectReason
|
||||
event.Data.RejectedAt = time.Now().Format(time.RFC3339)
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// WalletCreatedEvent 钱包创建事件
|
||||
type WalletCreatedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
WalletID string `json:"wallet_id"`
|
||||
AccessID string `json:"access_id"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewWalletCreatedEvent 创建钱包创建事件
|
||||
func NewWalletCreatedEvent(certification *entities.Certification, walletID, accessID string) *WalletCreatedEvent {
|
||||
event := &WalletCreatedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeWalletCreated,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.WalletID = walletID
|
||||
event.Data.AccessID = accessID
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// CertificationCompletedEvent 认证完成事件
|
||||
type CertificationCompletedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
WalletID string `json:"wallet_id"`
|
||||
CompletedAt string `json:"completed_at"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewCertificationCompletedEvent 创建认证完成事件
|
||||
func NewCertificationCompletedEvent(certification *entities.Certification, walletID string) *CertificationCompletedEvent {
|
||||
event := &CertificationCompletedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeCertificationCompleted,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.WalletID = walletID
|
||||
event.Data.CompletedAt = time.Now().Format(time.RFC3339)
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// CertificationFailedEvent 认证失败事件
|
||||
type CertificationFailedEvent struct {
|
||||
*BaseCertificationEvent
|
||||
Data struct {
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
FailedAt string `json:"failed_at"`
|
||||
FailureReason string `json:"failure_reason"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// NewCertificationFailedEvent 创建认证失败事件
|
||||
func NewCertificationFailedEvent(certification *entities.Certification, failureReason string) *CertificationFailedEvent {
|
||||
event := &CertificationFailedEvent{
|
||||
BaseCertificationEvent: NewBaseCertificationEvent(
|
||||
EventTypeCertificationFailed,
|
||||
certification.ID,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
event.Data.CertificationID = certification.ID
|
||||
event.Data.UserID = certification.UserID
|
||||
event.Data.FailedAt = time.Now().Format(time.RFC3339)
|
||||
event.Data.FailureReason = failureReason
|
||||
event.Data.Status = string(certification.Status)
|
||||
event.Payload = event.Data
|
||||
return event
|
||||
}
|
||||
|
||||
// generateEventID 生成事件ID
|
||||
func generateEventID() string {
|
||||
return time.Now().Format("20060102150405") + "-" + generateRandomString(8)
|
||||
}
|
||||
|
||||
// generateRandomString 生成随机字符串
|
||||
func generateRandomString(length int) string {
|
||||
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
b[i] = charset[time.Now().UnixNano()%int64(len(charset))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
489
internal/domains/certification/events/event_handlers.go
Normal file
489
internal/domains/certification/events/event_handlers.go
Normal file
@@ -0,0 +1,489 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/shared/interfaces"
|
||||
"tyapi-server/internal/shared/notification"
|
||||
)
|
||||
|
||||
// CertificationEventHandler 认证事件处理器
|
||||
type CertificationEventHandler struct {
|
||||
logger *zap.Logger
|
||||
notification notification.WeChatWorkService
|
||||
name string
|
||||
eventTypes []string
|
||||
isAsync bool
|
||||
}
|
||||
|
||||
// NewCertificationEventHandler 创建认证事件处理器
|
||||
func NewCertificationEventHandler(logger *zap.Logger, notification notification.WeChatWorkService) *CertificationEventHandler {
|
||||
return &CertificationEventHandler{
|
||||
logger: logger,
|
||||
notification: notification,
|
||||
name: "certification-event-handler",
|
||||
eventTypes: []string{
|
||||
EventTypeCertificationCreated,
|
||||
EventTypeCertificationSubmitted,
|
||||
EventTypeLicenseUploaded,
|
||||
EventTypeOCRCompleted,
|
||||
EventTypeEnterpriseInfoConfirmed,
|
||||
EventTypeFaceVerifyInitiated,
|
||||
EventTypeFaceVerifyCompleted,
|
||||
EventTypeContractRequested,
|
||||
EventTypeContractGenerated,
|
||||
EventTypeContractSigned,
|
||||
EventTypeCertificationApproved,
|
||||
EventTypeCertificationRejected,
|
||||
EventTypeWalletCreated,
|
||||
EventTypeCertificationCompleted,
|
||||
EventTypeCertificationFailed,
|
||||
},
|
||||
isAsync: true,
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取处理器名称
|
||||
func (h *CertificationEventHandler) GetName() string {
|
||||
return h.name
|
||||
}
|
||||
|
||||
// GetEventTypes 获取支持的事件类型
|
||||
func (h *CertificationEventHandler) GetEventTypes() []string {
|
||||
return h.eventTypes
|
||||
}
|
||||
|
||||
// IsAsync 是否为异步处理器
|
||||
func (h *CertificationEventHandler) IsAsync() bool {
|
||||
return h.isAsync
|
||||
}
|
||||
|
||||
// GetRetryConfig 获取重试配置
|
||||
func (h *CertificationEventHandler) GetRetryConfig() interfaces.RetryConfig {
|
||||
return interfaces.RetryConfig{
|
||||
MaxRetries: 3,
|
||||
RetryDelay: 5 * time.Second,
|
||||
BackoffFactor: 2.0,
|
||||
MaxDelay: 30 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (h *CertificationEventHandler) Handle(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("处理认证事件",
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
zap.String("aggregate_id", event.GetAggregateID()),
|
||||
)
|
||||
|
||||
switch event.GetType() {
|
||||
case EventTypeCertificationCreated:
|
||||
return h.handleCertificationCreated(ctx, event)
|
||||
case EventTypeCertificationSubmitted:
|
||||
return h.handleCertificationSubmitted(ctx, event)
|
||||
case EventTypeLicenseUploaded:
|
||||
return h.handleLicenseUploaded(ctx, event)
|
||||
case EventTypeOCRCompleted:
|
||||
return h.handleOCRCompleted(ctx, event)
|
||||
case EventTypeEnterpriseInfoConfirmed:
|
||||
return h.handleEnterpriseInfoConfirmed(ctx, event)
|
||||
case EventTypeFaceVerifyInitiated:
|
||||
return h.handleFaceVerifyInitiated(ctx, event)
|
||||
case EventTypeFaceVerifyCompleted:
|
||||
return h.handleFaceVerifyCompleted(ctx, event)
|
||||
case EventTypeContractRequested:
|
||||
return h.handleContractRequested(ctx, event)
|
||||
case EventTypeContractGenerated:
|
||||
return h.handleContractGenerated(ctx, event)
|
||||
case EventTypeContractSigned:
|
||||
return h.handleContractSigned(ctx, event)
|
||||
case EventTypeCertificationApproved:
|
||||
return h.handleCertificationApproved(ctx, event)
|
||||
case EventTypeCertificationRejected:
|
||||
return h.handleCertificationRejected(ctx, event)
|
||||
case EventTypeWalletCreated:
|
||||
return h.handleWalletCreated(ctx, event)
|
||||
case EventTypeCertificationCompleted:
|
||||
return h.handleCertificationCompleted(ctx, event)
|
||||
case EventTypeCertificationFailed:
|
||||
return h.handleCertificationFailed(ctx, event)
|
||||
default:
|
||||
h.logger.Warn("未知的事件类型", zap.String("event_type", event.GetType()))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// handleCertificationCreated 处理认证创建事件
|
||||
func (h *CertificationEventHandler) handleCertificationCreated(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("认证申请已创建",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("🎉 您的企业认证申请已创建成功!\n\n认证ID: %s\n创建时间: %s\n\n请按照指引完成后续认证步骤。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "认证申请创建成功", message)
|
||||
}
|
||||
|
||||
// handleCertificationSubmitted 处理认证提交事件
|
||||
func (h *CertificationEventHandler) handleCertificationSubmitted(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("认证申请已提交",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给管理员
|
||||
adminMessage := fmt.Sprintf("📋 新的企业认证申请待审核\n\n认证ID: %s\n用户ID: %s\n提交时间: %s\n\n请及时处理审核。",
|
||||
event.GetAggregateID(),
|
||||
h.extractUserID(event),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendAdminNotification(ctx, event, "新认证申请待审核", adminMessage)
|
||||
}
|
||||
|
||||
// handleLicenseUploaded 处理营业执照上传事件
|
||||
func (h *CertificationEventHandler) handleLicenseUploaded(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("营业执照已上传",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("📄 营业执照上传成功!\n\n认证ID: %s\n上传时间: %s\n\n系统正在识别营业执照信息,请稍候...",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "营业执照上传成功", message)
|
||||
}
|
||||
|
||||
// handleOCRCompleted 处理OCR识别完成事件
|
||||
func (h *CertificationEventHandler) handleOCRCompleted(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("OCR识别已完成",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("✅ OCR识别完成!\n\n认证ID: %s\n识别时间: %s\n\n请确认企业信息是否正确,如有问题请及时联系客服。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "OCR识别完成", message)
|
||||
}
|
||||
|
||||
// handleEnterpriseInfoConfirmed 处理企业信息确认事件
|
||||
func (h *CertificationEventHandler) handleEnterpriseInfoConfirmed(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("企业信息已确认",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("✅ 企业信息确认成功!\n\n认证ID: %s\n确认时间: %s\n\n下一步:请完成人脸识别验证。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "企业信息确认成功", message)
|
||||
}
|
||||
|
||||
// handleFaceVerifyInitiated 处理人脸识别初始化事件
|
||||
func (h *CertificationEventHandler) handleFaceVerifyInitiated(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("人脸识别已初始化",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("👤 人脸识别验证已开始!\n\n认证ID: %s\n开始时间: %s\n\n请按照指引完成人脸识别验证。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "人脸识别验证开始", message)
|
||||
}
|
||||
|
||||
// handleFaceVerifyCompleted 处理人脸识别完成事件
|
||||
func (h *CertificationEventHandler) handleFaceVerifyCompleted(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("人脸识别已完成",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("✅ 人脸识别验证完成!\n\n认证ID: %s\n完成时间: %s\n\n下一步:系统将为您申请电子合同。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "人脸识别验证完成", message)
|
||||
}
|
||||
|
||||
// handleContractRequested 处理合同申请事件
|
||||
func (h *CertificationEventHandler) handleContractRequested(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("电子合同申请已提交",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给管理员
|
||||
adminMessage := fmt.Sprintf("📋 新的电子合同申请待审核\n\n认证ID: %s\n用户ID: %s\n申请时间: %s\n\n请及时处理合同审核。",
|
||||
event.GetAggregateID(),
|
||||
h.extractUserID(event),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendAdminNotification(ctx, event, "新合同申请待审核", adminMessage)
|
||||
}
|
||||
|
||||
// handleContractGenerated 处理合同生成事件
|
||||
func (h *CertificationEventHandler) handleContractGenerated(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("电子合同已生成",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("📄 电子合同已生成!\n\n认证ID: %s\n生成时间: %s\n\n请及时签署电子合同以完成认证流程。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "电子合同已生成", message)
|
||||
}
|
||||
|
||||
// handleContractSigned 处理合同签署事件
|
||||
func (h *CertificationEventHandler) handleContractSigned(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("电子合同已签署",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("✅ 电子合同签署成功!\n\n认证ID: %s\n签署时间: %s\n\n您的企业认证申请已进入最终审核阶段。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "电子合同签署成功", message)
|
||||
}
|
||||
|
||||
// handleCertificationApproved 处理认证审核通过事件
|
||||
func (h *CertificationEventHandler) handleCertificationApproved(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("认证申请已审核通过",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("🎉 恭喜!您的企业认证申请已审核通过!\n\n认证ID: %s\n审核时间: %s\n\n系统正在为您创建钱包和访问密钥...",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "认证申请审核通过", message)
|
||||
}
|
||||
|
||||
// handleCertificationRejected 处理认证审核拒绝事件
|
||||
func (h *CertificationEventHandler) handleCertificationRejected(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("认证申请已被拒绝",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("❌ 很抱歉,您的企业认证申请未通过审核\n\n认证ID: %s\n拒绝时间: %s\n\n请根据拒绝原因修改后重新提交申请。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "认证申请审核未通过", message)
|
||||
}
|
||||
|
||||
// handleWalletCreated 处理钱包创建事件
|
||||
func (h *CertificationEventHandler) handleWalletCreated(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("钱包已创建",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("💰 钱包创建成功!\n\n认证ID: %s\n创建时间: %s\n\n您的企业钱包已激活,可以开始使用相关服务。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "钱包创建成功", message)
|
||||
}
|
||||
|
||||
// handleCertificationCompleted 处理认证完成事件
|
||||
func (h *CertificationEventHandler) handleCertificationCompleted(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("企业认证已完成",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("🎉 恭喜!您的企业认证已全部完成!\n\n认证ID: %s\n完成时间: %s\n\n您现在可以享受完整的企业级服务功能。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "企业认证完成", message)
|
||||
}
|
||||
|
||||
// handleCertificationFailed 处理认证失败事件
|
||||
func (h *CertificationEventHandler) handleCertificationFailed(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Error("企业认证失败",
|
||||
zap.String("certification_id", event.GetAggregateID()),
|
||||
zap.String("user_id", h.extractUserID(event)),
|
||||
)
|
||||
|
||||
// 发送通知给用户
|
||||
message := fmt.Sprintf("❌ 企业认证流程遇到问题\n\n认证ID: %s\n失败时间: %s\n\n请联系客服获取帮助。",
|
||||
event.GetAggregateID(),
|
||||
event.GetTimestamp().Format("2006-01-02 15:04:05"))
|
||||
|
||||
return h.sendUserNotification(ctx, event, "企业认证失败", message)
|
||||
}
|
||||
|
||||
// sendUserNotification 发送用户通知
|
||||
func (h *CertificationEventHandler) sendUserNotification(ctx context.Context, event interfaces.Event, title, message string) error {
|
||||
url := fmt.Sprintf("https://example.com/certification/%s", event.GetAggregateID())
|
||||
btnText := "查看详情"
|
||||
if err := h.notification.SendCardMessage(ctx, title, message, url, btnText); err != nil {
|
||||
h.logger.Error("发送用户通知失败",
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
zap.Error(err),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
h.logger.Info("用户通知发送成功",
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendAdminNotification 发送管理员通知
|
||||
func (h *CertificationEventHandler) sendAdminNotification(ctx context.Context, event interfaces.Event, title, message string) error {
|
||||
url := fmt.Sprintf("https://admin.example.com/certification/%s", event.GetAggregateID())
|
||||
btnText := "立即处理"
|
||||
if err := h.notification.SendCardMessage(ctx, title, message, url, btnText); err != nil {
|
||||
h.logger.Error("发送管理员通知失败",
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
zap.Error(err),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
h.logger.Info("管理员通知发送成功",
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// extractUserID 从事件中提取用户ID
|
||||
func (h *CertificationEventHandler) extractUserID(event interfaces.Event) string {
|
||||
if payload, ok := event.GetPayload().(map[string]interface{}); ok {
|
||||
if userID, exists := payload["user_id"]; exists {
|
||||
if id, ok := userID.(string); ok {
|
||||
return id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试从事件数据中提取
|
||||
if eventData, ok := event.(*BaseCertificationEvent); ok {
|
||||
if data, ok := eventData.Payload.(map[string]interface{}); ok {
|
||||
if userID, exists := data["user_id"]; exists {
|
||||
if id, ok := userID.(string); ok {
|
||||
return id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// LoggingEventHandler 日志记录事件处理器
|
||||
type LoggingEventHandler struct {
|
||||
logger *zap.Logger
|
||||
name string
|
||||
eventTypes []string
|
||||
isAsync bool
|
||||
}
|
||||
|
||||
// NewLoggingEventHandler 创建日志记录事件处理器
|
||||
func NewLoggingEventHandler(logger *zap.Logger) *LoggingEventHandler {
|
||||
return &LoggingEventHandler{
|
||||
logger: logger,
|
||||
name: "logging-event-handler",
|
||||
eventTypes: []string{
|
||||
EventTypeCertificationCreated,
|
||||
EventTypeCertificationSubmitted,
|
||||
EventTypeLicenseUploaded,
|
||||
EventTypeOCRCompleted,
|
||||
EventTypeEnterpriseInfoConfirmed,
|
||||
EventTypeFaceVerifyInitiated,
|
||||
EventTypeFaceVerifyCompleted,
|
||||
EventTypeContractRequested,
|
||||
EventTypeContractGenerated,
|
||||
EventTypeContractSigned,
|
||||
EventTypeCertificationApproved,
|
||||
EventTypeCertificationRejected,
|
||||
EventTypeWalletCreated,
|
||||
EventTypeCertificationCompleted,
|
||||
EventTypeCertificationFailed,
|
||||
},
|
||||
isAsync: false, // 同步处理,确保日志及时记录
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取处理器名称
|
||||
func (l *LoggingEventHandler) GetName() string {
|
||||
return l.name
|
||||
}
|
||||
|
||||
// GetEventTypes 获取支持的事件类型
|
||||
func (l *LoggingEventHandler) GetEventTypes() []string {
|
||||
return l.eventTypes
|
||||
}
|
||||
|
||||
// IsAsync 是否为异步处理器
|
||||
func (l *LoggingEventHandler) IsAsync() bool {
|
||||
return l.isAsync
|
||||
}
|
||||
|
||||
// GetRetryConfig 获取重试配置
|
||||
func (l *LoggingEventHandler) GetRetryConfig() interfaces.RetryConfig {
|
||||
return interfaces.RetryConfig{
|
||||
MaxRetries: 1,
|
||||
RetryDelay: 1 * time.Second,
|
||||
BackoffFactor: 1.0,
|
||||
MaxDelay: 1 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (l *LoggingEventHandler) Handle(ctx context.Context, event interfaces.Event) error {
|
||||
// 记录结构化日志
|
||||
eventData, _ := json.Marshal(event.GetPayload())
|
||||
|
||||
l.logger.Info("认证事件记录",
|
||||
zap.String("event_id", event.GetID()),
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("aggregate_id", event.GetAggregateID()),
|
||||
zap.String("aggregate_type", event.GetAggregateType()),
|
||||
zap.Time("timestamp", event.GetTimestamp()),
|
||||
zap.String("source", event.GetSource()),
|
||||
zap.String("payload", string(eventData)),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user