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) {