diff --git a/backend/internal/service/billing_service.go b/backend/internal/service/billing_service.go index bf196ac6..3fd05355 100644 --- a/backend/internal/service/billing_service.go +++ b/backend/internal/service/billing_service.go @@ -890,8 +890,9 @@ func (s *BillingService) CalculateImageCost(model string, imageSize string, imag actualCost := totalCost * rateMultiplier return &CostBreakdown{ - TotalCost: totalCost, - ActualCost: actualCost, + TotalCost: totalCost, + ActualCost: actualCost, + BillingMode: string(BillingModeImage), } } diff --git a/backend/internal/service/gateway_service.go b/backend/internal/service/gateway_service.go index d210ce21..7ae5cf54 100644 --- a/backend/internal/service/gateway_service.go +++ b/backend/internal/service/gateway_service.go @@ -7762,17 +7762,17 @@ func (s *GatewayService) RecordUsage(ctx context.Context, input *RecordUsageInpu } else if result.MediaType == "prompt" { cost = &CostBreakdown{} } else if result.ImageCount > 0 { - // 图片生成计费:渠道 token 定价优先,否则走按次计费(兼容旧版本) - useImageTokenBilling := false + // 图片生成计费:渠道级别定价优先,否则走按次计费(兼容旧版本) + hasChannelPricing := false if s.resolver != nil && apiKey.Group != nil { gid := apiKey.Group.ID resolved := s.resolver.Resolve(ctx, PricingInput{Model: billingModel, GroupID: &gid}) - if resolved.Source == "channel" && resolved.Mode == BillingModeToken { - useImageTokenBilling = true + if resolved.Source == "channel" { + hasChannelPricing = true } } - if useImageTokenBilling { - // 渠道配置了 token 定价 → 用 token 计费(image_output_tokens 独立计价) + if hasChannelPricing { + // 渠道定价优先 → 由 CalculateCostUnified 按 resolved.Mode 分发计费 tokens := UsageTokens{ InputTokens: result.Usage.InputTokens, OutputTokens: result.Usage.OutputTokens, @@ -7901,12 +7901,12 @@ func (s *GatewayService) RecordUsage(ctx context.Context, input *RecordUsageInpu // 设置计费模式 if result.MediaType != "image" && result.MediaType != "video" && result.MediaType != "prompt" { - if result.ImageCount > 0 { - billingMode := "image" - usageLog.BillingMode = &billingMode - } else if cost != nil && cost.BillingMode != "" { + if cost != nil && cost.BillingMode != "" { billingMode := cost.BillingMode usageLog.BillingMode = &billingMode + } else if result.ImageCount > 0 { + billingMode := "image" + usageLog.BillingMode = &billingMode } else { billingMode := "token" usageLog.BillingMode = &billingMode @@ -8033,16 +8033,16 @@ func (s *GatewayService) RecordUsageWithLongContext(ctx context.Context, input * // 根据请求类型选择计费方式 if result.ImageCount > 0 { - // 图片生成计费:渠道 token 定价优先,否则走按次计费(兼容旧版本) - useImageTokenBilling := false + // 图片生成计费:渠道级别定价优先,否则走按次计费(兼容旧版本) + hasChannelPricing := false if s.resolver != nil && apiKey.Group != nil { gid := apiKey.Group.ID resolved := s.resolver.Resolve(ctx, PricingInput{Model: billingModel, GroupID: &gid}) - if resolved.Source == "channel" && resolved.Mode == BillingModeToken { - useImageTokenBilling = true + if resolved.Source == "channel" { + hasChannelPricing = true } } - if useImageTokenBilling { + if hasChannelPricing { tokens := UsageTokens{ InputTokens: result.Usage.InputTokens, OutputTokens: result.Usage.OutputTokens, @@ -8175,12 +8175,12 @@ func (s *GatewayService) RecordUsageWithLongContext(ctx context.Context, input * } // 设置计费模式 - if result.ImageCount > 0 { - billingMode := "image" - usageLog.BillingMode = &billingMode - } else if cost != nil && cost.BillingMode != "" { + if cost != nil && cost.BillingMode != "" { billingMode := cost.BillingMode usageLog.BillingMode = &billingMode + } else if result.ImageCount > 0 { + billingMode := "image" + usageLog.BillingMode = &billingMode } else { billingMode := "token" usageLog.BillingMode = &billingMode