/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
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 || -,
},
{
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 ? {v} : -,
},
{
title: t('活跃 IP'),
dataIndex: 'last_ip',
width: 130,
render: (v) => v || -,
},
{
title: t('状态'),
dataIndex: 'status',
width: 100,
render: (v) =>
v === 1 ? (
{t('已封号')}
) : (
{t('已解封')}
),
},
{
title: t('解封时间'),
dataIndex: 'unban_at',
width: 150,
render: (v) => (v > 0 ? formatTime(v) : -),
},
{
title: t('解封原因'),
dataIndex: 'unban_reason',
width: 180,
render: (v) => v || -,
},
{
title: t('操作'),
width: 110,
fixed: 'right',
render: (_v, row) => {
if (row.status === 1) {
return (
);
}
return (
-
);
},
},
];
const statusOptions = [
{ value: 0, label: t('全部') },
{ value: 1, label: t('已封号') },
{ value: 2, label: t('已解封') },
];
return (
}
size='small'
onClick={() => fetchBanLogs()}
loading={loading}
>
{t('刷新')}
fetchBanLogs({ page }),
onPageSizeChange: (pageSize) => fetchBanLogs({ page: 1, pageSize }),
}}
empty={t('暂无封号记录')}
scroll={{ x: 1400 }}
/>
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 && (
{t('用户:')}
{unbanRow.user_snapshot || `user_${unbanRow.user_id}`}(ID:
{unbanRow.user_id})
{t(
'解封后用户账号会恢复,但被禁用的 Token 不会自动启用,需手动开启。',
)}
)}
);
};
export default RiskControlBanLogs;