优化(登录任务): 移除批量实时日志,增加账号独立日志
- 登录任务移除底部实时日志面板和 WebSocket 订阅 - 登录执行器按任务收集日志并在结束时持久化 - 新增登录任务日志接口和单账号日志弹窗 - 迁移 0134 增加 login_tasks.logs - 修复无 WebSocket 时开始按钮状态未复位
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
import { useEffect, useState, useMemo, useCallback, useRef } from 'react';
|
||||
import {
|
||||
Table, Button, Select, Tag, Space, Input, InputNumber, Tooltip, Popconfirm, theme, Modal, Form, Dropdown, Typography,
|
||||
Table, Button, Select, Tag, Space, Input, InputNumber, Tooltip, Popconfirm, theme, Modal, Form, Dropdown, Typography, Spin,
|
||||
} from 'antd';
|
||||
import { message } from '../utils/antdMessage';
|
||||
import {
|
||||
PlayCircleOutlined, StopOutlined, FilterOutlined, ReloadOutlined, DeleteOutlined,
|
||||
SettingOutlined, SafetyCertificateOutlined, DownloadOutlined, ClearOutlined,
|
||||
SettingOutlined, SafetyCertificateOutlined, DownloadOutlined, ClearOutlined, FileTextOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { accountApi, loginApi, type AccountItem, type LoginTaskItem, type TaskSummary } from '../api/modules';
|
||||
import { accountApi, loginApi, type AccountItem, type LoginTaskItem, type LoginTaskLogItem, type TaskSummary } from '../api/modules';
|
||||
import type { LoginTaskFilters } from '../api/login';
|
||||
import RealtimeLogPanel from '../components/RealtimeLogPanel';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { useWebSocketLogs } from '../hooks/useWebSocketLogs';
|
||||
import { formatTime } from '../utils/time';
|
||||
import { getErrorMessage } from '../utils/error';
|
||||
|
||||
@@ -142,6 +140,9 @@ export default function LoginTasksPage() {
|
||||
useEffect(() => { localStorage.setItem('login_max_login_retries', String(maxLoginRetries)); }, [maxLoginRetries]);
|
||||
useEffect(() => { localStorage.setItem('login_api_strategy', apiStrategy); }, [apiStrategy]);
|
||||
const [selectedRowKeys, setSelectedRowKeys] = useState<number[]>([]);
|
||||
const [logTask, setLogTask] = useState<LoginTaskItem | null>(null);
|
||||
const [logItems, setLogItems] = useState<LoginTaskLogItem[]>([]);
|
||||
const [logsLoading, setLogsLoading] = useState(false);
|
||||
const [selectedAllMatching, setSelectedAllMatching] = useState(false);
|
||||
// 任务列表分页:库里上万条,单页装不下;total 同时用于「全选所有结果」。
|
||||
const [taskPage, setTaskPage] = useState(1);
|
||||
@@ -162,7 +163,6 @@ export default function LoginTasksPage() {
|
||||
// 用它算「成功/失败」会和「共 N 个任务」对不上(曾显示「共 8508,成功 48」)。
|
||||
const [taskSummary, setTaskSummary] = useState<TaskSummary | null>(null);
|
||||
useEffect(() => { localStorage.setItem('login_task_page_size', String(taskPageSize)); }, [taskPageSize]);
|
||||
const { logs, connected: wsConnected, connect: connectLogs } = useWebSocketLogs();
|
||||
const { can } = usePermissions();
|
||||
|
||||
const { token } = theme.useToken();
|
||||
@@ -332,19 +332,6 @@ export default function LoginTasksPage() {
|
||||
);
|
||||
const effectiveBatchId = batchId || activeBatchIdFromTasks;
|
||||
|
||||
// 活跃批次存在但未订阅时自动重连实时日志(配合后端 WS 仅在批次结束后清理注册表)。
|
||||
const wsSubscribedBatchRef = useRef<string | null>(null);
|
||||
useEffect(() => {
|
||||
if (!effectiveBatchId || wsConnected) return;
|
||||
if (wsSubscribedBatchRef.current === effectiveBatchId) return;
|
||||
wsSubscribedBatchRef.current = effectiveBatchId;
|
||||
connectLogs(`/api/login/ws/login/${effectiveBatchId}`, {
|
||||
onClose: () => { setBatchId(null); setStarting(false); },
|
||||
onResult: () => { setBatchId(null); setStarting(false); },
|
||||
onError: () => { setBatchId(null); setStarting(false); },
|
||||
});
|
||||
}, [effectiveBatchId, wsConnected, connectLogs]);
|
||||
|
||||
useEffect(() => {
|
||||
void loadTags();
|
||||
void loadTaskFilterOptions();
|
||||
@@ -378,10 +365,10 @@ export default function LoginTasksPage() {
|
||||
}, [accountSearch, loadAccountsPage]);
|
||||
|
||||
useEffect(() => {
|
||||
const intervalMs = hasActiveTasks || wsConnected || starting || batchId ? 3000 : 15000;
|
||||
const intervalMs = hasActiveTasks || starting || batchId ? 3000 : 15000;
|
||||
const timer = setInterval(loadTasks, intervalMs);
|
||||
return () => clearInterval(timer);
|
||||
}, [batchId, hasActiveTasks, loadTasks, starting, wsConnected]);
|
||||
}, [batchId, hasActiveTasks, loadTasks, starting]);
|
||||
|
||||
// 共享的批量任务启动逻辑
|
||||
const startBatch = async (accountIds: number[], mode: BatchMode = 'login') => {
|
||||
@@ -407,18 +394,11 @@ export default function LoginTasksPage() {
|
||||
mode,
|
||||
});
|
||||
setBatchId(result.batch_id);
|
||||
wsSubscribedBatchRef.current = result.batch_id;
|
||||
message.success(`已创建${mode === 'check' ? '检测' : '登录'}任务,共 ${result.count} 个账号`);
|
||||
|
||||
connectLogs(`/api/login/ws/login/${result.batch_id}`, {
|
||||
onClose: () => { setBatchId(null); setStarting(false); },
|
||||
onResult: () => { setBatchId(null); setStarting(false); },
|
||||
onError: () => { setBatchId(null); setStarting(false); },
|
||||
});
|
||||
} catch (e: unknown) {
|
||||
message.error(getErrorMessage(e));
|
||||
setStarting(false);
|
||||
} finally {
|
||||
setStarting(false);
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
@@ -600,6 +580,20 @@ export default function LoginTasksPage() {
|
||||
[tagAccountIds],
|
||||
);
|
||||
|
||||
const openTaskLogs = async (task: LoginTaskItem) => {
|
||||
setLogTask(task);
|
||||
setLogItems([]);
|
||||
setLogsLoading(true);
|
||||
try {
|
||||
const data = await loginApi.taskLogs(task.id);
|
||||
setLogItems(data.items || []);
|
||||
} catch (error) {
|
||||
message.error(getErrorMessage(error));
|
||||
} finally {
|
||||
setLogsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{ title: 'ID', dataIndex: 'id', width: 60 },
|
||||
{ title: '登录账号', dataIndex: 'account_username' },
|
||||
@@ -656,10 +650,18 @@ export default function LoginTasksPage() {
|
||||
{ title: '时间', dataIndex: 'created_at', width: 180, render: (val: string) => formatTime(val) },
|
||||
{
|
||||
title: '操作',
|
||||
width: 120,
|
||||
width: 190,
|
||||
render: (_: unknown, record: LoginTaskItem) => (
|
||||
<Space size={4}>
|
||||
{['failed', 'error'].includes(record.status) && !wsConnected && (
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
icon={<FileTextOutlined />}
|
||||
onClick={() => void openTaskLogs(record)}
|
||||
>
|
||||
日志
|
||||
</Button>
|
||||
{['failed', 'error'].includes(record.status) && (
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
@@ -981,7 +983,7 @@ export default function LoginTasksPage() {
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{!wsConnected && failedCount > 0 && (
|
||||
{failedCount > 0 && (
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
@@ -1048,15 +1050,6 @@ export default function LoginTasksPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 实时日志 - 底部可折叠 */}
|
||||
<RealtimeLogPanel
|
||||
logs={logs}
|
||||
connected={wsConnected}
|
||||
collapsible
|
||||
spinWhenEmpty
|
||||
style={{ marginTop: 4 }}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
title="登录设置"
|
||||
open={settingsOpen}
|
||||
@@ -1143,6 +1136,43 @@ export default function LoginTasksPage() {
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
title={logTask ? `登录日志 · ${logTask.account_username}` : '登录日志'}
|
||||
open={Boolean(logTask)}
|
||||
footer={null}
|
||||
width={760}
|
||||
onCancel={() => setLogTask(null)}
|
||||
>
|
||||
{logsLoading ? (
|
||||
<div style={{ minHeight: 120, display: 'grid', placeItems: 'center' }}>
|
||||
<Spin />
|
||||
</div>
|
||||
) : logItems.length === 0 ? (
|
||||
<Text type="secondary">暂无日志;任务完成后会写入本次登录尝试的日志。</Text>
|
||||
) : (
|
||||
<div style={{ maxHeight: '62vh', overflow: 'auto', display: 'grid', gap: 8 }}>
|
||||
{logItems.map((item, index) => (
|
||||
<div
|
||||
key={`${item.at || ''}-${index}`}
|
||||
style={{
|
||||
padding: '8px 10px',
|
||||
border: `1px solid ${token.colorBorderSecondary}`,
|
||||
borderRadius: 6,
|
||||
}}
|
||||
>
|
||||
<Space size={6} style={{ marginBottom: 4 }}>
|
||||
<Tag color={item.level === 'error' ? 'red' : item.level === 'warning' ? 'orange' : item.level === 'success' ? 'green' : 'blue'}>
|
||||
{item.level || 'info'}
|
||||
</Tag>
|
||||
{item.at ? <Text type="secondary" style={{ fontSize: 12 }}>{formatTime(item.at)}</Text> : null}
|
||||
</Space>
|
||||
<div style={{ whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>{item.message}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user