Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
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, { useMemo, useState } from 'react';
|
||||
import {
|
||||
Card,
|
||||
Table,
|
||||
Tag,
|
||||
Button,
|
||||
InputNumber,
|
||||
Select,
|
||||
Space,
|
||||
Modal,
|
||||
Input,
|
||||
Typography,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import {
|
||||
IconRefresh,
|
||||
IconAlertTriangle,
|
||||
IconShieldStroked,
|
||||
} from '@douyinfe/semi-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())}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* 高频低消耗用户监测组件
|
||||
*
|
||||
* 列:用户ID / 昵称 / 请求次数 / 总消耗 / 平均消耗 / 状态 / 最常用模型 / 最近IP / 最近活跃 / 操作
|
||||
*/
|
||||
const HighFreqLowCostUsers = ({
|
||||
loading,
|
||||
items,
|
||||
windowRange,
|
||||
filters,
|
||||
setFilters,
|
||||
fetchSuspects,
|
||||
rules,
|
||||
unbanUser,
|
||||
onOpenRuleModal,
|
||||
t,
|
||||
}) => {
|
||||
const [unbanModalVisible, setUnbanModalVisible] = useState(false);
|
||||
const [unbanRow, setUnbanRow] = useState(null);
|
||||
const [unbanReason, setUnbanReason] = useState('');
|
||||
|
||||
// 当前启用规则(仅取 enabled=true 的规则)—用于"是否命中封号阈值"标签
|
||||
// 不回落到禁用规则:否则会显示"待自动封号"红色 tag 但实际永不封号,误导管理员
|
||||
const activeRule = useMemo(
|
||||
() => (rules || []).find((r) => r.enabled),
|
||||
[rules],
|
||||
);
|
||||
|
||||
const evalRowStatus = (row) => {
|
||||
// 状态优先级:已自动封号 > 命中封号阈值(待下次扫描)> 命中预警 > 仅满足查询条件
|
||||
if (row.user_status !== undefined && row.user_status === 2) {
|
||||
return { level: 'banned', label: t('已封号'), color: 'red' };
|
||||
}
|
||||
if (!activeRule)
|
||||
return { level: 'hit', label: t('命中筛选'), color: 'grey' };
|
||||
// 仅当规则的"自动封号"总开关启用时,才判断是否命中封号阈值
|
||||
const meetsBan =
|
||||
activeRule.auto_ban_enabled &&
|
||||
activeRule.ban_min_requests > 0 &&
|
||||
row.request_count >= activeRule.ban_min_requests &&
|
||||
(activeRule.ban_max_avg_quota <= 0 ||
|
||||
row.avg_quota <= activeRule.ban_max_avg_quota) &&
|
||||
(activeRule.ban_max_total_quota <= 0 ||
|
||||
row.total_quota <= activeRule.ban_max_total_quota);
|
||||
if (meetsBan) return { level: 'ban', label: t('待自动封号'), color: 'red' };
|
||||
const meetsWarn =
|
||||
activeRule.warn_min_requests > 0 &&
|
||||
row.request_count >= activeRule.warn_min_requests &&
|
||||
(activeRule.warn_max_avg_quota <= 0 ||
|
||||
row.avg_quota <= activeRule.warn_max_avg_quota);
|
||||
if (meetsWarn) return { level: 'warn', label: t('预警'), color: 'orange' };
|
||||
return { level: 'hit', label: t('命中筛选'), color: 'grey' };
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: t('用户ID'),
|
||||
dataIndex: 'user_id',
|
||||
width: 100,
|
||||
render: (v) => <Text>{v}</Text>,
|
||||
},
|
||||
{
|
||||
title: t('昵称'),
|
||||
dataIndex: 'display_name',
|
||||
width: 160,
|
||||
render: (v, row) => (
|
||||
<div className='flex flex-col'>
|
||||
<Text strong>{v || row.username || `user_${row.user_id}`}</Text>
|
||||
{row.username && v && row.username !== v && (
|
||||
<Text type='tertiary' size='small'>
|
||||
@{row.username}
|
||||
</Text>
|
||||
)}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('请求次数'),
|
||||
dataIndex: 'request_count',
|
||||
width: 110,
|
||||
sorter: (a, b) => a.request_count - b.request_count,
|
||||
defaultSortOrder: 'descend',
|
||||
render: (v) => <Text strong>{v.toLocaleString()}</Text>,
|
||||
},
|
||||
{
|
||||
title: t('总消耗'),
|
||||
dataIndex: 'total_quota',
|
||||
width: 120,
|
||||
sorter: (a, b) => a.total_quota - b.total_quota,
|
||||
render: (v) => <Text>{renderQuota(v, 4)}</Text>,
|
||||
},
|
||||
{
|
||||
title: t('平均消耗'),
|
||||
dataIndex: 'avg_quota',
|
||||
width: 130,
|
||||
sorter: (a, b) => a.avg_quota - b.avg_quota,
|
||||
render: (v) => (
|
||||
<Text type={v < 100 ? 'danger' : 'primary'}>{renderQuota(v, 6)}</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
width: 110,
|
||||
render: (_v, row) => {
|
||||
const s = evalRowStatus(row);
|
||||
return <Tag color={s.color}>{s.label}</Tag>;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('最常用模型'),
|
||||
dataIndex: 'top_model',
|
||||
width: 160,
|
||||
render: (v) =>
|
||||
v ? <Tag color='blue'>{v}</Tag> : <Text type='tertiary'>-</Text>,
|
||||
},
|
||||
{
|
||||
title: t('活跃 IP'),
|
||||
dataIndex: 'last_ip',
|
||||
width: 130,
|
||||
render: (v) => (v ? <Text>{v}</Text> : <Text type='tertiary'>-</Text>),
|
||||
},
|
||||
{
|
||||
title: t('最近活跃'),
|
||||
dataIndex: 'last_active_at',
|
||||
width: 150,
|
||||
render: (v) => formatTime(v),
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
width: 130,
|
||||
render: (_v, row) => {
|
||||
const isBanned = row.user_status === 2;
|
||||
const isRiskBan = isBanned && row.ban_log_id > 0;
|
||||
if (isRiskBan) {
|
||||
return (
|
||||
<Button
|
||||
size='small'
|
||||
type='primary'
|
||||
theme='light'
|
||||
onClick={() => {
|
||||
setUnbanRow(row);
|
||||
setUnbanReason('');
|
||||
setUnbanModalVisible(true);
|
||||
}}
|
||||
>
|
||||
{t('解封')}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
if (isBanned) {
|
||||
return (
|
||||
<Text type='tertiary' size='small'>
|
||||
{t('已被手动封号')}
|
||||
</Text>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<Text type='tertiary' size='small'>
|
||||
{t('监测中')}
|
||||
</Text>
|
||||
);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const windowOptions = [
|
||||
{ value: 60, label: t('最近 1 小时') },
|
||||
{ value: 360, label: t('最近 6 小时') },
|
||||
{ value: 1440, label: t('最近 24 小时') },
|
||||
{ value: 4320, label: t('最近 3 天') },
|
||||
{ value: 10080, label: t('最近 7 天') },
|
||||
];
|
||||
|
||||
return (
|
||||
<Card
|
||||
className='shadow-sm hover:shadow-md transition-shadow'
|
||||
title={
|
||||
<div className='flex items-center justify-between flex-wrap gap-2'>
|
||||
<div className='flex items-center gap-2'>
|
||||
<IconAlertTriangle className='text-orange-500' />
|
||||
<span className='font-semibold'>{t('高频低消耗用户监测')}</span>
|
||||
{windowRange && windowRange.start > 0 && (
|
||||
<Text type='tertiary' size='small' className='ml-2'>
|
||||
{formatTime(windowRange.start)} ~ {formatTime(windowRange.end)}
|
||||
</Text>
|
||||
)}
|
||||
</div>
|
||||
<Space>
|
||||
<Button
|
||||
icon={<IconShieldStroked />}
|
||||
onClick={onOpenRuleModal}
|
||||
type='tertiary'
|
||||
>
|
||||
{t('风控规则')}
|
||||
</Button>
|
||||
<Button
|
||||
icon={<IconRefresh />}
|
||||
onClick={() => fetchSuspects()}
|
||||
loading={loading}
|
||||
>
|
||||
{t('刷新')}
|
||||
</Button>
|
||||
</Space>
|
||||
</div>
|
||||
}
|
||||
bodyStyle={{ padding: '16px' }}
|
||||
>
|
||||
{/* 筛选区 */}
|
||||
<div className='mb-4 grid grid-cols-2 sm:grid-cols-4 gap-3 items-end'>
|
||||
<div>
|
||||
<div className='text-xs text-gray-500 mb-1'>{t('时间窗')}</div>
|
||||
<Select
|
||||
value={filters.windowMinutes}
|
||||
onChange={(v) => setFilters((f) => ({ ...f, windowMinutes: v }))}
|
||||
optionList={windowOptions}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<div className='text-xs text-gray-500 mb-1'>{t('请求次数 ≥')}</div>
|
||||
<InputNumber
|
||||
value={filters.minRequests}
|
||||
onChange={(v) => setFilters((f) => ({ ...f, minRequests: v || 0 }))}
|
||||
min={1}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<div className='text-xs text-gray-500 mb-1'>
|
||||
{t('平均消耗 ≤ (quota,0=不限)')}
|
||||
</div>
|
||||
<InputNumber
|
||||
value={filters.maxAvgQuota}
|
||||
onChange={(v) => setFilters((f) => ({ ...f, maxAvgQuota: v || 0 }))}
|
||||
min={0}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</div>
|
||||
<div className='flex gap-2'>
|
||||
<Button
|
||||
type='primary'
|
||||
theme='solid'
|
||||
onClick={() => fetchSuspects()}
|
||||
loading={loading}
|
||||
block
|
||||
>
|
||||
{t('查询')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{(filters.maxAvgQuota > 0 || filters.maxTotalQuota > 0) && (
|
||||
<div className='mb-3 text-xs text-gray-500'>
|
||||
{filters.maxAvgQuota > 0 && (
|
||||
<span>
|
||||
{t('平均消耗 ≤')} {renderQuota(filters.maxAvgQuota, 6)}
|
||||
{' '}
|
||||
</span>
|
||||
)}
|
||||
{filters.maxTotalQuota > 0 && (
|
||||
<span>
|
||||
{t('总消耗 ≤')} {renderQuota(filters.maxTotalQuota, 4)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={items}
|
||||
rowKey='user_id'
|
||||
loading={loading}
|
||||
pagination={{ pageSize: 20, showTotal: true }}
|
||||
empty={t('暂无数据')}
|
||||
scroll={{ x: 1200 }}
|
||||
/>
|
||||
|
||||
<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.ban_log_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.display_name || unbanRow.username}(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>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
export default HighFreqLowCostUsers;
|
||||
Reference in New Issue
Block a user