This commit is contained in:
2025-07-20 20:53:26 +08:00
parent 83bf9aea7d
commit 8ad1d7288e
158 changed files with 18156 additions and 13188 deletions

View File

@@ -5,25 +5,18 @@ import (
"time"
"tyapi-server/internal/domains/certification/entities"
"github.com/google/uuid"
)
// 认证事件类型常量
// 事件类型常量
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"
EventTypeCertificationCreated = "certification.created"
EventTypeEnterpriseInfoSubmitted = "enterprise.info.submitted"
EventTypeEnterpriseVerified = "enterprise.verified"
EventTypeContractApplied = "contract.applied"
EventTypeContractSigned = "contract.signed"
EventTypeCertificationCompleted = "certification.completed"
)
// BaseCertificationEvent 认证事件基础结构
@@ -39,7 +32,7 @@ type BaseCertificationEvent struct {
Payload interface{} `json:"payload"`
}
// 实现 Event 接口
// 实现 DomainEvent 接口
func (e *BaseCertificationEvent) GetID() string { return e.ID }
func (e *BaseCertificationEvent) GetType() string { return e.Type }
func (e *BaseCertificationEvent) GetVersion() string { return e.Version }
@@ -62,15 +55,15 @@ func NewBaseCertificationEvent(eventType, aggregateID string, payload interface{
Type: eventType,
Version: "1.0",
Timestamp: time.Now(),
Source: "certification-domain",
Source: "certification-service",
AggregateID: aggregateID,
AggregateType: "certification",
AggregateType: "Certification",
Metadata: make(map[string]interface{}),
Payload: payload,
}
}
// CertificationCreatedEvent 认证创建事件
// CertificationCreatedEvent 认证申请创建事件
type CertificationCreatedEvent struct {
*BaseCertificationEvent
Data struct {
@@ -80,7 +73,7 @@ type CertificationCreatedEvent struct {
} `json:"data"`
}
// NewCertificationCreatedEvent 创建认证创建事件
// NewCertificationCreatedEvent 创建认证申请创建事件
func NewCertificationCreatedEvent(certification *entities.Certification) *CertificationCreatedEvent {
event := &CertificationCreatedEvent{
BaseCertificationEvent: NewBaseCertificationEvent(
@@ -96,8 +89,8 @@ func NewCertificationCreatedEvent(certification *entities.Certification) *Certif
return event
}
// CertificationSubmittedEvent 认证提交事件
type CertificationSubmittedEvent struct {
// EnterpriseInfoSubmittedEvent 企业信息提交事件
type EnterpriseInfoSubmittedEvent struct {
*BaseCertificationEvent
Data struct {
CertificationID string `json:"certification_id"`
@@ -106,11 +99,11 @@ type CertificationSubmittedEvent struct {
} `json:"data"`
}
// NewCertificationSubmittedEvent 创建认证提交事件
func NewCertificationSubmittedEvent(certification *entities.Certification) *CertificationSubmittedEvent {
event := &CertificationSubmittedEvent{
// NewEnterpriseInfoSubmittedEvent 创建企业信息提交事件
func NewEnterpriseInfoSubmittedEvent(certification *entities.Certification) *EnterpriseInfoSubmittedEvent {
event := &EnterpriseInfoSubmittedEvent{
BaseCertificationEvent: NewBaseCertificationEvent(
EventTypeCertificationSubmitted,
EventTypeEnterpriseInfoSubmitted,
certification.ID,
nil,
),
@@ -122,158 +115,8 @@ func NewCertificationSubmittedEvent(certification *entities.Certification) *Cert
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 {
// EnterpriseVerifiedEvent 企业认证完成事件
type EnterpriseVerifiedEvent struct {
*BaseCertificationEvent
Data struct {
CertificationID string `json:"certification_id"`
@@ -282,11 +125,11 @@ type ContractRequestedEvent struct {
} `json:"data"`
}
// NewContractRequestedEvent 创建合同申请事件
func NewContractRequestedEvent(certification *entities.Certification) *ContractRequestedEvent {
event := &ContractRequestedEvent{
// NewEnterpriseVerifiedEvent 创建企业认证完成事件
func NewEnterpriseVerifiedEvent(certification *entities.Certification) *EnterpriseVerifiedEvent {
event := &EnterpriseVerifiedEvent{
BaseCertificationEvent: NewBaseCertificationEvent(
EventTypeContractRequested,
EventTypeEnterpriseVerified,
certification.ID,
nil,
),
@@ -298,31 +141,27 @@ func NewContractRequestedEvent(certification *entities.Certification) *ContractR
return event
}
// ContractGeneratedEvent 合同生成事件
type ContractGeneratedEvent struct {
// ContractAppliedEvent 合同申请事件
type ContractAppliedEvent 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{
// NewContractAppliedEvent 创建合同申请事件
func NewContractAppliedEvent(certification *entities.Certification) *ContractAppliedEvent {
event := &ContractAppliedEvent{
BaseCertificationEvent: NewBaseCertificationEvent(
EventTypeContractGenerated,
EventTypeContractApplied,
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
@@ -334,14 +173,13 @@ type ContractSignedEvent struct {
Data struct {
CertificationID string `json:"certification_id"`
UserID string `json:"user_id"`
ContractID string `json:"contract_id"`
SignedAt string `json:"signed_at"`
ContractURL string `json:"contract_url"`
Status string `json:"status"`
} `json:"data"`
}
// NewContractSignedEvent 创建合同签署事件
func NewContractSignedEvent(certification *entities.Certification, record *entities.ContractRecord) *ContractSignedEvent {
func NewContractSignedEvent(certification *entities.Certification, contractURL string) *ContractSignedEvent {
event := &ContractSignedEvent{
BaseCertificationEvent: NewBaseCertificationEvent(
EventTypeContractSigned,
@@ -351,100 +189,7 @@ func NewContractSignedEvent(certification *entities.Certification, record *entit
}
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.ContractURL = contractURL
event.Data.Status = string(certification.Status)
event.Payload = event.Data
return event
@@ -456,14 +201,13 @@ type CertificationCompletedEvent struct {
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 {
func NewCertificationCompletedEvent(certification *entities.Certification) *CertificationCompletedEvent {
event := &CertificationCompletedEvent{
BaseCertificationEvent: NewBaseCertificationEvent(
EventTypeCertificationCompleted,
@@ -473,54 +217,13 @@ func NewCertificationCompletedEvent(certification *entities.Certification, walle
}
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)
return uuid.New().String()
}

View File

@@ -29,20 +29,11 @@ func NewCertificationEventHandler(logger *zap.Logger, notification notification.
name: "certification-event-handler",
eventTypes: []string{
EventTypeCertificationCreated,
EventTypeCertificationSubmitted,
EventTypeLicenseUploaded,
EventTypeOCRCompleted,
EventTypeEnterpriseInfoConfirmed,
EventTypeFaceVerifyInitiated,
EventTypeFaceVerifyCompleted,
EventTypeContractRequested,
EventTypeContractGenerated,
EventTypeEnterpriseInfoSubmitted,
EventTypeEnterpriseVerified,
EventTypeContractApplied,
EventTypeContractSigned,
EventTypeCertificationApproved,
EventTypeCertificationRejected,
EventTypeWalletCreated,
EventTypeCertificationCompleted,
EventTypeCertificationFailed,
},
isAsync: true,
}
@@ -84,34 +75,16 @@ func (h *CertificationEventHandler) Handle(ctx context.Context, event interfaces
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 EventTypeEnterpriseInfoSubmitted:
return h.handleEnterpriseInfoSubmitted(ctx, event)
case EventTypeEnterpriseVerified:
return h.handleEnterpriseVerified(ctx, event)
case EventTypeContractApplied:
return h.handleContractApplied(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
@@ -133,126 +106,49 @@ func (h *CertificationEventHandler) handleCertificationCreated(ctx context.Conte
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("营业执照已上传",
// handleEnterpriseInfoSubmitted 处理企业信息提交事件
func (h *CertificationEventHandler) handleEnterpriseInfoSubmitted(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系统正在识别营业执照信息,请稍候...",
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)
return h.sendUserNotification(ctx, event, "企业信息提交成功", message)
}
// handleOCRCompleted 处理OCR识别完成事件
func (h *CertificationEventHandler) handleOCRCompleted(ctx context.Context, event interfaces.Event) error {
h.logger.Info("OCR识别已完成",
// handleEnterpriseVerified 处理企业认证完成事件
func (h *CertificationEventHandler) handleEnterpriseVerified(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("✅ OCR识别完成!\n\n认证ID: %s\n识别时间: %s\n\n请确认企业信息是否正确,如有问题请及时联系客服。",
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, "OCR识别完成", message)
return h.sendUserNotification(ctx, event, "企业认证完成", 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 {
// handleContractApplied 处理合同申请事件
func (h *CertificationEventHandler) handleContractApplied(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请及时签署电子合同以完成认证流程。",
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)
return h.sendUserNotification(ctx, event, "合同申请已提交", message)
}
// handleContractSigned 处理合同签署事件
@@ -263,56 +159,11 @@ func (h *CertificationEventHandler) handleContractSigned(ctx context.Context, ev
)
// 发送通知给用户
message := fmt.Sprintf("✅ 电子合同签署成\n\n认证ID: %s\n签署时间: %s\n\n您的企业认证申请已进入最终审核阶段。",
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)
return h.sendUserNotification(ctx, event, "合同签署成", message)
}
// handleCertificationCompleted 处理认证完成事件
@@ -323,44 +174,26 @@ func (h *CertificationEventHandler) handleCertificationCompleted(ctx context.Con
)
// 发送通知给用户
message := fmt.Sprintf("🎉 恭喜!您的企业认证已全部完成!\n\n认证ID: %s\n完成时间: %s\n\n您现在可以享受完整的企业级服务功能。",
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
userID := h.extractUserID(event)
if userID == "" {
h.logger.Warn("无法提取用户ID跳过通知发送")
return nil
}
h.logger.Info("用户通知发送成功",
zap.String("event_type", event.GetType()),
zap.String("event_id", event.GetID()),
// 这里可以调用通知服务发送消息
h.logger.Info("发送用户通知",
zap.String("user_id", userID),
zap.String("title", title),
zap.String("message", message),
)
return nil
@@ -368,20 +201,10 @@ func (h *CertificationEventHandler) sendUserNotification(ctx context.Context, ev
// 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()),
// 这里可以调用通知服务发送管理员消息
h.logger.Info("发送管理员通知",
zap.String("title", title),
zap.String("message", message),
)
return nil
@@ -389,29 +212,58 @@ func (h *CertificationEventHandler) sendAdminNotification(ctx context.Context, e
// 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
payload := event.GetPayload()
if payload == nil {
return ""
}
// 尝试从payload中提取user_id
if data, ok := payload.(map[string]interface{}); ok {
if userID, exists := data["user_id"]; exists {
if str, ok := userID.(string); ok {
return str
}
}
}
// 尝试从事件数据中提取
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
// 尝试从JSON中解析
if data, ok := payload.(map[string]interface{}); ok {
if dataField, exists := data["data"]; exists {
if dataMap, ok := dataField.(map[string]interface{}); ok {
if userID, exists := dataMap["user_id"]; exists {
if str, ok := userID.(string); ok {
return str
}
}
}
}
}
return "unknown"
// 尝试从JSON字符串解析
if jsonData, err := json.Marshal(payload); err == nil {
var data map[string]interface{}
if err := json.Unmarshal(jsonData, &data); err == nil {
if userID, exists := data["user_id"]; exists {
if str, ok := userID.(string); ok {
return str
}
}
if dataField, exists := data["data"]; exists {
if dataMap, ok := dataField.(map[string]interface{}); ok {
if userID, exists := dataMap["user_id"]; exists {
if str, ok := userID.(string); ok {
return str
}
}
}
}
}
}
return ""
}
// LoggingEventHandler 日志记录事件处理器
// LoggingEventHandler 日志事件处理器
type LoggingEventHandler struct {
logger *zap.Logger
name string
@@ -419,29 +271,20 @@ type LoggingEventHandler struct {
isAsync bool
}
// NewLoggingEventHandler 创建日志记录事件处理器
// 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,
EventTypeEnterpriseInfoSubmitted,
EventTypeEnterpriseVerified,
EventTypeContractApplied,
EventTypeContractSigned,
EventTypeCertificationApproved,
EventTypeCertificationRejected,
EventTypeWalletCreated,
EventTypeCertificationCompleted,
EventTypeCertificationFailed,
},
isAsync: false, // 同步处理,确保日志及时记录
isAsync: false,
}
}
@@ -463,27 +306,21 @@ func (l *LoggingEventHandler) IsAsync() bool {
// GetRetryConfig 获取重试配置
func (l *LoggingEventHandler) GetRetryConfig() interfaces.RetryConfig {
return interfaces.RetryConfig{
MaxRetries: 1,
RetryDelay: 1 * time.Second,
MaxRetries: 0,
RetryDelay: 0,
BackoffFactor: 1.0,
MaxDelay: 1 * time.Second,
MaxDelay: 0,
}
}
// 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()),
l.logger.Info("认证事件日志",
zap.String("event_type", event.GetType()),
zap.String("event_id", event.GetID()),
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)),
zap.Any("payload", event.GetPayload()),
)
return nil
}