优化列表分页和数据加载
This commit is contained in:
@@ -2,7 +2,7 @@ import { useEffect, useState, useCallback } from 'react';
|
||||
import { Table, Button, Card, Row, Col, Statistic, Tag, Popconfirm, Space, Typography, Input, theme, Dropdown } from 'antd';
|
||||
import { message } from '../utils/antdMessage';
|
||||
import { DownloadOutlined, DeleteOutlined, CopyOutlined, SearchOutlined } from '@ant-design/icons';
|
||||
import { cookieApi, type CookieItem } from '../api/modules';
|
||||
import { cookieApi, type BasicSummary, type CookieItem } from '../api/modules';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { formatTime } from '../utils/time';
|
||||
import { getErrorMessage } from '../utils/error';
|
||||
@@ -43,6 +43,8 @@ export default function CookiePage() {
|
||||
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 v = localStorage.getItem('cookie_page_size');
|
||||
return v ? Number(v) || 20 : 20;
|
||||
@@ -56,19 +58,38 @@ export default function CookiePage() {
|
||||
const loadCookies = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await cookieApi.list();
|
||||
setCookies(data);
|
||||
const data = await cookieApi.listPaged({
|
||||
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 cookieApi.summary();
|
||||
setSummary(data);
|
||||
} catch {
|
||||
// 统计加载失败不影响列表操作。
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
loadCookies();
|
||||
}, [loadCookies]);
|
||||
|
||||
useEffect(() => {
|
||||
loadSummary();
|
||||
}, [loadSummary]);
|
||||
|
||||
const handleExport = async (format: string = 'csv') => {
|
||||
try {
|
||||
const blob = await cookieApi.exportCsv(format);
|
||||
@@ -84,33 +105,36 @@ export default function CookiePage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleCopyCookie = (record: CookieItem) => {
|
||||
if (!record.cookie) {
|
||||
message.warning('Cookie 为空');
|
||||
return;
|
||||
const handleCopyCookie = async (record: CookieItem) => {
|
||||
try {
|
||||
const detail = await cookieApi.get(record.id);
|
||||
if (!detail.cookie) {
|
||||
message.warning('Cookie 为空');
|
||||
return;
|
||||
}
|
||||
const text = `${detail.account_username}----${detail.account_password || ''}----${detail.cookie}`;
|
||||
await copyToClipboard(text);
|
||||
message.success(`已复制 ${detail.account_username} 的 Cookie`);
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e) || '复制失败');
|
||||
}
|
||||
const text = `${record.account_username}----${record.account_password || ''}----${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((c) => selectedRowKeys.includes(c.id));
|
||||
const text = selected
|
||||
.map((c) => `${c.account_username}----${c.account_password || ''}----${c.cookie || ''}`)
|
||||
.join('\r\n');
|
||||
copyToClipboard(text).then(() => {
|
||||
try {
|
||||
const selected = await Promise.all(selectedRowKeys.map((key) => cookieApi.get(Number(key))));
|
||||
const text = selected
|
||||
.map((c) => `${c.account_username}----${c.account_password || ''}----${c.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) => {
|
||||
@@ -119,6 +143,7 @@ export default function CookiePage() {
|
||||
message.success('已删除');
|
||||
setSelectedRowKeys((prev) => prev.filter((k) => k !== id));
|
||||
loadCookies();
|
||||
loadSummary();
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
}
|
||||
@@ -135,21 +160,12 @@ export default function CookiePage() {
|
||||
message.success(res.message);
|
||||
setSelectedRowKeys([]);
|
||||
loadCookies();
|
||||
loadSummary();
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
}
|
||||
};
|
||||
|
||||
// 筛选
|
||||
const filteredCookies = cookies.filter((c) => {
|
||||
if (!searchText) return true;
|
||||
const s = searchText.toLowerCase();
|
||||
return (
|
||||
c.account_username?.toLowerCase().includes(s) ||
|
||||
c.assigned_username?.toLowerCase().includes(s)
|
||||
);
|
||||
});
|
||||
|
||||
const columns = [
|
||||
{ title: 'ID', dataIndex: 'id', width: 60, align: 'center' as const },
|
||||
{
|
||||
@@ -206,9 +222,6 @@ export default function CookiePage() {
|
||||
},
|
||||
];
|
||||
|
||||
const assignedCount = cookies.filter((c) => c.assigned_to).length;
|
||||
const unassignedCount = cookies.length - assignedCount;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
@@ -254,13 +267,13 @@ export default function CookiePage() {
|
||||
</div>
|
||||
<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}
|
||||
value={summary.assigned_count}
|
||||
styles={{ content: { color: token.colorSuccess } }}
|
||||
/>
|
||||
</Card>
|
||||
@@ -269,7 +282,7 @@ export default function CookiePage() {
|
||||
<Card size="small">
|
||||
<Statistic
|
||||
title="未分配"
|
||||
value={unassignedCount}
|
||||
value={summary.unassigned_count}
|
||||
styles={{ content: { color: token.colorError } }}
|
||||
/>
|
||||
</Card>
|
||||
@@ -280,7 +293,10 @@ export default function CookiePage() {
|
||||
placeholder="搜索账号或分配客服"
|
||||
allowClear
|
||||
value={searchText}
|
||||
onChange={(e) => setSearchText(e.target.value)}
|
||||
onChange={(e) => {
|
||||
setSearchText(e.target.value);
|
||||
setCurrentPage(1);
|
||||
}}
|
||||
style={{ width: 260 }}
|
||||
size="small"
|
||||
prefix={<SearchOutlined />}
|
||||
@@ -290,15 +306,17 @@ export default function CookiePage() {
|
||||
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: (t) => `共 ${t} 条`,
|
||||
onChange: (page, size) => {
|
||||
|
||||
Reference in New Issue
Block a user