Files
monkey-ai-web/src/components/analytics/RiskControlBanLogs.jsx
T
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

262 lines
6.8 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { formatUserReference } from '../../helpers/safeDisplay';
import { Trans } from 'react-i18next';
import React, { useState } from 'react';
import {
Table,
Tag,
Button,
Modal,
Input,
Typography,
Space,
Select,
} from '@/components/ui';
import { IconRefresh } from '@/components/ui/icons';
import { renderQuota } from '../../helpers';
const { Text } = Typography;
const formatTime = (ts) => {
if (!ts) return '-';
const d = new Date(ts * 1000);
const pad = (n) => (n < 10 ? '0' + n : '' + n);
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
};
/**
* 自动封号历史 / 解封操作中心
*
* 职责:
* - 持久查看所有由风控自动封号产生的记录(即使用户已经从疑似列表中消失)
* - 解封操作的主入口(疑似列表里的解封按钮只能覆盖"封号后还有活动"的边界场景)
*/
const RiskControlBanLogs = ({
loading,
banLogs,
banLogTotal,
banLogStatus,
setBanLogStatus,
banLogPage,
banLogPageSize,
fetchBanLogs,
unbanUser,
t,
}) => {
const [unbanModalVisible, setUnbanModalVisible] = useState(false);
const [unbanRow, setUnbanRow] = useState(null);
const [unbanReason, setUnbanReason] = useState('');
const columns = [
{
title: t('封号时间'),
dataIndex: 'ban_at',
width: 150,
render: (v) => formatTime(v),
},
{
title: t('用户ID'),
dataIndex: 'user_id',
width: 100,
},
{
title: t('用户名(快照)'),
dataIndex: 'user_snapshot',
width: 140,
render: (v) => v || <Text type='tertiary'>-</Text>,
},
{
title: t('请求次数'),
dataIndex: 'request_count',
width: 100,
render: (v) => (v || 0).toLocaleString(),
},
{
title: t('总消耗'),
dataIndex: 'total_quota',
width: 110,
render: (v) => renderQuota(v || 0, 4),
},
{
title: t('平均消耗'),
dataIndex: 'avg_quota_x1000',
width: 120,
render: (v) => renderQuota((v || 0) / 1000, 6),
},
{
title: t('最常用模型'),
dataIndex: 'top_model',
width: 150,
render: (v) =>
v ? <Tag color='blue'>{v}</Tag> : <Text type='tertiary'>-</Text>,
},
{
title: t('活跃 IP'),
dataIndex: 'last_ip',
width: 130,
render: (v) => v || <Text type='tertiary'>-</Text>,
},
{
title: t('状态'),
dataIndex: 'status',
width: 100,
render: (v) =>
v === 1 ? (
<Tag color='red'>{t('已封号')}</Tag>
) : (
<Tag color='green'>{t('已解封')}</Tag>
),
},
{
title: t('解封时间'),
dataIndex: 'unban_at',
width: 150,
render: (v) => (v > 0 ? formatTime(v) : <Text type='tertiary'>-</Text>),
},
{
title: t('解封原因'),
dataIndex: 'unban_reason',
width: 180,
render: (v) => v || <Text type='tertiary'>-</Text>,
},
{
title: t('操作'),
width: 110,
fixed: 'right',
render: (_v, row) => {
if (row.status === 1) {
return (
<Button
size='small'
type='primary'
theme='light'
onClick={() => {
setUnbanRow(row);
setUnbanReason('');
setUnbanModalVisible(true);
}}
>
{t('解封')}
</Button>
);
}
return (
<Text type='tertiary' size='small'>
-
</Text>
);
},
},
];
const statusOptions = [
{ value: 0, label: t('全部') },
{ value: 1, label: t('已封号') },
{ value: 2, label: t('已解封') },
];
return (
<div>
<div className='mb-3 flex items-center justify-between flex-wrap gap-2'>
<Space>
<Text type='tertiary' size='small'>
<Trans
t={t}
i18nKey='共 {{value}} 条'
values={{ value: banLogTotal || 0 }}
/>
</Text>
<Select
value={banLogStatus}
onChange={(v) => {
setBanLogStatus(v);
fetchBanLogs({ status: v, page: 1 });
}}
optionList={statusOptions}
style={{ width: 120 }}
size='small'
/>
</Space>
<Button
icon={<IconRefresh />}
size='small'
onClick={() => fetchBanLogs()}
loading={loading}
>
{t('刷新')}
</Button>
</div>
<Table
columns={columns}
dataSource={banLogs}
rowKey='id'
loading={loading}
pagination={{
pageSize: banLogPageSize,
showTotal: true,
total: banLogTotal,
// 受控分页:避免切换状态筛选后 Semi 内部页码与 hook 不同步
currentPage: banLogPage,
onPageChange: (page) => fetchBanLogs({ page }),
onPageSizeChange: (pageSize) => fetchBanLogs({ page: 1, pageSize }),
}}
empty={t('暂无封号记录')}
scroll={{ x: 1400 }}
/>
<Modal
title={t('解封用户')}
visible={unbanModalVisible}
onCancel={() => setUnbanModalVisible(false)}
onOk={async () => {
if (!unbanRow) return;
// unbanUser 内部会并发刷新 suspects + banLogs,无需在此重复 fetch
const ok = await unbanUser({
userId: unbanRow.user_id,
banLogId: unbanRow.id,
reason: unbanReason,
});
if (ok) {
setUnbanModalVisible(false);
}
}}
okText={t('确认解封')}
cancelText={t('取消')}
>
{unbanRow && (
<div className='space-y-3'>
<div>
<Text type='tertiary'>{t('用户:')}</Text>
<Text strong>
{unbanRow.user_snapshot ||
formatUserReference('', unbanRow.user_id, t('未知用户'))}
(ID:
{unbanRow.user_id})
</Text>
</div>
<div>
<Text type='tertiary'>
{t(
'解封后用户账号会恢复,但被禁用的 Token 不会自动启用,需手动开启。',
)}
</Text>
</div>
<div>
<div className='text-xs text-gray-500 mb-1'>{t('解封原因')}</div>
<Input.TextArea
value={unbanReason}
onChange={setUnbanReason}
placeholder={t('记录解封原因,便于审计')}
rows={3}
/>
</div>
</div>
)}
</Modal>
</div>
);
};
export default RiskControlBanLogs;