优化列表分页和数据加载
This commit is contained in:
@@ -4,7 +4,7 @@ import {
|
||||
} from 'antd';
|
||||
import { message } from '../utils/antdMessage';
|
||||
import { CopyOutlined, DeleteOutlined, DownloadOutlined, SearchOutlined } from '@ant-design/icons';
|
||||
import { huyaApi, type HuyaCookieItem } from '../api/modules';
|
||||
import { huyaApi, type BasicSummary, type HuyaCookieItem } from '../api/modules';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { formatTime } from '../utils/time';
|
||||
import { getErrorMessage } from '../utils/error';
|
||||
@@ -40,6 +40,8 @@ export default function HuyaCookiePage() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
|
||||
const [searchText, setSearchText] = useState('');
|
||||
const [total, setTotal] = useState(0);
|
||||
const [summary, setSummary] = useState<BasicSummary>({ total: 0, assigned_count: 0, unassigned_count: 0 });
|
||||
const [pageSize, setPageSize] = useState(() => {
|
||||
const value = localStorage.getItem('huya_cookie_page_size');
|
||||
return value ? Number(value) || 20 : 20;
|
||||
@@ -54,19 +56,38 @@ export default function HuyaCookiePage() {
|
||||
const loadCookies = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await huyaApi.listCookies();
|
||||
setCookies(data);
|
||||
const data = await huyaApi.listCookiesPaged({
|
||||
page: currentPage,
|
||||
page_size: pageSize,
|
||||
search: searchText.trim() || undefined,
|
||||
include_cookie: false,
|
||||
});
|
||||
setCookies(data.items);
|
||||
setTotal(data.total);
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [currentPage, pageSize, searchText]);
|
||||
|
||||
const loadSummary = useCallback(async () => {
|
||||
try {
|
||||
const data = await huyaApi.cookiesSummary();
|
||||
setSummary(data);
|
||||
} catch {
|
||||
// 统计加载失败时不影响主列表操作。
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
loadCookies();
|
||||
}, [loadCookies]);
|
||||
|
||||
useEffect(() => {
|
||||
loadSummary();
|
||||
}, [loadSummary]);
|
||||
|
||||
const handleExport = async (format: string = 'csv') => {
|
||||
try {
|
||||
const blob = await huyaApi.exportCookies(format);
|
||||
@@ -82,31 +103,34 @@ export default function HuyaCookiePage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleCopyCookie = (record: HuyaCookieItem) => {
|
||||
if (!record.cookie) {
|
||||
message.warning('Cookie 为空');
|
||||
return;
|
||||
const handleCopyCookie = async (record: HuyaCookieItem) => {
|
||||
try {
|
||||
const detail = await huyaApi.getCookie(record.id);
|
||||
if (!detail.cookie) {
|
||||
message.warning('Cookie 为空');
|
||||
return;
|
||||
}
|
||||
const text = `${detail.account_username}----${detail.cookie}`;
|
||||
await copyToClipboard(text);
|
||||
message.success(`已复制 ${detail.account_username} 的 Cookie`);
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e) || '复制失败');
|
||||
}
|
||||
const text = `${record.account_username}----${record.cookie}`;
|
||||
copyToClipboard(text).then(() => {
|
||||
message.success(`已复制 ${record.account_username} 的 Cookie`);
|
||||
}).catch(() => {
|
||||
message.error('复制失败');
|
||||
});
|
||||
};
|
||||
|
||||
const handleCopySelected = () => {
|
||||
const handleCopySelected = async () => {
|
||||
if (selectedRowKeys.length === 0) {
|
||||
message.warning('请先选择 Cookie');
|
||||
return;
|
||||
}
|
||||
const selected = cookies.filter((item) => selectedRowKeys.includes(item.id));
|
||||
const text = selected.map((item) => `${item.account_username}----${item.cookie || ''}`).join('\r\n');
|
||||
copyToClipboard(text).then(() => {
|
||||
try {
|
||||
const selected = await Promise.all(selectedRowKeys.map((key) => huyaApi.getCookie(Number(key))));
|
||||
const text = selected.map((item) => `${item.account_username}----${item.cookie || ''}`).join('\r\n');
|
||||
await copyToClipboard(text);
|
||||
message.success(`已复制 ${selected.length} 条 Cookie`);
|
||||
}).catch(() => {
|
||||
message.error('复制失败');
|
||||
});
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e) || '复制失败');
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id: number) => {
|
||||
@@ -115,6 +139,7 @@ export default function HuyaCookiePage() {
|
||||
message.success('已清除');
|
||||
setSelectedRowKeys((prev) => prev.filter((key) => key !== id));
|
||||
loadCookies();
|
||||
loadSummary();
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
}
|
||||
@@ -130,22 +155,12 @@ export default function HuyaCookiePage() {
|
||||
message.success(result.message);
|
||||
setSelectedRowKeys([]);
|
||||
loadCookies();
|
||||
loadSummary();
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
}
|
||||
};
|
||||
|
||||
const filteredCookies = cookies.filter((item) => {
|
||||
if (!searchText) return true;
|
||||
const search = searchText.toLowerCase();
|
||||
return (
|
||||
item.account_username.toLowerCase().includes(search) ||
|
||||
item.uid.toLowerCase().includes(search) ||
|
||||
item.yyuid.toLowerCase().includes(search) ||
|
||||
(item.assigned_username || '').toLowerCase().includes(search)
|
||||
);
|
||||
});
|
||||
|
||||
const columns = [
|
||||
{ title: 'ID', dataIndex: 'id', width: 70, align: 'center' as const },
|
||||
{
|
||||
@@ -204,9 +219,6 @@ export default function HuyaCookiePage() {
|
||||
},
|
||||
];
|
||||
|
||||
const assignedCount = cookies.filter((item) => item.assigned_to).length;
|
||||
const unassignedCount = cookies.length - assignedCount;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
@@ -245,16 +257,16 @@ export default function HuyaCookiePage() {
|
||||
|
||||
<Row gutter={12} style={{ marginBottom: 16 }}>
|
||||
<Col span={6}>
|
||||
<Card size="small"><Statistic title="Cookie 总数" value={cookies.length} /></Card>
|
||||
<Card size="small"><Statistic title="Cookie 总数" value={summary.total} /></Card>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Card size="small">
|
||||
<Statistic title="已分配" value={assignedCount} styles={{ content: { color: token.colorSuccess } }} />
|
||||
<Statistic title="已分配" value={summary.assigned_count} styles={{ content: { color: token.colorSuccess } }} />
|
||||
</Card>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Card size="small">
|
||||
<Statistic title="未分配" value={unassignedCount} styles={{ content: { color: token.colorError } }} />
|
||||
<Statistic title="未分配" value={summary.unassigned_count} styles={{ content: { color: token.colorError } }} />
|
||||
</Card>
|
||||
</Col>
|
||||
</Row>
|
||||
@@ -264,7 +276,10 @@ export default function HuyaCookiePage() {
|
||||
placeholder="搜索账号、UID 或分配客服"
|
||||
allowClear
|
||||
value={searchText}
|
||||
onChange={(e) => setSearchText(e.target.value)}
|
||||
onChange={(e) => {
|
||||
setSearchText(e.target.value);
|
||||
setCurrentPage(1);
|
||||
}}
|
||||
style={{ width: 300 }}
|
||||
size="small"
|
||||
prefix={<SearchOutlined />}
|
||||
@@ -275,15 +290,17 @@ export default function HuyaCookiePage() {
|
||||
rowSelection={{
|
||||
selectedRowKeys,
|
||||
onChange: (keys) => setSelectedRowKeys(keys),
|
||||
preserveSelectedRowKeys: true,
|
||||
}}
|
||||
columns={columns}
|
||||
dataSource={filteredCookies}
|
||||
dataSource={cookies}
|
||||
rowKey="id"
|
||||
loading={loading}
|
||||
size="small"
|
||||
pagination={{
|
||||
current: currentPage,
|
||||
pageSize,
|
||||
total,
|
||||
showSizeChanger: true,
|
||||
showTotal: (total) => `共 ${total} 条`,
|
||||
onChange: (page, size) => {
|
||||
|
||||
Reference in New Issue
Block a user