diff --git a/backend/internal/handler/admin/channel_handler.go b/backend/internal/handler/admin/channel_handler.go index 0e860fe0..08c6370c 100644 --- a/backend/internal/handler/admin/channel_handler.go +++ b/backend/internal/handler/admin/channel_handler.go @@ -226,14 +226,34 @@ func pricingRequestToService(reqs []channelModelPricingRequest) []service.Channe return result } -// validatePricingBillingMode 校验按次/图片计费模式必须配置 PerRequestPrice 或 Intervals +// validatePricingBillingMode 校验计费配置 func validatePricingBillingMode(pricing []service.ChannelModelPricing) error { for _, p := range pricing { + // 按次/图片模式必须配置默认价格或区间 if p.BillingMode == service.BillingModePerRequest || p.BillingMode == service.BillingModeImage { if p.PerRequestPrice == nil && len(p.Intervals) == 0 { return errors.New("per-request price or intervals required for per_request/image billing mode") } } + // 校验价格不能为负 + if err := validatePriceNotNegative("input_price", p.InputPrice); err != nil { + return err + } + if err := validatePriceNotNegative("output_price", p.OutputPrice); err != nil { + return err + } + if err := validatePriceNotNegative("cache_write_price", p.CacheWritePrice); err != nil { + return err + } + if err := validatePriceNotNegative("cache_read_price", p.CacheReadPrice); err != nil { + return err + } + if err := validatePriceNotNegative("image_output_price", p.ImageOutputPrice); err != nil { + return err + } + if err := validatePriceNotNegative("per_request_price", p.PerRequestPrice); err != nil { + return err + } // 校验 interval:至少有一个价格字段非空 for _, iv := range p.Intervals { if iv.InputPrice == nil && iv.OutputPrice == nil && @@ -247,6 +267,13 @@ func validatePricingBillingMode(pricing []service.ChannelModelPricing) error { return nil } +func validatePriceNotNegative(field string, val *float64) error { + if val != nil && *val < 0 { + return fmt.Errorf("%s must be >= 0", field) + } + return nil +} + func formatMaxTokens(max *int) string { if max == nil { return "∞"