270 lines
7.1 KiB
React
270 lines
7.1 KiB
React
/*
|
||
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 <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
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,
|
||
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'>
|
||
{t('共')} {banLogTotal || 0} {t('条')}
|
||
</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: 20,
|
||
showTotal: true,
|
||
total: banLogTotal,
|
||
// 受控分页:避免切换状态筛选后 Semi 内部页码与 hook 不同步
|
||
currentPage: banLogPage,
|
||
onPageChange: (page) => fetchBanLogs({ page }),
|
||
}}
|
||
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 || `user_${unbanRow.user_id}`}(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;
|