package middleware import ( "github.com/zeromicro/go-zero/core/logx" "net/http" "tianyuan-api/apps/api/internal/service" ) type ApiMqsInterceptorMiddleware struct { ApiRequestMqsService *service.ApiRequestMqsService } func NewApiMqsInterceptorMiddleware(apiRequestMqsService *service.ApiRequestMqsService) *ApiMqsInterceptorMiddleware { return &ApiMqsInterceptorMiddleware{ ApiRequestMqsService: apiRequestMqsService, } } func (m *ApiMqsInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { next(w, r) //响应拦截 ctx := r.Context() transactionID, ok := ctx.Value("transactionID").(string) if ok && transactionID != "" { userId, userIdOk := ctx.Value("userId").(int64) if !userIdOk { logx.Error("userId 不存在或类型错误") return } productCode, productCodeOk := ctx.Value("productCode").(string) if !productCodeOk || productCode == "" { logx.Errorf("productCode 不存在或为空") } status, statusOk := ctx.Value("status").(string) if !statusOk || status == "" { status = "failed" } remark, remarkOk := ctx.Value("remark").(string) if !remarkOk || remark == "" { } charges, chargesOk := ctx.Value("charges").(bool) if !chargesOk || !charges { charges = false } err := m.ApiRequestMqsService.SendApiRequestMessage(ctx, transactionID, userId, productCode, status, charges, remark) if err != nil { logx.Errorf("发送 API 请求消息失败: %v", err) } } } }