From 235f7c6e5f0c97c0bacf6744f19bf9b25135a6d8 Mon Sep 17 00:00:00 2001 From: fengsilin Date: Fri, 17 Apr 2026 10:34:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(channel-pricing):=20=E6=8F=90=E5=8F=96?= =?UTF-8?q?=20ParseTagIds=20=E8=BE=85=E5=8A=A9=E5=87=BD=E6=95=B0=20+=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=B0=83=E8=AF=95=20console.log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 tag ID 解析逻辑提取为 model.ParseTagIds,消除 model 层和 controller 层重复代码 - 统一 TrimSpace 处理(之前 controller 版本漏了) - 移除 ChannelPricingView 残留的 console.log('Expanded keys:') - 删除多余空行 Co-Authored-By: Claude Opus 4.6 --- controller/channel_pricing.go | 11 +------ model/channel_pricing.go | 29 +++++++++++-------- .../Setting/Ratio/ChannelPricingView.jsx | 1 - 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/controller/channel_pricing.go b/controller/channel_pricing.go index 5e9d456..9768e70 100644 --- a/controller/channel_pricing.go +++ b/controller/channel_pricing.go @@ -365,16 +365,7 @@ func GetChannelPricingWithTags(c *gin.Context) { for _, cp := range list { item := &ChannelPricingWithTags{ ChannelPricing: cp, - Tags: make([]*model.PricingTag, 0), - } - if cp.TagIds != "" { - for _, idStr := range strings.Split(cp.TagIds, ",") { - if id, err := strconv.Atoi(idStr); err == nil { - if tag, ok := tagMap[id]; ok { - item.Tags = append(item.Tags, tag) - } - } - } + Tags: model.ParseTagIds(cp.TagIds, tagMap), } result = append(result, item) } diff --git a/model/channel_pricing.go b/model/channel_pricing.go index a5280fb..9a5c05d 100644 --- a/model/channel_pricing.go +++ b/model/channel_pricing.go @@ -172,6 +172,22 @@ func GetEffectivePricing(modelName string, channelId int) (*ChannelPricing, bool return cp, true } +// ParseTagIds 解析逗号分隔的标签ID字符串为 PricingTag 切片 +func ParseTagIds(tagIds string, tagMap map[int]*PricingTag) []*PricingTag { + if tagIds == "" { + return nil + } + tags := make([]*PricingTag, 0) + for _, idStr := range strings.Split(tagIds, ",") { + if id, err := strconv.Atoi(strings.TrimSpace(idStr)); err == nil { + if tag, ok := tagMap[id]; ok { + tags = append(tags, tag) + } + } + } + return tags +} + // LoadChannelPricingCache 全量加载渠道定价到内存(启动时调用) func LoadChannelPricingCache() { var pricings []*ChannelPricing @@ -234,8 +250,6 @@ func GetChannelPricingByModelWithChannelInfo(modelName string) ([]*ChannelPricin if !hasPrice { globalModelPrice = 0 } - - // 获取全局扩展比率(用于 CASE WHEN 回退) globalCacheRatio, _ := ratio_setting.GetCacheRatio(modelName) globalCacheCreationRatio, _ := ratio_setting.GetCreateCacheRatio(modelName) @@ -283,16 +297,7 @@ func GetChannelPricingByModelWithChannelInfo(modelName string) ([]*ChannelPricin // 为每个渠道定价填充标签 for _, result := range results { - if result.TagIds != "" { - result.Tags = make([]*PricingTag, 0) - for _, idStr := range strings.Split(result.TagIds, ",") { - if id, err := strconv.Atoi(strings.TrimSpace(idStr)); err == nil { - if tag, ok := tagMap[id]; ok { - result.Tags = append(result.Tags, tag) - } - } - } - } + result.Tags = ParseTagIds(result.TagIds, tagMap) } return results, nil diff --git a/web/src/pages/Setting/Ratio/ChannelPricingView.jsx b/web/src/pages/Setting/Ratio/ChannelPricingView.jsx index f097bc9..cd09e3e 100644 --- a/web/src/pages/Setting/Ratio/ChannelPricingView.jsx +++ b/web/src/pages/Setting/Ratio/ChannelPricingView.jsx @@ -463,7 +463,6 @@ const ChannelPricingView = ({ channels, channelPricings, tags, onRefresh }) => { rowKey="modelName" expandedRowKeys={expandedRowKeys} onExpandedRowsChange={(keys) => { - console.log('Expanded keys:', keys); setExpandedRowKeys(keys); }} expandedRowRender={expandedRowRender}