313 lines
12 KiB
TypeScript
313 lines
12 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
||
import { Button, Input, Popconfirm, Select, Space, Table, Tag, Typography } from 'antd';
|
||
import { message } from '../utils/antdMessage';
|
||
import { LoadingOutlined, ReloadOutlined, SearchOutlined, StopOutlined } from '@ant-design/icons';
|
||
import { cookieApi, type CookieOperationCheckResult, type CookieOperationItem } from '../api/modules';
|
||
import { formatTime } from '../utils/time';
|
||
import { getErrorMessage } from '../utils/error';
|
||
|
||
const { Text } = Typography;
|
||
|
||
export default function CookieOperationsPage() {
|
||
const [items, setItems] = useState<CookieOperationItem[]>([]);
|
||
const [loading, setLoading] = useState(false);
|
||
const [search, setSearch] = useState('');
|
||
const [tagFilter, setTagFilter] = useState('');
|
||
const [tags, setTags] = useState<string[]>([]);
|
||
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
|
||
const [checkingIds, setCheckingIds] = useState<Set<number>>(new Set());
|
||
const [reloginIds, setReloginIds] = useState<Set<number>>(new Set());
|
||
const [checkResults, setCheckResults] = useState<Map<number, CookieOperationCheckResult>>(new Map());
|
||
const [total, setTotal] = useState(0);
|
||
const [pageSize, setPageSize] = useState(() => {
|
||
const value = localStorage.getItem('cookie_operations_page_size');
|
||
return value ? Number(value) || 20 : 20;
|
||
});
|
||
const [currentPage, setCurrentPage] = useState(1);
|
||
|
||
const loadItems = useCallback(async () => {
|
||
setLoading(true);
|
||
try {
|
||
const data = await cookieApi.listOperations({
|
||
page: currentPage,
|
||
page_size: pageSize,
|
||
search: search.trim() || undefined,
|
||
tag: tagFilter || undefined,
|
||
});
|
||
setItems(data.items);
|
||
setTotal(data.total);
|
||
setCheckResults((previous) => {
|
||
const next = new Map(previous);
|
||
for (const item of data.items) {
|
||
if (!item.ck_check_status) continue;
|
||
next.set(item.id, {
|
||
id: item.id,
|
||
valid: item.ck_check_status === 'valid',
|
||
checked_at: item.ck_checked_at ?? '',
|
||
});
|
||
}
|
||
return next;
|
||
});
|
||
} catch (error: unknown) {
|
||
message.error(getErrorMessage(error));
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
}, [currentPage, pageSize, search, tagFilter]);
|
||
|
||
useEffect(() => {
|
||
void loadItems();
|
||
}, [loadItems]);
|
||
|
||
const hasActiveRelogin = items.some((item) => (
|
||
item.relogin_status === 'relogin_pending' || item.relogin_status === 'relogin_running'
|
||
));
|
||
const activeReloginBatchIds = Array.from(new Set(items
|
||
.filter((item) => item.relogin_status === 'relogin_pending' || item.relogin_status === 'relogin_running')
|
||
.map((item) => item.relogin_batch_id)
|
||
.filter(Boolean)));
|
||
|
||
useEffect(() => {
|
||
if (!hasActiveRelogin) return;
|
||
const timer = window.setInterval(() => void loadItems(), 2000);
|
||
return () => window.clearInterval(timer);
|
||
}, [hasActiveRelogin, loadItems]);
|
||
|
||
useEffect(() => {
|
||
cookieApi.listOperationTags().then(setTags).catch(() => {
|
||
// 标签加载失败不影响 CK 检测与重登。
|
||
});
|
||
}, []);
|
||
|
||
const handleCheck = async (ids: number[]) => {
|
||
if (ids.length === 0) return;
|
||
setCheckingIds((previous) => new Set([...previous, ...ids]));
|
||
try {
|
||
const response = await cookieApi.checkOperations(ids);
|
||
setCheckResults((previous) => {
|
||
const next = new Map(previous);
|
||
response.results.forEach((item) => next.set(item.id, item));
|
||
return next;
|
||
});
|
||
message.success(`检测完成:${response.results.filter((item) => item.valid).length}/${response.results.length} 条有效`);
|
||
void loadItems();
|
||
} catch (error: unknown) {
|
||
message.error(getErrorMessage(error));
|
||
} finally {
|
||
setCheckingIds((previous) => {
|
||
const next = new Set(previous);
|
||
ids.forEach((id) => next.delete(id));
|
||
return next;
|
||
});
|
||
}
|
||
};
|
||
|
||
const handleRelogin = async (ids: number[]) => {
|
||
if (ids.length === 0) return;
|
||
setReloginIds((previous) => new Set([...previous, ...ids]));
|
||
try {
|
||
const response = await cookieApi.reloginOperations(ids);
|
||
const skipped = response.skipped ? `,${response.skipped} 条因缺少登录凭据跳过` : '';
|
||
message.success(`已开始重登 ${response.count} 个账号${skipped}`);
|
||
setSelectedRowKeys([]);
|
||
window.setTimeout(() => void loadItems(), 1000);
|
||
} catch (error: unknown) {
|
||
message.error(getErrorMessage(error));
|
||
} finally {
|
||
setReloginIds((previous) => {
|
||
const next = new Set(previous);
|
||
ids.forEach((id) => next.delete(id));
|
||
return next;
|
||
});
|
||
}
|
||
};
|
||
|
||
const handleReloginInvalid = async () => {
|
||
try {
|
||
const response = await cookieApi.reloginInvalidOperations({
|
||
search: search.trim() || undefined,
|
||
tag: tagFilter || undefined,
|
||
});
|
||
const skipped = response.skipped ? `,${response.skipped} 条因缺少登录凭据跳过` : '';
|
||
message.success(`已开始重登 ${response.count} 个失效账号${skipped}`);
|
||
setSelectedRowKeys([]);
|
||
void loadItems();
|
||
} catch (error: unknown) {
|
||
message.error(getErrorMessage(error));
|
||
}
|
||
};
|
||
|
||
const handleStopRelogin = async () => {
|
||
if (activeReloginBatchIds.length === 0) return;
|
||
try {
|
||
await Promise.all(activeReloginBatchIds.map((batchId) => cookieApi.stopOperationRelogin(batchId)));
|
||
message.success('已发送停止信号');
|
||
void loadItems();
|
||
} catch (error: unknown) {
|
||
message.error(getErrorMessage(error));
|
||
}
|
||
};
|
||
|
||
const resultFor = (item: CookieOperationItem) => checkResults.get(item.id);
|
||
const selectedIds = selectedRowKeys.map((key) => Number(key));
|
||
const reloginStatus = (item: CookieOperationItem) => {
|
||
if (item.relogin_status === 'relogin_pending') return <Tag color="processing">重登排队中</Tag>;
|
||
if (item.relogin_status === 'relogin_running') return <Tag color="processing" icon={<LoadingOutlined />}>重登中</Tag>;
|
||
if (item.relogin_status === 'relogin_failed') return <Tag color="error">重登失败</Tag>;
|
||
if (item.relogin_message.startsWith('重新登录成功')) return <Tag color="success">重登成功</Tag>;
|
||
return <Text type="secondary">-</Text>;
|
||
};
|
||
const columns = [
|
||
{ title: '账号', dataIndex: 'account_username', ellipsis: true },
|
||
{
|
||
title: '标签',
|
||
dataIndex: 'tag',
|
||
width: 140,
|
||
ellipsis: true,
|
||
render: (tag: string) => tag ? <Tag color="blue">{tag}</Tag> : <Text type="secondary">-</Text>,
|
||
},
|
||
{
|
||
title: '有效性',
|
||
width: 110,
|
||
render: (_: unknown, item: CookieOperationItem) => {
|
||
if (checkingIds.has(item.id)) return <Tag color="processing" icon={<LoadingOutlined />}>检测中</Tag>;
|
||
const result = resultFor(item);
|
||
if (!result) return <Text type="secondary">未检测</Text>;
|
||
return <Tag color={result.valid ? 'success' : 'error'}>{result.valid ? '有效' : '无效'}</Tag>;
|
||
},
|
||
},
|
||
{
|
||
title: '检测时间',
|
||
width: 180,
|
||
render: (_: unknown, item: CookieOperationItem) => {
|
||
const checkedAt = resultFor(item)?.checked_at || item.ck_checked_at;
|
||
return checkedAt ? formatTime(checkedAt) : <Text type="secondary">-</Text>;
|
||
},
|
||
},
|
||
{
|
||
title: '重登状态',
|
||
width: 130,
|
||
render: (_: unknown, item: CookieOperationItem) => (
|
||
<Space direction="vertical" size={0}>
|
||
{reloginStatus(item)}
|
||
{item.relogin_status === 'relogin_failed' && item.relogin_message ? (
|
||
<Text type="secondary" ellipsis style={{ maxWidth: 180, fontSize: 12 }} title={item.relogin_message}>
|
||
{item.relogin_message}
|
||
</Text>
|
||
) : null}
|
||
</Space>
|
||
),
|
||
},
|
||
{
|
||
title: '操作',
|
||
width: 190,
|
||
render: (_: unknown, item: CookieOperationItem) => (
|
||
<Space size={4}>
|
||
<Button
|
||
size="small"
|
||
loading={checkingIds.has(item.id)}
|
||
disabled={checkingIds.has(item.id) || item.relogin_status === 'relogin_pending' || item.relogin_status === 'relogin_running'}
|
||
onClick={() => void handleCheck([item.id])}
|
||
>
|
||
检测
|
||
</Button>
|
||
<Popconfirm title="将使用已保存的账号信息重新登录并替换 CK,确认继续?" onConfirm={() => void handleRelogin([item.id])}>
|
||
<Button
|
||
size="small"
|
||
icon={<ReloadOutlined />}
|
||
loading={reloginIds.has(item.id)}
|
||
disabled={reloginIds.has(item.id) || item.relogin_status === 'relogin_pending' || item.relogin_status === 'relogin_running'}
|
||
>
|
||
重登
|
||
</Button>
|
||
</Popconfirm>
|
||
</Space>
|
||
),
|
||
},
|
||
];
|
||
|
||
return (
|
||
<div>
|
||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12 }}>
|
||
<h2 style={{ margin: 0 }}>CK 检测与重登</h2>
|
||
<Space wrap>
|
||
<Button loading={checkingIds.size > 0} disabled={selectedIds.length === 0} onClick={() => void handleCheck(selectedIds)}>
|
||
检测选中 {selectedIds.length > 0 ? `(${selectedIds.length})` : ''}
|
||
</Button>
|
||
<Popconfirm
|
||
title={`将重新登录选中的 ${selectedIds.length} 个账号并替换 CK,确认继续?`}
|
||
disabled={selectedIds.length === 0}
|
||
onConfirm={() => void handleRelogin(selectedIds)}
|
||
>
|
||
<Button icon={<ReloadOutlined />} disabled={selectedIds.length === 0}>
|
||
重登选中 {selectedIds.length > 0 ? `(${selectedIds.length})` : ''}
|
||
</Button>
|
||
</Popconfirm>
|
||
<Popconfirm
|
||
title="将重新登录当前筛选范围内所有已检测为无效的账号,并在成功后替换旧 CK,确认继续?"
|
||
onConfirm={() => void handleReloginInvalid()}
|
||
>
|
||
<Button icon={<ReloadOutlined />}>
|
||
批量重登失效账号
|
||
</Button>
|
||
</Popconfirm>
|
||
{activeReloginBatchIds.length > 0 ? (
|
||
<Popconfirm title="确认停止当前重登任务?旧 CK 会保留。" onConfirm={() => void handleStopRelogin()}>
|
||
<Button danger icon={<StopOutlined />}>停止重登</Button>
|
||
</Popconfirm>
|
||
) : null}
|
||
</Space>
|
||
</div>
|
||
<Space wrap style={{ marginBottom: 12 }}>
|
||
<Select
|
||
allowClear
|
||
showSearch
|
||
placeholder="按标签筛选"
|
||
style={{ width: 160 }}
|
||
value={tagFilter || undefined}
|
||
onChange={(value) => {
|
||
setTagFilter(value || '');
|
||
setCurrentPage(1);
|
||
setSelectedRowKeys([]);
|
||
}}
|
||
options={tags.map((tag) => ({ value: tag, label: tag }))}
|
||
/>
|
||
<Input.Search
|
||
allowClear
|
||
prefix={<SearchOutlined />}
|
||
placeholder="搜索账号或标签"
|
||
style={{ width: 280 }}
|
||
value={search}
|
||
onChange={(event) => {
|
||
setSearch(event.target.value);
|
||
setCurrentPage(1);
|
||
setSelectedRowKeys([]);
|
||
}}
|
||
/>
|
||
</Space>
|
||
<Table
|
||
rowKey="id"
|
||
size="small"
|
||
loading={loading}
|
||
columns={columns}
|
||
dataSource={items}
|
||
rowSelection={{ selectedRowKeys, onChange: setSelectedRowKeys, preserveSelectedRowKeys: true }}
|
||
pagination={{
|
||
current: currentPage,
|
||
pageSize,
|
||
total,
|
||
showSizeChanger: true,
|
||
showTotal: (count) => `共 ${count} 条`,
|
||
onChange: (page, size) => {
|
||
setCurrentPage(size === pageSize ? page : 1);
|
||
if (size !== pageSize) {
|
||
setPageSize(size);
|
||
localStorage.setItem('cookie_operations_page_size', String(size));
|
||
}
|
||
},
|
||
}}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|