From e73710144e9de2ba8513032b2866a1cc4ee54f95 Mon Sep 17 00:00:00 2001 From: erio Date: Sat, 7 Mar 2026 12:12:11 +0800 Subject: [PATCH] fix: always return affinity_client_count for affinity-enabled accounts Previously the count was only returned when Redis had data, causing the badge to not display for accounts with zero affinity clients. Now returns count=0 for all affinity-enabled accounts. Co-Authored-By: Claude Opus 4.6 --- .../internal/handler/admin/account_handler.go | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/backend/internal/handler/admin/account_handler.go b/backend/internal/handler/admin/account_handler.go index 6bfdda793c..eb9c370d7f 100644 --- a/backend/internal/handler/admin/account_handler.go +++ b/backend/internal/handler/admin/account_handler.go @@ -207,15 +207,26 @@ func (h *AccountHandler) buildAccountResponseWithRuntime(ctx context.Context, ac } } - // 亲和客户端数据 - if h.gatewayCache != nil && account.IsClientAffinityEnabled() && len(account.GroupIDs) > 0 { - accountGroups := map[int64][]int64{account.ID: account.GroupIDs} - if clients, err := h.gatewayCache.GetAccountAffinityClientsBatch(ctx, accountGroups, service.ClientAffinityTTL); err == nil { - if cl, ok := clients[account.ID]; ok { - count := int64(len(cl)) - item.AffinityClientCount = &count - item.AffinityClients = cl + // 亲和客户端数据(启用亲和的账号始终返回 count,即使为 0) + if account.IsClientAffinityEnabled() { + if h.gatewayCache != nil && len(account.GroupIDs) > 0 { + accountGroups := map[int64][]int64{account.ID: account.GroupIDs} + if clients, err := h.gatewayCache.GetAccountAffinityClientsBatch(ctx, accountGroups, service.ClientAffinityTTL); err == nil { + if cl, ok := clients[account.ID]; ok && len(cl) > 0 { + count := int64(len(cl)) + item.AffinityClientCount = &count + item.AffinityClients = cl + } else { + zero := int64(0) + item.AffinityClientCount = &zero + } + } else { + zero := int64(0) + item.AffinityClientCount = &zero } + } else { + zero := int64(0) + item.AffinityClientCount = &zero } } @@ -392,12 +403,15 @@ func (h *AccountHandler) List(c *gin.Context) { } } - // 注入亲和客户端数据到 DTO - if affinityClients != nil { - if clients, ok := affinityClients[acc.ID]; ok { + // 注入亲和客户端数据到 DTO(启用亲和的账号始终返回 count,即使为 0) + if acc.IsClientAffinityEnabled() { + if clients, ok := affinityClients[acc.ID]; ok && len(clients) > 0 { count := int64(len(clients)) item.AffinityClientCount = &count item.AffinityClients = clients + } else { + zero := int64(0) + item.AffinityClientCount = &zero } }