import { useEffect, useState } from 'react'; import { Table, Button, Space, Modal, Form, Input, message, Tag, Popconfirm, } from 'antd'; import { PlusOutlined, EditOutlined } from '@ant-design/icons'; import { listAccounts, createAccount, updateAccount, deleteAccount, switchAccount, Account, AccountInput, } from '../api/accounts'; export default function AccountsPage() { const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(false); const [modalOpen, setModalOpen] = useState(false); const [editingAccount, setEditingAccount] = useState(null); const [form] = Form.useForm(); const fetchAccounts = async () => { setLoading(true); try { const data = await listAccounts(); setAccounts(data); } finally { setLoading(false); } }; useEffect(() => { fetchAccounts(); }, []); const openCreateModal = () => { setEditingAccount(null); form.resetFields(); setModalOpen(true); }; const openEditModal = (record: Account) => { setEditingAccount(record); form.setFieldsValue({ name: record.name, accessKeyId: record.accessKeyId, region: record.region, endpoint: record.endpoint, }); setModalOpen(true); }; const closeModal = () => { setModalOpen(false); setEditingAccount(null); form.resetFields(); }; const handleSubmit = async (values: AccountInput) => { try { if (editingAccount) { const payload: Partial = { ...values }; if (!payload.accessKeySecret) { delete payload.accessKeySecret; } await updateAccount(editingAccount.id, payload); message.success('账号更新成功'); } else { await createAccount(values); message.success('账号添加成功'); } closeModal(); fetchAccounts(); } catch (error) { message.error(editingAccount ? '更新失败' : '添加失败'); } }; const handleDelete = async (id: string) => { try { await deleteAccount(id); message.success('已删除'); fetchAccounts(); } catch (error) { message.error('删除失败'); } }; const handleSwitch = async (id: string) => { try { await switchAccount(id); message.success('已切换当前账号'); fetchAccounts(); } catch (error) { message.error('切换失败'); } }; const columns = [ { title: '名称', dataIndex: 'name' }, { title: 'AccessKey ID', dataIndex: 'accessKeyId', ellipsis: true }, { title: 'Region', dataIndex: 'region' }, { title: '状态', dataIndex: 'isActive', render: (isActive: number) => isActive ? 当前使用 : 备用, }, { title: '操作', key: 'action', render: (_: unknown, record: Account) => ( {!record.isActive && ( )} handleDelete(record.id)}> ), }, ]; const modalTitle = editingAccount ? '编辑阿里云 VOD 账号' : '添加阿里云 VOD 账号'; return (
); }