P1 修复内容: - F7: health handler 连接池容量检查 (80%阈值返回503 degraded) - F9: SSE spawned task 并发限制 (Semaphore 16 permits) - F10: Key Pool 单次 JOIN 查询优化 (消除 N+1) - F12: CORS panic → 配置错误 - F14: 连接池使用率计算修正 (ratio = used*100/total) - F15: SQL 迁移解析器替换为状态机 (支持 $$, DO $body$, 存储过程) - Worker 重试机制: 失败任务通过 mpsc channel 重新入队 - DOMPurify XSS 防护 (PipelineResultPreview) - Admin V2: ErrorBoundary + SWR全局配置 + 请求优化
166 lines
5.5 KiB
TypeScript
166 lines
5.5 KiB
TypeScript
// ============================================================
|
||
// API 密钥管理
|
||
// ============================================================
|
||
|
||
import { useState } from 'react'
|
||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||
import { Button, message, Tag, Modal, Form, Input, InputNumber, Select, Popconfirm, Space, Typography } from 'antd'
|
||
import { PlusOutlined, CopyOutlined } from '@ant-design/icons'
|
||
import type { ProColumns } from '@ant-design/pro-components'
|
||
import { ProTable } from '@ant-design/pro-components'
|
||
import { apiKeyService } from '@/services/api-keys'
|
||
import type { TokenInfo } from '@/types'
|
||
|
||
const { Text } = Typography
|
||
|
||
export default function ApiKeys() {
|
||
const queryClient = useQueryClient()
|
||
const [form] = Form.useForm()
|
||
const [modalOpen, setModalOpen] = useState(false)
|
||
const [newToken, setNewToken] = useState<string | null>(null)
|
||
|
||
const { data, isLoading } = useQuery({
|
||
queryKey: ['api-keys'],
|
||
queryFn: ({ signal }) => apiKeyService.list(signal),
|
||
})
|
||
|
||
const createMutation = useMutation({
|
||
mutationFn: (data: { name: string; expires_days?: number; permissions: string[] }) =>
|
||
apiKeyService.create(data),
|
||
onSuccess: (result: TokenInfo) => {
|
||
message.success('创建成功')
|
||
queryClient.invalidateQueries({ queryKey: ['api-keys'] })
|
||
if (result.token) {
|
||
setNewToken(result.token)
|
||
}
|
||
setModalOpen(false)
|
||
form.resetFields()
|
||
},
|
||
onError: (err: Error) => message.error(err.message || '创建失败'),
|
||
})
|
||
|
||
const revokeMutation = useMutation({
|
||
mutationFn: (id: string) => apiKeyService.revoke(id),
|
||
onSuccess: () => {
|
||
message.success('已撤销')
|
||
queryClient.invalidateQueries({ queryKey: ['api-keys'] })
|
||
},
|
||
onError: (err: Error) => message.error(err.message || '撤销失败'),
|
||
})
|
||
|
||
const columns: ProColumns<TokenInfo>[] = [
|
||
{ title: '名称', dataIndex: 'name', width: 160 },
|
||
{ title: '前缀', dataIndex: 'token_prefix', width: 120, render: (_, r) => <Text code>{r.token_prefix}...</Text> },
|
||
{
|
||
title: '权限',
|
||
dataIndex: 'permissions',
|
||
width: 200,
|
||
render: (_, r) => r.permissions?.map((p) => <Tag key={p}>{p}</Tag>),
|
||
},
|
||
{
|
||
title: '过期时间',
|
||
dataIndex: 'expires_at',
|
||
width: 180,
|
||
render: (_, r) => r.expires_at ? new Date(r.expires_at).toLocaleString('zh-CN') : '永不过期',
|
||
},
|
||
{
|
||
title: '最后使用',
|
||
dataIndex: 'last_used_at',
|
||
width: 180,
|
||
render: (_, r) => r.last_used_at ? new Date(r.last_used_at).toLocaleString('zh-CN') : '-',
|
||
},
|
||
{
|
||
title: '创建时间',
|
||
dataIndex: 'created_at',
|
||
width: 180,
|
||
render: (_, r) => new Date(r.created_at).toLocaleString('zh-CN'),
|
||
},
|
||
{
|
||
title: '操作',
|
||
width: 100,
|
||
render: (_, record) => (
|
||
<Popconfirm title="确定撤销此密钥?撤销后无法恢复。" onConfirm={() => revokeMutation.mutate(record.id)}>
|
||
<Button size="small" danger>撤销</Button>
|
||
</Popconfirm>
|
||
),
|
||
},
|
||
]
|
||
|
||
const handleCreate = async () => {
|
||
const values = await form.validateFields()
|
||
createMutation.mutate(values)
|
||
}
|
||
|
||
const copyToken = () => {
|
||
if (newToken) {
|
||
navigator.clipboard.writeText(newToken)
|
||
message.success('已复制到剪贴板')
|
||
}
|
||
}
|
||
|
||
return (
|
||
<div>
|
||
<ProTable<TokenInfo>
|
||
columns={columns}
|
||
dataSource={data?.items ?? []}
|
||
loading={isLoading}
|
||
rowKey="id"
|
||
search={false}
|
||
toolBarRender={() => [
|
||
<Button key="add" type="primary" icon={<PlusOutlined />} onClick={() => { form.resetFields(); setModalOpen(true) }}>
|
||
创建密钥
|
||
</Button>,
|
||
]}
|
||
pagination={{
|
||
total: data?.total ?? 0,
|
||
pageSize: data?.page_size ?? 20,
|
||
current: data?.page ?? 1,
|
||
showSizeChanger: false,
|
||
}}
|
||
/>
|
||
|
||
<Modal
|
||
title="创建 API 密钥"
|
||
open={modalOpen}
|
||
onOk={handleCreate}
|
||
onCancel={() => { setModalOpen(false); form.resetFields() }}
|
||
confirmLoading={createMutation.isPending}
|
||
>
|
||
<Form form={form} layout="vertical">
|
||
<Form.Item name="name" label="名称" rules={[{ required: true }]}>
|
||
<Input placeholder="给密钥起个名字" />
|
||
</Form.Item>
|
||
<Form.Item name="expires_days" label="有效期 (天)">
|
||
<InputNumber min={1} placeholder="留空则永不过期" style={{ width: '100%' }} />
|
||
</Form.Item>
|
||
<Form.Item name="permissions" label="权限" rules={[{ required: true }]}>
|
||
<Select mode="multiple" placeholder="选择权限" options={[
|
||
{ value: 'relay:use', label: '中转使用' },
|
||
{ value: 'model:read', label: '模型读取' },
|
||
{ value: 'config:read', label: '配置读取' },
|
||
]} />
|
||
</Form.Item>
|
||
</Form>
|
||
</Modal>
|
||
|
||
<Modal
|
||
title="密钥创建成功"
|
||
open={!!newToken}
|
||
onOk={() => setNewToken(null)}
|
||
onCancel={() => setNewToken(null)}
|
||
>
|
||
<p>请立即保存此密钥,关闭后将无法再次查看:</p>
|
||
<Input.TextArea
|
||
value={newToken || ''}
|
||
rows={3}
|
||
readOnly
|
||
addonAfter={<CopyOutlined onClick={copyToken} style={{ cursor: 'pointer' }} />}
|
||
/>
|
||
<Button type="primary" icon={<CopyOutlined />} onClick={copyToken} style={{ marginTop: 8 }}>
|
||
复制密钥
|
||
</Button>
|
||
</Modal>
|
||
</div>
|
||
)
|
||
}
|