import { useCallback, useEffect, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { Button, Card, Col, Descriptions, Divider, Empty, Input, Popconfirm, Row, Select, Space, Spin, Statistic, Table, Tag, Tooltip, Typography, } from 'antd'; import type { TableProps } from 'antd'; import { ArrowLeftOutlined, EditOutlined, ReloadOutlined, ThunderboltOutlined, } from '@ant-design/icons'; import { accountApi, douyuApi, userApi, type AccountEventItem, type AccountOverview, type UserInfo } from '../api/modules'; import AccountOverviewPanel from './douyu-tasks/AccountOverviewPanel'; import { getErrorMessage } from '../utils/error'; import { message } from '../utils/antdMessage'; import { formatTime } from '../utils/time'; const { Text, Title } = Typography; /** * 事件类型 → 展示样式。 * 颜色用来一眼区分「钱进来 / 分出去 / 状态变化」三类,而不是按类型各给一个颜色。 */ const EVENT_STYLE: Record = { imported: { label: '导入', color: 'default' }, login_ok: { label: '登录', color: 'blue' }, ck_invalid: { label: 'CK 失效', color: 'red' }, ck_cleared: { label: 'CK 已清除', color: 'red' }, relogin_ok: { label: '重登成功', color: 'green' }, relogin_failed: { label: '重登转人工', color: 'orange' }, status_changed: { label: '状态变更', color: 'geekblue' }, bind_ok: { label: '绑定', color: 'cyan' }, recharge: { label: '充值', color: 'green' }, exchange: { label: '兑换', color: 'purple' }, points_credit: { label: '积分到账', color: 'gold' }, elite_opened: { label: '精英宝典', color: 'magenta' }, esports_opened: { label: '电竞手册', color: 'volcano' }, assigned: { label: '分配', color: 'default' }, tag_changed: { label: '标签', color: 'default' }, remark_changed: { label: '备注', color: 'default' }, }; /** 事件粗分类(与后端 `account_events_service.CATEGORY_*` 对应)。 */ const CATEGORY_OPTIONS = [ { value: 'lifecycle', label: '状态变迁' }, { value: 'auth', label: '登录与 CK' }, { value: 'business', label: '业务流水' }, { value: 'ops', label: '人工操作' }, ]; /** 生命周期状态 → 展示色(与账号列表页保持一致,取值域见后端 account_lifecycle)。 */ const STATUS_TONES: Record = { imported: 'default', active: 'green', invalid: 'red', manual: 'orange', disabled: 'default', archived: 'default', }; /** 绑定状态 → 展示色(与后端 account_binding 的四个枚举对应)。 */ const BIND_TONES: Record = { bound: 'green', pending: 'orange', unbound: 'red', unknown: 'default', }; function eventTag(type: string) { const style = EVENT_STYLE[type] ?? { label: type, color: 'default' }; return {style.label}; } /** 金额展示:充值为正(绿色 ¥),兑换消耗为负(红色积分)。 */ function amountText(item: AccountEventItem) { if (item.amount === null || item.amount === undefined) return null; if (item.event_type === 'recharge') { return +¥{item.amount}; } if (item.event_type === 'exchange') { return {item.amount} 分; } return ( = 0 ? 'success' : 'danger'}> {item.amount > 0 ? '+' : ''} {item.amount} ); } export default function AccountDetailPage() { const { id } = useParams<{ id: string }>(); const accountId = Number(id); const navigate = useNavigate(); const [overview, setOverview] = useState(null); const [loading, setLoading] = useState(true); const [events, setEvents] = useState([]); const [eventTotal, setEventTotal] = useState(0); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(20); const [eventType, setEventType] = useState(''); const [category, setCategory] = useState(''); const [eventsLoading, setEventsLoading] = useState(false); const [tagDraft, setTagDraft] = useState(''); // 一键查询:向斗鱼实时查一次该账号的全部关键信息(异步任务 + 轮询结果) const [ovLoading, setOvLoading] = useState(false); const [ovError, setOvError] = useState(''); const [tagEditing, setTagEditing] = useState(false); const [supportUsers, setSupportUsers] = useState([]); const [remarkEditing, setRemarkEditing] = useState(false); const [remarkDraft, setRemarkDraft] = useState(''); /** 加载客服列表(分配用)。只在需要时拉一次。 */ useEffect(() => { let alive = true; void (async () => { try { const users = await userApi.list(); if (alive) setSupportUsers((users || []).filter((u) => u.role === 'support')); } catch { // 拉不到客服列表不影响页面其它部分 } })(); return () => { alive = false; }; }, []); // 一键查询:建一个只读聚合任务 → 轮询**账号自身**直到快照更新。 // // 结果写在账号上(`overview_snapshot`),所以轮询账号就够了。 // // ⚠️ 轮询必须**能自证在推进**。之前的 bug:详情接口忘了返回 // `overview_queried_at`(字段加到了列表接口上),于是比较永远相等、 // 轮询满 120 秒 —— 用户看到的就是"点一下就一直转圈刷请求"。 // 所以这里加了:拿不到标记字段就**立刻报错退出**,而不是傻等。 const runOverview = useCallback(async () => { if (!id) return; const accountId = Number(id); const before = overview?.overview_queried_at ?? ''; setOvLoading(true); setOvError(''); try { await douyuApi.createTasks({ account_ids: [accountId], task_type: 'query_account_overview', handbook_scope: 'elite', }); const deadline = Date.now() + 120_000; let first = true; while (Date.now() < deadline) { await new Promise((resolve) => setTimeout(resolve, 2000)); const fresh = await accountApi.overview(accountId); const marker = fresh.overview_queried_at; if (first && marker === undefined) { // 接口没返回标记字段 —— 再怎么轮询也不可能变化,直接如实报错 setOvError('接口未返回查询时间标记,无法判断是否完成(请检查后端详情接口)'); return; } first = false; if ((marker ?? '') !== before) { setOverview(fresh); message.success('查询完成'); return; } } setOvError('查询超时(120 秒未出结果),稍后刷新页面看看'); } catch (error) { setOvError(getErrorMessage(error)); } finally { setOvLoading(false); } }, [id, overview?.overview_queried_at]); const loadOverview = useCallback(async () => { if (!accountId) return; setLoading(true); try { setOverview(await accountApi.overview(accountId)); } catch (e: unknown) { message.error(getErrorMessage(e)); } finally { setLoading(false); } }, [accountId]); const loadEvents = useCallback(async () => { if (!accountId) return; setEventsLoading(true); try { const data = await accountApi.events(accountId, { page, page_size: pageSize, event_type: eventType, category, }); setEvents(data.items); setEventTotal(data.total); } catch (e: unknown) { message.error(getErrorMessage(e)); } finally { setEventsLoading(false); } }, [accountId, page, pageSize, eventType, category]); useEffect(() => { void loadOverview(); }, [loadOverview]); useEffect(() => { void loadEvents(); }, [loadEvents]); const columns: TableProps['columns'] = [ { title: '时间', dataIndex: 'occurred_at', width: 165, render: (val: string) => formatTime(val), }, { title: '类型', dataIndex: 'event_type', width: 100, render: (val: string) => eventTag(val), }, { title: '说明', dataIndex: 'title', ellipsis: true }, { title: '金额 / 变动', dataIndex: 'amount', width: 110, align: 'right', render: (_val, record) => amountText(record) ?? -, }, { title: '变动后余额', dataIndex: 'balance_after', width: 110, align: 'right', render: (val: number | null) => val === null || val === undefined ? - : val, }, ]; if (!accountId) { return ; } const stats = overview?.stats; const status = overview?.status || ''; /** 人工切换生命周期状态:disabled / archived / auto(恢复自动推导)。 */ const handleLifecycle = async (next: 'disabled' | 'archived' | 'auto') => { if (!accountId) return; try { await accountApi.setLifecycle(accountId, next); message.success('状态已更新'); void loadOverview(); void loadEvents(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; const handleAssign = async (assignedTo: number | null) => { if (!accountId) return; try { await accountApi.assign(accountId, assignedTo); message.success(assignedTo ? '已分配' : '已取消分配'); void loadOverview(); void loadEvents(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; const handleSaveTag = async () => { if (!accountId) return; try { await accountApi.setTag(accountId, tagDraft.trim()); message.success('标签已更新'); setTagEditing(false); void loadOverview(); void loadEvents(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; /** 保存备注:备注是自由文本,走单个接口(标签那种短枚举才有批量)。 */ const handleSaveRemark = async () => { if (!accountId) return; try { await accountApi.setRemark(accountId, remarkDraft.trim()); message.success('备注已更新'); setRemarkEditing(false); void loadOverview(); void loadEvents(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; const handleAutomation = async (autoManaged: boolean) => { if (!accountId) return; try { await accountApi.setAutomation(accountId, autoManaged); message.success(autoManaged ? '已恢复自动化' : '已设为不参与自动化'); void loadOverview(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; return (
{status === 'disabled' || status === 'archived' ? ( void handleLifecycle('auto')} > ) : ( <> void handleLifecycle('disabled')} > void handleLifecycle('archived')} > )} {overview?.auto_managed === false ? ( ) : ( void handleAutomation(false)} > )} {/* ── 卡片 1:账号信息 ────────────────────────────── 身份 + 状态 + 归属,一张表看完。**不重复展示资产**(见卡片 2)。 */} {overview?.nickname || overview?.username || '—'} #{overview?.id ?? '-'} · {overview?.username || '—'} {overview?.status_label || '—'} {overview?.tag && {overview.tag}} {overview?.auto_managed === false && 不参与自动化} } > {overview?.uid || '-'} {overview?.game_name || '-'} {overview?.game_channel ? ( ({overview.game_channel}) ) : null} {formatTime(overview?.created_at)} {formatTime(overview?.status_changed_at)} {overview?.status_reason ? ( ({overview.status_reason}) ) : null} {overview?.has_cookie === false ? ( 无 CK ) : ( {overview?.ck_check_status === 'invalid' ? '已失效' : overview?.ck_check_status === 'valid' ? '正常' : '未检测'} )} {formatTime(stats?.last_ck_check_at)} {/* 三个作用域各一列,取值 unknown/unbound/pending/bound */} 精英:{overview?.role_bind_label || '未知'} 电竞:{overview?.esports_bind_label || '未知'} 小店:{overview?.xpd_bind_label || '未知'} setTagDraft(e.target.value)} onPressEnter={() => void handleSaveTag()} /> ) : ( {overview?.tag || '-'} ) : ( {overview?.remark || '-'} } > {ovError && ( {ovError} )} {overview?.overview_snapshot ? ( ) : ( 点右上角「一键查询」实时向斗鱼查一次:鱼翅 / 积分 / 开通状态会就地刷新到上面的 「资产与统计」,这里只展示账号表里没有的两项 —— 高价值套装是否已获得,以及官方兑换记录。 )} { setEventType(v); setPage(1); }} options={[ { value: '', label: '全部事件' }, ...Object.entries(EVENT_STYLE).map(([value, s]) => ({ value, label: s.label })), ]} /> 共 {eventTotal} 条 } > `共 ${t} 条`, onChange: (p, s) => { setPage(p); setPageSize(s); }, }} locale={{ emptyText: }} /> ); }