import { useCallback, useEffect, useMemo, useState } from 'react'; import { Button, Card, Col, Form, Input, message, Modal, Popconfirm, Row, Space, Statistic, Table, Tag, Typography, } from 'antd'; import type { TableProps } from 'antd'; import { DeleteOutlined, ImportOutlined, LoginOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons'; import { huyaApi, type HuyaAccountItem } from '../api/modules'; import { usePermissions } from '../hooks/usePermissions'; import { formatTime } from '../utils/time'; import { getErrorMessage } from '../utils/error'; const { Text, Paragraph } = Typography; const { TextArea } = Input; const STATUS_LABELS: Record = { imported: '已导入', updated: '已更新', login_success: '登录成功', active: '正常', invalid: '失效', }; const STATUS_COLORS: Record = { imported: 'blue', updated: 'cyan', login_success: 'success', active: 'success', invalid: 'error', }; interface PasswordLoginFormValues { username: string; password: string; tag?: string; cookie?: string; } export default function HuyaAccountsPage() { const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(false); const [importOpen, setImportOpen] = useState(false); const [importText, setImportText] = useState(''); const [importTag, setImportTag] = useState(''); const [importing, setImporting] = useState(false); const [passwordLoginOpen, setPasswordLoginOpen] = useState(false); const [passwordLogging, setPasswordLogging] = useState(false); const [searchText, setSearchText] = useState(''); const [selectedRowKeys, setSelectedRowKeys] = useState([]); const [pageSize, setPageSize] = useState(() => { const v = localStorage.getItem('huya_account_page_size'); return v ? Number(v) || 20 : 20; }); const [currentPage, setCurrentPage] = useState(1); const [passwordLoginForm] = Form.useForm(); const { can } = usePermissions(); const canManage = can('huya:account'); const loadAccounts = useCallback(async () => { setLoading(true); try { const data = await huyaApi.listAccounts(); setAccounts(data); } catch (e: unknown) { message.error(getErrorMessage(e)); } finally { setLoading(false); } }, []); useEffect(() => { loadAccounts(); }, [loadAccounts]); const tags = useMemo(() => { return [...new Set(accounts.map((item) => item.tag.trim()).filter(Boolean))].sort(); }, [accounts]); const filteredAccounts = useMemo(() => { const s = searchText.trim().toLowerCase(); if (!s) return accounts; return accounts.filter((item) => ( item.uid.toLowerCase().includes(s) || item.yyuid.toLowerCase().includes(s) || item.username.toLowerCase().includes(s) || item.nickname.toLowerCase().includes(s) || item.tag.toLowerCase().includes(s) || item.game_name.toLowerCase().includes(s) || item.game_phone.toLowerCase().includes(s) )); }, [accounts, searchText]); const handleImport = async () => { if (!importText.trim()) { message.warning('请先粘贴虎牙 CK'); return; } setImporting(true); try { const result = await huyaApi.importCookies(importText, importTag); message.success(result.message); setImportOpen(false); setImportText(''); setImportTag(''); loadAccounts(); } catch (e: unknown) { message.error(getErrorMessage(e)); } finally { setImporting(false); } }; const openPasswordLogin = () => { passwordLoginForm.resetFields(); setPasswordLoginOpen(true); }; const closePasswordLogin = () => { if (passwordLogging) return; setPasswordLoginOpen(false); passwordLoginForm.resetFields(); }; const handlePasswordLogin = async (values: PasswordLoginFormValues) => { setPasswordLogging(true); try { const result = await huyaApi.passwordLogin({ username: values.username.trim(), password: values.password, tag: values.tag?.trim() || '', cookie: values.cookie?.trim() || '', }); message.success(result.message || '登录成功,Cookie 已保存'); setPasswordLoginOpen(false); passwordLoginForm.resetFields(); loadAccounts(); } catch (e: unknown) { message.error(getErrorMessage(e)); } finally { setPasswordLogging(false); } }; const handleDelete = async (id: number) => { try { await huyaApi.deleteAccount(id); message.success('已删除'); setSelectedRowKeys((prev) => prev.filter((key) => key !== id)); loadAccounts(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; const handleDeleteSelected = async () => { if (selectedRowKeys.length === 0) { message.warning('请先选择虎牙 CK'); return; } try { const result = await huyaApi.deleteAccounts(selectedRowKeys.map((key) => Number(key))); message.success(result.message); setSelectedRowKeys([]); loadAccounts(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; const boundCount = accounts.filter((item) => item.game_name || item.game_channel || item.game_phone).length; const pointCount = accounts.filter((item) => item.points !== null && item.points !== undefined).length; const columns: TableProps['columns'] = [ { title: 'ID', dataIndex: 'id', width: 70, align: 'center' }, { title: '虎牙账号', width: 180, render: (_: unknown, record) => ( {record.nickname || record.username || record.uid || '-'} UID {record.uid || record.yyuid || '-'} ), }, { title: '标签', dataIndex: 'tag', width: 110, render: (tag: string) => tag ? {tag} : -, }, { title: '积分', dataIndex: 'points', width: 90, align: 'center', render: (points: number | null) => points ?? 未查, }, { title: '游戏名', dataIndex: 'game_name', width: 160, ellipsis: true, render: (value: string, record) => value ? ( {value} {record.game_channel ? ( {record.game_channel} ) : null} ) : 未查, }, { title: '手机号', dataIndex: 'game_phone', width: 140, ellipsis: true, render: (value: string) => value || -, }, { title: 'Cookie', dataIndex: 'cookie_preview', ellipsis: true, render: (value: string) => ( {value || '-'} ), }, { title: '状态', dataIndex: 'status', width: 100, align: 'center', render: (status: string) => ( {STATUS_LABELS[status] || status || '-'} ), }, { title: '更新时间', dataIndex: 'updated_at', width: 170, render: (value: string | null) => value ? formatTime(value) : -, }, { title: '操作', width: 90, fixed: 'right', align: 'center', render: (_: unknown, record) => ( handleDelete(record.id)}> {selectedRowKeys.length > 0 && ( )} {canManage && ( )} {canManage && ( )}
setSearchText(e.target.value)} style={{ width: 300 }} prefix={} /> {tags.map((tag) => ( setSearchText(tag)} style={{ cursor: 'pointer' }}> {tag} ))}
setSelectedRowKeys(keys), }} columns={columns} dataSource={filteredAccounts} rowKey="id" loading={loading} size="small" pagination={{ current: currentPage, pageSize, showSizeChanger: true, showTotal: (total) => `共 ${total} 条`, onChange: (page, size) => { setCurrentPage(page); if (size !== pageSize) { setPageSize(size); localStorage.setItem('huya_account_page_size', String(size)); setCurrentPage(1); } }, }} scroll={{ x: 1120 }} /> setImportOpen(false)} onOk={handleImport} okText="导入" confirmLoading={importing} width={720} > setImportTag(e.target.value)} />