import { useState, useEffect, useCallback } from 'react' import { Key, Plus, Trash2, Copy, RefreshCw, Code, X } from 'lucide-react' import api, { APIResponse } from '../services/api' interface ApiKeyItem { id: string name: string key?: string prefix: string ip_whitelist: string created_at: string last_used: string } const BASE_URL = window.location.origin export default function ApiIntegration() { const [keys, setKeys] = useState([]) const [loading, setLoading] = useState(true) const [showCreate, setShowCreate] = useState(false) const [newName, setNewName] = useState('') const [newIPs, setNewIPs] = useState('') const [creating, setCreating] = useState(false) const [newKey, setNewKey] = useState('') const [showDocs, setShowDocs] = useState(true) const [copiedKey, setCopiedKey] = useState(false) const fetchKeys = useCallback(async () => { try { const res = await api.get>('/api-keys') setKeys(res.data.data || []) } catch { /* ignore */ } finally { setLoading(false) } }, []) useEffect(() => { fetchKeys() }, [fetchKeys]) const createKey = async () => { if (!newName.trim()) return setCreating(true) try { const res = await api.post>('/api-keys', { name: newName.trim(), ip_whitelist: newIPs.trim(), }) if (res.data.data?.key) { setNewKey(res.data.data.key) setKeys(prev => [res.data.data!, ...prev]) } setNewName('') setNewIPs('') setShowCreate(false) } catch { /* ignore */ } finally { setCreating(false) } } const deleteKey = async (id: string) => { if (!window.confirm('确定要删除此 API Key 吗?')) return try { await api.delete(`/api-keys/${id}`) setKeys(prev => prev.filter(k => k.id !== id)) } catch { /* ignore */ } } const copyKey = () => { try { navigator.clipboard.writeText(newKey) } catch { const ta = document.createElement('textarea') ta.value = newKey ta.style.position = 'fixed' ta.style.left = '-9999px' document.body.appendChild(ta) ta.select() document.execCommand('copy') document.body.removeChild(ta) } setCopiedKey(true) setTimeout(() => setCopiedKey(false), 2000) } return (

API 集成

管理 API Key 与查看接口文档

{/* API Keys */}

API Keys

{newKey && (
新 API Key 已生成

此 Key 仅显示一次,请立即复制保存。

{newKey}
)} {loading ? (
加载中...
) : keys.length === 0 ? (
暂无 API Key,点击"创建 Key"开始
) : (
{keys.map(k => ( ))}
名称 Key 前缀 IP 白名单 创建时间 最后使用 操作
{k.name} {k.prefix} {k.ip_whitelist || '不限制'} {k.created_at} {k.last_used || '未使用'}
)}
{/* Create Key Modal */} {showCreate && (
setShowCreate(false)} />

创建 API Key

setNewName(e.target.value)} placeholder="例如:自动化脚本、CI/CD" className="w-full px-3 py-2 border border-gray-300 rounded-md text-sm" onKeyDown={e => e.key === 'Enter' && createKey()} autoFocus />