From ed82af418077daab221523fd59b13486f686eab1 Mon Sep 17 00:00:00 2001 From: qian Date: Tue, 18 Aug 2026 22:32:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(apikey):=E4=BF=AE=E5=A4=8D=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E4=BD=93=E9=AA=8C=E8=8E=B7=E5=8F=96=E5=88=B0=E6=97=A0?= =?UTF-8?q?=E6=95=88key=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/tenant/v1/api_key.go | 1 + internal/logic/tenant/api_key.go | 7 +++++++ .../src/views/service/playground/usePlaygroundApiKey.ts | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/api/tenant/v1/api_key.go b/api/tenant/v1/api_key.go index b57aa15..7675360 100644 --- a/api/tenant/v1/api_key.go +++ b/api/tenant/v1/api_key.go @@ -11,6 +11,7 @@ type TenantApiKeyListReq struct { g.Meta `path:"/api-keys" method:"get" mime:"json" tags:"租户控制台-API Key" summary:"API Key列表"` KeyType string `json:"key_type" in:"query"` ProjectID int64 `json:"project_id" in:"query"` + ValidOnly bool `json:"valid_only" in:"query" dc:"仅返回有效密钥(active 且未过期),用于在线体验等场景"` Page int `json:"page" in:"query" d:"1"` PageSize int `json:"page_size" in:"query" d:"20"` } diff --git a/internal/logic/tenant/api_key.go b/internal/logic/tenant/api_key.go index 5096f6c..c05015d 100644 --- a/internal/logic/tenant/api_key.go +++ b/internal/logic/tenant/api_key.go @@ -52,6 +52,13 @@ func (s *sTenant) ApiKeyList(ctx context.Context, req *v1.TenantApiKeyListReq) ( query = query.Where("user_id", userID).Where("key_type", "personal") } + // valid_only:只返回真正可用的密钥(active 且未过期)。过期密钥的 status 仍为 active, + // 仅凭状态过滤不出来,必须同时比较 expires_at;管理列表默认不过滤,禁用/过期密钥仍需展示 + if req.ValidOnly { + query = query.Where("status", "active"). + Where("(expires_at IS NULL OR expires_at > NOW())") + } + type keyRow struct { Id int64 `json:"id"` Name string `json:"name"` diff --git a/web/tenant/src/views/service/playground/usePlaygroundApiKey.ts b/web/tenant/src/views/service/playground/usePlaygroundApiKey.ts index 487828f..7bc6b1e 100644 --- a/web/tenant/src/views/service/playground/usePlaygroundApiKey.ts +++ b/web/tenant/src/views/service/playground/usePlaygroundApiKey.ts @@ -25,10 +25,10 @@ export function usePlaygroundApiKey() { loading.value = true error.value = null const res = await request.get('/tenant/api-keys', { - params: { key_type: 'personal', page: 1, page_size: 100 }, + params: { key_type: 'personal', valid_only: true, page: 1, page_size: 100 }, }) const list: ApiKeyItem[] = res.data?.data?.list ?? [] - // 只保留 active 状态的 key + // 后端 valid_only 已过滤禁用/过期密钥,此处再按 active 兜底一次 apiKeys.value = list.filter((k: ApiKeyItem) => k.status === 'active') if (apiKeys.value.length > 0) {