- 前端改造为服务端分页查询 (accountApi.listPaged),避免一次性拉取 1.6 万+ 账号导致页面卡死 - 优化后端 assignments_summary 聚合查询,消除大子查询临时表 - 修复 Badge 默认 99+ 限制,设置 overflowCount 支持展示完整账号数量 - 修复测试用例 test_business_events.py 当前时间跨天断言问题
384 lines
14 KiB
TypeScript
384 lines
14 KiB
TypeScript
import { useEffect, useMemo, useState } from 'react';
|
|
import {
|
|
Badge, Button, Card, Col, Input, Row, Select, Space, Statistic, Table, Tabs, Tag, theme, Typography,
|
|
} from 'antd';
|
|
import { message } from '../utils/antdMessage';
|
|
import {
|
|
CheckCircleOutlined, ClearOutlined, SwapOutlined, TeamOutlined, UsergroupAddOutlined, UserOutlined,
|
|
} from '@ant-design/icons';
|
|
import { huyaApi, type HuyaAccountItem, type SupportUserItem } from '../api/modules';
|
|
import { getErrorMessage } from '../utils/error';
|
|
|
|
const { Text } = Typography;
|
|
const STORAGE_KEY_SELECTED_USER = 'huya_assignments_selected_user_id';
|
|
|
|
function accountName(account: HuyaAccountItem) {
|
|
return account.nickname || account.username || account.uid || `#${account.id}`;
|
|
}
|
|
|
|
export default function HuyaAssignmentsPage() {
|
|
const { token } = theme.useToken();
|
|
const [accounts, setAccounts] = useState<HuyaAccountItem[]>([]);
|
|
const [supportUsers, setSupportUsers] = useState<SupportUserItem[]>([]);
|
|
const [selectedUser, setSelectedUser] = useState<SupportUserItem | null>(null);
|
|
const [selectedRowKeys, setSelectedRowKeys] = useState<number[]>([]);
|
|
const [searchText, setSearchText] = useState('');
|
|
const [filterTag, setFilterTag] = useState<string | undefined>(undefined);
|
|
const [activeTab, setActiveTab] = useState('unassigned');
|
|
const [assigning, setAssigning] = useState(false);
|
|
const [pageSize, setPageSize] = useState(() => {
|
|
const value = localStorage.getItem('huya_assignment_page_size');
|
|
return value ? Number(value) || 15 : 15;
|
|
});
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
|
|
const loadSummary = async () => {
|
|
try {
|
|
const data = await huyaApi.assignmentsSummary();
|
|
setSupportUsers(data.support_users);
|
|
return data.support_users;
|
|
} catch (e: unknown) {
|
|
message.error(getErrorMessage(e));
|
|
return [];
|
|
}
|
|
};
|
|
|
|
const loadAccounts = async () => {
|
|
try {
|
|
const data = await huyaApi.listAccounts({ has_cookie: true, include_cookie: false });
|
|
setAccounts(data);
|
|
} catch (e: unknown) {
|
|
message.error(getErrorMessage(e));
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const init = async () => {
|
|
void loadAccounts();
|
|
const users = await loadSummary();
|
|
if (users.length > 0) {
|
|
const savedId = localStorage.getItem(STORAGE_KEY_SELECTED_USER);
|
|
const savedUser = savedId ? users.find((user) => user.id === Number(savedId)) : null;
|
|
setSelectedUser(savedUser || users[0]);
|
|
}
|
|
};
|
|
void init();
|
|
}, []);
|
|
|
|
const allTags = useMemo(() => {
|
|
return [...new Set(accounts.map((account) => (account.tag || '').trim()).filter(Boolean))].sort();
|
|
}, [accounts]);
|
|
|
|
const unassignedAccounts = useMemo(() => accounts.filter((account) => !account.assigned_to), [accounts]);
|
|
|
|
const assignedAccounts = useMemo(() => {
|
|
if (!selectedUser) return [];
|
|
return accounts.filter((account) => account.assigned_to === selectedUser.id);
|
|
}, [accounts, selectedUser]);
|
|
|
|
const displayAccounts = useMemo(() => {
|
|
let list = activeTab === 'unassigned' ? unassignedAccounts : assignedAccounts;
|
|
if (searchText) {
|
|
const search = searchText.toLowerCase();
|
|
list = list.filter((account) => (
|
|
accountName(account).toLowerCase().includes(search) ||
|
|
account.uid.toLowerCase().includes(search) ||
|
|
account.yyuid.toLowerCase().includes(search) ||
|
|
account.game_phone.toLowerCase().includes(search)
|
|
));
|
|
}
|
|
if (filterTag) {
|
|
list = list.filter((account) => (account.tag || '').trim() === filterTag);
|
|
}
|
|
return list;
|
|
}, [activeTab, assignedAccounts, filterTag, searchText, unassignedAccounts]);
|
|
|
|
const handleSelectUser = (user: SupportUserItem) => {
|
|
setSelectedUser(user);
|
|
setSelectedRowKeys([]);
|
|
setActiveTab('unassigned');
|
|
setSearchText('');
|
|
setFilterTag(undefined);
|
|
localStorage.setItem(STORAGE_KEY_SELECTED_USER, String(user.id));
|
|
};
|
|
|
|
const refreshAfterAssign = async () => {
|
|
await loadAccounts();
|
|
const users = await loadSummary();
|
|
const refreshed = users.find((user) => user.id === selectedUser?.id);
|
|
if (refreshed) setSelectedUser(refreshed);
|
|
};
|
|
|
|
const handleBatchAssign = async () => {
|
|
if (selectedRowKeys.length === 0) {
|
|
message.warning('请先选择虎牙账号');
|
|
return;
|
|
}
|
|
if (!selectedUser) {
|
|
message.warning('请先选择客服');
|
|
return;
|
|
}
|
|
setAssigning(true);
|
|
try {
|
|
await huyaApi.batchAssign(selectedRowKeys, selectedUser.id);
|
|
message.success(`已将 ${selectedRowKeys.length} 个虎牙账号分配给 ${selectedUser.username}`);
|
|
setSelectedRowKeys([]);
|
|
await refreshAfterAssign();
|
|
} catch (e: unknown) {
|
|
message.error(getErrorMessage(e));
|
|
} finally {
|
|
setAssigning(false);
|
|
}
|
|
};
|
|
|
|
const handleBatchUnassign = async () => {
|
|
if (selectedRowKeys.length === 0) {
|
|
message.warning('请先选择虎牙账号');
|
|
return;
|
|
}
|
|
setAssigning(true);
|
|
try {
|
|
await huyaApi.batchAssign(selectedRowKeys, null);
|
|
message.success(`已取消 ${selectedRowKeys.length} 个虎牙账号的分配`);
|
|
setSelectedRowKeys([]);
|
|
await refreshAfterAssign();
|
|
} catch (e: unknown) {
|
|
message.error(getErrorMessage(e));
|
|
} finally {
|
|
setAssigning(false);
|
|
}
|
|
};
|
|
|
|
const handleAssignAllUnassigned = async () => {
|
|
if (!selectedUser) {
|
|
message.warning('请先选择客服');
|
|
return;
|
|
}
|
|
const ids = displayAccounts.map((account) => account.id);
|
|
if (ids.length === 0) {
|
|
message.info('没有未分配的虎牙账号');
|
|
return;
|
|
}
|
|
setAssigning(true);
|
|
try {
|
|
await huyaApi.batchAssign(ids, selectedUser.id);
|
|
message.success(`已将 ${ids.length} 个虎牙账号分配给 ${selectedUser.username}`);
|
|
await refreshAfterAssign();
|
|
} catch (e: unknown) {
|
|
message.error(getErrorMessage(e));
|
|
} finally {
|
|
setAssigning(false);
|
|
}
|
|
};
|
|
|
|
const assignedCount = accounts.filter((account) => !!account.assigned_to).length;
|
|
const unassignedCount = accounts.length - assignedCount;
|
|
|
|
const columns = [
|
|
{ title: 'ID', dataIndex: 'id', width: 70 },
|
|
{
|
|
title: '虎牙账号',
|
|
render: (_: unknown, record: HuyaAccountItem) => (
|
|
<Space orientation="vertical" size={0}>
|
|
<Text strong>{accountName(record)}</Text>
|
|
<Text type="secondary" style={{ fontSize: 12 }}>UID {record.uid || record.yyuid || '-'}</Text>
|
|
</Space>
|
|
),
|
|
},
|
|
{
|
|
title: '标签',
|
|
dataIndex: 'tag',
|
|
width: 100,
|
|
render: (tag: string) => tag ? <Tag color="blue">{tag}</Tag> : <Text type="secondary">-</Text>,
|
|
},
|
|
{
|
|
title: '手机号',
|
|
dataIndex: 'game_phone',
|
|
width: 130,
|
|
render: (value: string) => value || <Text type="secondary">-</Text>,
|
|
},
|
|
];
|
|
|
|
return (
|
|
<div style={{ height: '100%', display: 'flex', flexDirection: 'column', gap: 12 }}>
|
|
<h2 style={{ margin: 0, flexShrink: 0 }}>虎牙分配管理</h2>
|
|
|
|
<Row gutter={12} style={{ flexShrink: 0 }}>
|
|
<Col span={6}>
|
|
<Card size="small"><Statistic title="虎牙账号" value={accounts.length} prefix={<TeamOutlined />} /></Card>
|
|
</Col>
|
|
<Col span={6}>
|
|
<Card size="small">
|
|
<Statistic title="已分配" value={assignedCount} styles={{ content: { color: token.colorSuccess } }} prefix={<CheckCircleOutlined />} />
|
|
</Card>
|
|
</Col>
|
|
<Col span={6}>
|
|
<Card size="small">
|
|
<Statistic title="未分配" value={unassignedCount} styles={{ content: { color: token.colorError } }} prefix={<SwapOutlined />} />
|
|
</Card>
|
|
</Col>
|
|
<Col span={6}>
|
|
<Card size="small"><Statistic title="客服人数" value={supportUsers.length} prefix={<UsergroupAddOutlined />} /></Card>
|
|
</Col>
|
|
</Row>
|
|
|
|
<Row gutter={12} style={{ flex: 1, minHeight: 0 }}>
|
|
<Col span={8} style={{ height: '100%', display: 'flex', flexDirection: 'column', minHeight: 0 }}>
|
|
<Card
|
|
title="客服列表"
|
|
size="small"
|
|
style={{ flex: 1, display: 'flex', flexDirection: 'column', minHeight: 0 }}
|
|
styles={{ body: { flex: 1, overflow: 'auto', padding: 8 } }}
|
|
>
|
|
<div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
|
|
{supportUsers.map((user) => {
|
|
const isSelected = selectedUser?.id === user.id;
|
|
return (
|
|
<div
|
|
key={user.id}
|
|
onClick={() => handleSelectUser(user)}
|
|
style={{
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
padding: '10px 14px',
|
|
borderRadius: 6,
|
|
cursor: 'pointer',
|
|
border: `1px solid ${isSelected ? token.colorPrimary : token.colorBorderSecondary}`,
|
|
background: isSelected ? token.colorPrimaryBg : token.colorBgContainer,
|
|
transition: 'all 0.2s',
|
|
}}
|
|
>
|
|
<Space>
|
|
<UserOutlined style={{ color: token.colorPrimary }} />
|
|
<Text strong>{user.username}</Text>
|
|
</Space>
|
|
<Badge count={user.assigned_count} overflowCount={999999} showZero style={{ backgroundColor: token.colorPrimary }} />
|
|
</div>
|
|
);
|
|
})}
|
|
{supportUsers.length === 0 && (
|
|
<Text type="secondary" style={{ textAlign: 'center', padding: 20, display: 'block' }}>
|
|
暂无客服用户
|
|
</Text>
|
|
)}
|
|
</div>
|
|
</Card>
|
|
</Col>
|
|
|
|
<Col span={16} style={{ height: '100%', display: 'flex', flexDirection: 'column', minHeight: 0 }}>
|
|
<Card
|
|
size="small"
|
|
title={selectedUser ? (
|
|
<Space>
|
|
<span>已选中客服:</span>
|
|
<Tag color="blue" style={{ fontSize: 14, padding: '2px 10px' }}>
|
|
{selectedUser.username}(已分配 {selectedUser.assigned_count} 个)
|
|
</Tag>
|
|
</Space>
|
|
) : <Text type="secondary">请选择左侧客服进行操作</Text>}
|
|
style={{ flex: 1, display: 'flex', flexDirection: 'column', minHeight: 0 }}
|
|
styles={{ body: { flex: 1, display: 'flex', flexDirection: 'column', minHeight: 0, padding: 0 } }}
|
|
>
|
|
<div style={{ padding: '8px 16px', borderBottom: `1px solid ${token.colorBorderSecondary}`, display: 'flex', gap: 8, alignItems: 'center', flexShrink: 0 }}>
|
|
<Input.Search
|
|
placeholder="搜索昵称、UID、手机号"
|
|
allowClear
|
|
value={searchText}
|
|
onChange={(e) => setSearchText(e.target.value)}
|
|
style={{ width: 220 }}
|
|
size="small"
|
|
/>
|
|
<Select
|
|
allowClear
|
|
placeholder="按标签筛选"
|
|
value={filterTag}
|
|
onChange={(value) => setFilterTag(value || undefined)}
|
|
style={{ width: 150 }}
|
|
size="small"
|
|
options={allTags.map((tag) => ({ value: tag, label: tag }))}
|
|
/>
|
|
</div>
|
|
|
|
<Tabs
|
|
size="small"
|
|
activeKey={activeTab}
|
|
onChange={(key) => { setActiveTab(key); setSelectedRowKeys([]); }}
|
|
style={{ padding: '0 16px', flexShrink: 0, marginBottom: 0 }}
|
|
items={[
|
|
{ key: 'unassigned', label: `未分配 (${unassignedAccounts.length})` },
|
|
{
|
|
key: 'assigned',
|
|
label: selectedUser ? `已分配给 ${selectedUser.username} (${assignedAccounts.length})` : '已分配账号',
|
|
disabled: !selectedUser,
|
|
},
|
|
]}
|
|
/>
|
|
|
|
<div style={{ padding: '0 16px 8px', flexShrink: 0 }}>
|
|
<Space>
|
|
{activeTab === 'unassigned' && selectedUser && (
|
|
<>
|
|
<Button
|
|
type="primary"
|
|
size="small"
|
|
icon={<SwapOutlined />}
|
|
loading={assigning}
|
|
disabled={selectedRowKeys.length === 0}
|
|
onClick={handleBatchAssign}
|
|
>
|
|
分配选中 ({selectedRowKeys.length}) 给 {selectedUser.username}
|
|
</Button>
|
|
<Button size="small" loading={assigning} onClick={handleAssignAllUnassigned}>
|
|
全部分配给 {selectedUser.username}
|
|
</Button>
|
|
</>
|
|
)}
|
|
{activeTab === 'assigned' && selectedUser && (
|
|
<Button
|
|
danger
|
|
size="small"
|
|
icon={<ClearOutlined />}
|
|
loading={assigning}
|
|
disabled={selectedRowKeys.length === 0}
|
|
onClick={handleBatchUnassign}
|
|
>
|
|
取消分配选中 ({selectedRowKeys.length})
|
|
</Button>
|
|
)}
|
|
</Space>
|
|
</div>
|
|
|
|
<Table
|
|
columns={columns}
|
|
dataSource={displayAccounts}
|
|
rowKey="id"
|
|
size="small"
|
|
pagination={{
|
|
current: currentPage,
|
|
pageSize,
|
|
size: 'small',
|
|
showSizeChanger: true,
|
|
showTotal: (total) => `共 ${total} 条`,
|
|
onChange: (page, size) => {
|
|
setCurrentPage(page);
|
|
if (size !== pageSize) {
|
|
setPageSize(size);
|
|
localStorage.setItem('huya_assignment_page_size', String(size));
|
|
setCurrentPage(1);
|
|
}
|
|
},
|
|
}}
|
|
rowSelection={{
|
|
selectedRowKeys,
|
|
onChange: (keys) => setSelectedRowKeys(keys as number[]),
|
|
}}
|
|
scroll={{ y: 'calc(100vh - 500px)' }}
|
|
/>
|
|
</Card>
|
|
</Col>
|
|
</Row>
|
|
</div>
|
|
);
|
|
}
|