fix(apikey):修复在线体验获取到无效key的问题

This commit is contained in:
qian committed 2026-08-18 22:32:58 +08:00
1 parent 9161ddb5c8
commit ed82af4180
3 files changed
+10 -2

No files matched your search

+1
View File
@@ -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"`
}
+7
View File
@@ -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"`
@@ -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) {