import React, { useCallback, useEffect, useState } from 'react'; import { Alert, Button, DatePicker, Form, Input, InputNumber, Modal, Popconfirm, Select, Space, Switch, Table, Tabs, Tag, Tooltip, Typography, message, } from 'antd'; import { CopyOutlined, DeleteOutlined, DesktopOutlined, EditOutlined, ExportOutlined, KeyOutlined, LinkOutlined, PlusOutlined, ReloadOutlined, } from '@ant-design/icons'; import dayjs from 'dayjs'; import { SycmCard } from '../../components/Common'; import { createApiKey, createExternalAutomationApp, deleteApiKey, deleteExternalAutomationApp, getApiKeys, getExternalAutomationApps, updateApiKey, updateExternalAutomationApp, } from '../../api/admin'; const { Paragraph, Text, Title } = Typography; const API_BASE_URL = 'https://pddg.jiansuntuzhi.cn/api/external/selection-fission'; const PRESALE_API_URL = `${API_BASE_URL}/presale/tasks`; const PLATFORM_LABELS = { windows: 'Windows', macos: 'macOS', linux: 'Linux', all: '全平台', }; function formatDateTime(value) { if (!value) return '-'; const date = dayjs(value); return date.isValid() ? date.format('YYYY-MM-DD HH:mm:ss') : '-'; } const ApiKeySettings = () => { const [form] = Form.useForm(); const [appForm] = Form.useForm(); const [rows, setRows] = useState([]); const [appRows, setAppRows] = useState([]); const [loading, setLoading] = useState(false); const [appLoading, setAppLoading] = useState(false); const [saving, setSaving] = useState(false); const [updatingId, setUpdatingId] = useState(null); const [editingRecord, setEditingRecord] = useState(null); const [editorOpen, setEditorOpen] = useState(false); const [revealedKey, setRevealedKey] = useState(''); const [appEditorOpen, setAppEditorOpen] = useState(false); const [editingApp, setEditingApp] = useState(null); const [appSaving, setAppSaving] = useState(false); const loadRows = useCallback(async () => { try { setLoading(true); const result = await getApiKeys(); setRows(Array.isArray(result?.data) ? result.data : []); } catch (error) { message.error(error.response?.data?.error || '加载 API Key 失败'); } finally { setLoading(false); } }, []); const loadAppRows = useCallback(async () => { try { setAppLoading(true); const result = await getExternalAutomationApps(); setAppRows(Array.isArray(result?.data) ? result.data : []); } catch (error) { message.error(error.response?.data?.error || '加载 App 链接失败'); } finally { setAppLoading(false); } }, []); useEffect(() => { loadRows(); loadAppRows(); }, [loadAppRows, loadRows]); const refreshAll = useCallback(() => { loadRows(); loadAppRows(); }, [loadAppRows, loadRows]); const copyText = async (value, successText) => { try { await navigator.clipboard.writeText(value); message.success(successText); } catch { message.error('复制失败,请手动选择复制'); } }; const openCreateApp = () => { setEditingApp(null); appForm.resetFields(); appForm.setFieldsValue({ platform: 'windows', enabled: true, sortOrder: 0 }); setAppEditorOpen(true); }; const openEditApp = (record) => { setEditingApp(record); appForm.setFieldsValue(record); setAppEditorOpen(true); }; const saveApp = async () => { try { const values = await appForm.validateFields(); setAppSaving(true); if (editingApp) { await updateExternalAutomationApp(editingApp.id, values); } else { await createExternalAutomationApp(values); } message.success(editingApp ? 'App 链接已更新' : 'App 链接已创建'); setAppEditorOpen(false); appForm.resetFields(); await loadAppRows(); } catch (error) { if (!error?.errorFields) { message.error(error.response?.data?.error || '保存 App 链接失败'); } } finally { setAppSaving(false); } }; const toggleApp = async (record, enabled) => { try { setUpdatingId(`app-${record.id}`); await updateExternalAutomationApp(record.id, { enabled }); message.success(enabled ? 'App 链接已启用' : 'App 链接已停用'); await loadAppRows(); } catch (error) { message.error(error.response?.data?.error || '更新 App 链接状态失败'); } finally { setUpdatingId(null); } }; const removeApp = async (record) => { try { setUpdatingId(`app-${record.id}`); await deleteExternalAutomationApp(record.id); message.success('App 链接已删除'); await loadAppRows(); } catch (error) { message.error(error.response?.data?.error || '删除 App 链接失败'); } finally { setUpdatingId(null); } }; const openCreate = () => { setEditingRecord(null); form.resetFields(); form.setFieldsValue({ enabled: true, hmacRequired: false, allowedIps: '' }); setEditorOpen(true); }; const openEdit = (record) => { setEditingRecord(record); form.setFieldsValue({ name: record.name, description: record.description || '', apiKey: '', allowedIps: Array.isArray(record.allowedIps) ? record.allowedIps.join('\n') : '', hmacRequired: record.hmacRequired === true, enabled: record.enabled, expiresAt: record.expiresAt ? dayjs(record.expiresAt) : null, }); setEditorOpen(true); }; const saveKey = async () => { try { const values = await form.validateFields(); setSaving(true); const payload = { name: values.name, description: values.description || '', allowedIps: String(values.allowedIps || '') .split(/[\s,;]+/) .map((item) => item.trim()) .filter(Boolean), hmacRequired: values.hmacRequired === true, enabled: values.enabled !== false, expiresAt: values.expiresAt ? values.expiresAt.toISOString() : null, }; const apiKey = String(values.apiKey || '').trim(); if (apiKey) payload.apiKey = apiKey; const result = editingRecord ? await updateApiKey(editingRecord.id, payload) : await createApiKey(payload); setEditorOpen(false); form.resetFields(); message.success(editingRecord ? 'API Key 已更新' : 'API Key 已创建'); if (result?.data?.plainApiKey) { setRevealedKey(result.data.plainApiKey); } await loadRows(); } catch (error) { if (!error?.errorFields) { message.error(error.response?.data?.error || '保存 API Key 失败'); } } finally { setSaving(false); } }; const toggleKey = async (record, enabled) => { try { setUpdatingId(record.id); await updateApiKey(record.id, { enabled }); message.success(enabled ? 'API Key 已启用' : 'API Key 已停用'); await loadRows(); } catch (error) { message.error(error.response?.data?.error || '更新 API Key 状态失败'); } finally { setUpdatingId(null); } }; const removeKey = async (record) => { try { setUpdatingId(record.id); await deleteApiKey(record.id); message.success('API Key 已删除'); await loadRows(); } catch (error) { message.error(error.response?.data?.error || '删除 API Key 失败'); } finally { setUpdatingId(null); } }; const copyRevealedKey = async () => { await copyText(revealedKey, '完整 API Key 已复制'); }; const appColumns = [ { title: 'App', dataIndex: 'name', width: 220, render: (value, record) => (
{value} {record.version ? {record.version} : null} {record.description ? {record.description} : null}
), }, { title: '平台', dataIndex: 'platform', width: 110, render: (value) => {PLATFORM_LABELS[value] || value}, }, { title: '下载/启动链接', dataIndex: 'downloadUrl', width: 420, render: (value) => ( {value} } message="对外接口地址" description={(
自动上架 {API_BASE_URL}
预售任务 {PRESALE_API_URL}
)} /> App 链接, children: ( <>
提供桌面端安装包、启动地址和版本更新入口
10 ? { pageSize: 10, showSizeChanger: false } : false} scroll={{ x: 1120 }} locale={{ emptyText: '暂无 App 链接,点击右上角新增' }} /> ), }, { key: 'keys', label: 请求 Key, children: ( <>
同一个 Key 用于自动上架、店铺同步、结果回传和预售任务查询
10 ? { pageSize: 10, showSizeChanger: false } : false} scroll={{ x: 1540 }} locale={{ emptyText: '暂无请求 Key,点击右上角生成' }} /> ), }, ]} /> setAppEditorOpen(false)} destroyOnHidden >
} placeholder="https://..." /> } placeholder="选填,https://..." />
setEditorOpen(false)} destroyOnHidden >
setRevealedKey('')}> 我已保存 )} >
{revealedKey}
); }; export default ApiKeySettings;