From a8f7c0614f515425e20eff66df021bb511a16c2f Mon Sep 17 00:00:00 2001 From: Seefs Date: Mon, 5 Jan 2026 18:09:02 +0800 Subject: [PATCH] fix: batch add key backend deduplication --- controller/channel.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index ea2f47680..9fea9a805 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -970,9 +970,6 @@ func UpdateChannel(c *gin.Context) { // 单个JSON密钥 newKeys = []string{channel.Key} } - // 合并密钥 - allKeys := append(existingKeys, newKeys...) - channel.Key = strings.Join(allKeys, "\n") } else { // 普通渠道的处理 inputKeys := strings.Split(channel.Key, "\n") @@ -982,10 +979,31 @@ func UpdateChannel(c *gin.Context) { newKeys = append(newKeys, key) } } - // 合并密钥 - allKeys := append(existingKeys, newKeys...) - channel.Key = strings.Join(allKeys, "\n") } + + seen := make(map[string]struct{}, len(existingKeys)+len(newKeys)) + for _, key := range existingKeys { + normalized := strings.TrimSpace(key) + if normalized == "" { + continue + } + seen[normalized] = struct{}{} + } + dedupedNewKeys := make([]string, 0, len(newKeys)) + for _, key := range newKeys { + normalized := strings.TrimSpace(key) + if normalized == "" { + continue + } + if _, ok := seen[normalized]; ok { + continue + } + seen[normalized] = struct{}{} + dedupedNewKeys = append(dedupedNewKeys, normalized) + } + + allKeys := append(existingKeys, dedupedNewKeys...) + channel.Key = strings.Join(allKeys, "\n") } case "replace": // 覆盖模式:直接使用新密钥(默认行为,不需要特殊处理)