import { useCallback, useEffect, useState } from 'react'; import { Alert, Button, Descriptions, Drawer, Form, Input, InputNumber, Modal, Popconfirm, QRCode, Select, Space, Spin, Table, Tabs, Tag, Tooltip, Typography, theme, } from 'antd'; import type { TableProps } from 'antd'; import { DeleteOutlined, AppstoreOutlined, ImportOutlined, QrcodeOutlined, ReloadOutlined, SearchOutlined, } from '@ant-design/icons'; import { weiboApi, type PaginatedResponse, type WeiboAccountItem, type WeiboBindQrResult, type WeiboExchangeRecordsResult, type WeiboImportError, type WeiboMallListResult, type WeiboPaymentStatusResult, type WeiboPointHistoryResult, type WeiboRechargeOrderResult, } from '../api/modules'; import { usePermissions } from '../hooks/usePermissions'; import { formatTime } from '../utils/time'; import { getErrorMessage } from '../utils/error'; import { message } from '../utils/antdMessage'; const { Text, Title } = Typography; const { TextArea } = Input; const CK_STATUS: Record = { valid: { label: '有效', color: 'success' }, invalid: { label: '失效', color: 'error' }, error: { label: '查询失败', color: 'warning' }, }; const PAYMENT_STATUS_LABELS: Record = { PAY_STATUS_PENDING: '等待支付', PAY_STATUS_SUCCESS: '支付成功', PAY_STATUS_FAILED: '支付失败', PAY_STATUS_CLOSED: '订单关闭', }; function bindStatus(account: WeiboAccountItem) { if (account.is_bound === true && account.can_rebind === true) { return 可换绑; } if (account.is_bound === true) { const days = account.rebind_remain_days ?? 0; return {days > 0 ? `${days} 天后可换绑` : '已绑定'}; } if (account.is_bound === false) return 未绑定; return 未查询; } export default function WeiboAccountsPage() { const { token } = theme.useToken(); const { can } = usePermissions(); const canImport = can('weibo:import'); const canBind = can('weibo:bind'); const canRecharge = can('weibo:recharge'); const canExchange = can('weibo:exchange'); const canDelete = can('weibo:delete'); const [items, setItems] = useState([]); const [total, setTotal] = useState(0); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(20); const [search, setSearch] = useState(''); const [bindFilter, setBindFilter] = useState(''); const [loading, setLoading] = useState(false); const [refreshingId, setRefreshingId] = useState(null); const [importOpen, setImportOpen] = useState(false); const [importing, setImporting] = useState(false); const [importErrors, setImportErrors] = useState([]); const [form] = Form.useForm<{ text: string; tag?: string }>(); const [qrOpen, setQrOpen] = useState(false); const [qrAccount, setQrAccount] = useState(null); const [qrLoading, setQrLoading] = useState(false); const [qrResult, setQrResult] = useState(null); const [featureOpen, setFeatureOpen] = useState(false); const [featureAccount, setFeatureAccount] = useState(null); const [featureTab, setFeatureTab] = useState('goods'); const [mallLoading, setMallLoading] = useState(false); const [mallResult, setMallResult] = useState(null); const [exchangeLoading, setExchangeLoading] = useState(false); const [exchangeResult, setExchangeResult] = useState(null); const [pointLoading, setPointLoading] = useState(false); const [pointResult, setPointResult] = useState(null); const [exchangingGoodsId, setExchangingGoodsId] = useState(null); const [rechargeAmount, setRechargeAmount] = useState(1); const [rechargeCreating, setRechargeCreating] = useState(false); const [rechargeOrder, setRechargeOrder] = useState(null); const [paymentStatusLoading, setPaymentStatusLoading] = useState(false); const [paymentStatus, setPaymentStatus] = useState(null); const loadAccounts = useCallback(async () => { setLoading(true); try { const data: PaginatedResponse = await weiboApi.listAccounts({ search: search.trim() || undefined, bind_status: bindFilter || undefined, page, page_size: pageSize, }); setItems(data.items); setTotal(data.total); } catch (error) { message.error(getErrorMessage(error)); } finally { setLoading(false); } }, [bindFilter, page, pageSize, search]); useEffect(() => { void loadAccounts(); }, [loadAccounts]); const handleImport = async () => { const values = await form.validateFields(); setImporting(true); try { const result = await weiboApi.importCookies(values.text, values.tag || ''); setImportErrors(result.errors); if (result.count > 0) { message.success(result.message); await loadAccounts(); } if (result.errors.length === 0 && result.count > 0) { setImportOpen(false); form.resetFields(); } else if (result.count === 0) { message.error(result.message); } } catch (error) { message.error(getErrorMessage(error)); } finally { setImporting(false); } }; const handleRefresh = async (account: WeiboAccountItem) => { setRefreshingId(account.id); try { const updated = await weiboApi.refreshAccount(account.id); setItems((current) => current.map((item) => (item.id === updated.id ? updated : item)), ); setFeatureAccount((current) => current?.id === updated.id ? updated : current, ); message.success('账号资料已刷新'); } catch (error) { message.error(getErrorMessage(error)); void loadAccounts(); } finally { setRefreshingId(null); } }; const openQr = async (account: WeiboAccountItem) => { setQrAccount(account); setQrResult(null); setQrOpen(true); setQrLoading(true); try { const result = await weiboApi.bindQrcodes(account.id); setQrResult(result); } catch (error) { message.error(getErrorMessage(error)); setQrOpen(false); void loadAccounts(); } finally { setQrLoading(false); } }; const handleDelete = async (account: WeiboAccountItem) => { try { await weiboApi.deleteAccount(account.id); message.success('微博账号已删除'); if (items.length === 1 && page > 1) setPage(page - 1); else await loadAccounts(); } catch (error) { message.error(getErrorMessage(error)); } }; const loadMall = async (account: WeiboAccountItem) => { setMallLoading(true); try { setMallResult(await weiboApi.mallList(account.id)); } catch (error) { message.error(getErrorMessage(error)); } finally { setMallLoading(false); } }; const handleExchange = async (goods: WeiboMallListResult['goods'][number]) => { if (!featureAccount) return; setExchangingGoodsId(goods.id); try { const result = await weiboApi.exchangeGoods( featureAccount.id, goods.id, goods.price, ); if (result.verified) { message.success( `兑换成功:${result.goods_name},余额 ${result.balance_after?.toLocaleString() ?? '-'}`, ); } else { message.warning(result.verify_error || '兑换已提交,请查兑换记录复核'); } await loadMall(featureAccount); await loadExchangeRecords(featureAccount); await handleRefresh(featureAccount); } catch (error) { message.error(getErrorMessage(error)); } finally { setExchangingGoodsId(null); } }; const loadExchangeRecords = async (account: WeiboAccountItem) => { setExchangeLoading(true); try { setExchangeResult(await weiboApi.exchangeRecords(account.id)); } catch (error) { message.error(getErrorMessage(error)); } finally { setExchangeLoading(false); } }; const loadPointHistory = async (account: WeiboAccountItem) => { setPointLoading(true); try { setPointResult(await weiboApi.pointHistory(account.id)); } catch (error) { message.error(getErrorMessage(error)); } finally { setPointLoading(false); } }; const openFeatures = (account: WeiboAccountItem) => { setFeatureAccount(account); setFeatureOpen(true); setFeatureTab('goods'); setMallResult(null); setExchangeResult(null); setPointResult(null); setRechargeOrder(null); setPaymentStatus(null); setRechargeAmount(1); void loadMall(account); }; const switchFeatureTab = (key: string) => { setFeatureTab(key); if (!featureAccount) return; if (key === 'goods' && !mallResult) void loadMall(featureAccount); if (key === 'exchange' && !exchangeResult) { void loadExchangeRecords(featureAccount); } if (key === 'points' && !pointResult) void loadPointHistory(featureAccount); }; const createRecharge = async () => { if (!featureAccount) return; setRechargeCreating(true); setPaymentStatus(null); try { const order = await weiboApi.createRechargeOrder( featureAccount.id, rechargeAmount, ); setRechargeOrder(order); message.success(`已创建 ${order.amount_yuan} 元充值订单`); } catch (error) { message.error(getErrorMessage(error)); } finally { setRechargeCreating(false); } }; const refreshPaymentStatus = async () => { if (!featureAccount || !rechargeOrder) return; setPaymentStatusLoading(true); try { const status = await weiboApi.rechargeStatus( featureAccount.id, rechargeOrder.pay_id, ); setPaymentStatus(status); } catch (error) { message.error(getErrorMessage(error)); } finally { setPaymentStatusLoading(false); } }; const refreshAfterRecharge = async () => { if (!featureAccount) return; await handleRefresh(featureAccount); await loadPointHistory(featureAccount); }; const columns: TableProps['columns'] = [ { title: '微博账号', key: 'account', width: 220, render: (_, account) => ( {account.nickname || account.uid} UID {account.uid} ), }, { title: '标签', dataIndex: 'tag', width: 120, render: (value: string) => value || -, }, { title: 'CK', key: 'ck', width: 120, render: (_, account) => { const state = CK_STATUS[account.ck_check_status] || { label: '未检测', color: 'default', }; return ( {state.label} ); }, }, { title: '角色绑定', key: 'bind', width: 150, render: (_, account) => ( {bindStatus(account)} {account.game_role_name && ( {account.game_role_name} )} ), }, { title: '微豆', dataIndex: 'weidou_balance', width: 100, align: 'right', render: (value: number | null) => value == null ? - : value.toLocaleString(), }, { title: '最近检查', dataIndex: 'ck_checked_at', width: 170, render: (value: string | null) => value ? formatTime(value) : -, }, { title: '操作', key: 'actions', width: 390, fixed: 'right', render: (_, account) => { const cannotBind = account.is_bound === true && account.can_rebind !== true; return ( {canDelete && ( void handleDelete(account)} > ); }, }, ]; const exchangeColumns: TableProps< WeiboExchangeRecordsResult['items'][number] >['columns'] = [ { title: '商品', dataIndex: 'goods_name' }, { title: '游戏角色', dataIndex: 'game_account', width: 180 }, { title: '微豆', dataIndex: 'cost_weidou', width: 100, align: 'right', render: (value: number | null) => value ?? '-', }, { title: '兑换时间', dataIndex: 'exchange_time', width: 170 }, { title: '状态', dataIndex: 'reward_status', width: 100, render: (value: number | null) => value ?? '-', }, ]; const pointColumns: TableProps< WeiboPointHistoryResult['items'][number] >['columns'] = [ { title: '来源', dataIndex: 'source_name', width: 150 }, { title: '微豆', dataIndex: 'point_num', width: 100, align: 'right', render: (value: number) => ( = 0 ? 'success' : 'danger'}> {value > 0 ? `+${value}` : value} ), }, { title: '到账时间', dataIndex: 'create_time', width: 170 }, { title: '过期时间', dataIndex: 'expire_time', width: 170 }, { title: '订单号', dataIndex: 'source_id' }, ]; return (
微博账号 CK 导入、微豆资料刷新和角色绑定二维码
} placeholder="UID、昵称、角色、标签" value={search} onChange={(event) => { setSearch(event.target.value); setPage(1); }} style={{ width: 240 }} />