Files
monkey-ai-web/src/components/analytics/RiskControlRulesModal.jsx
T
zhanjunhao 67969a041d fix(i18n): 补齐九种语言的翻译并修复国际化检查
补齐页面缺译,统一动态文案的整句插值方式。
2026-11-17 14:10:25 +08:00

302 lines
9.0 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.
/*
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 { Trans } from 'react-i18next';
import React, { useEffect, useRef, useState } from 'react';
import {
Modal,
Form,
Switch,
InputNumber,
Input,
Typography,
Banner,
Button,
Space,
} from '@/components/ui';
import { renderQuota } from '../../helpers';
const { Text } = Typography;
const defaultValues = {
id: 0,
name: '高频低消耗自动封号',
enabled: false,
window_minutes: 1440,
scan_interval_minutes: 10,
warn_min_requests: 500,
warn_max_avg_quota: 50,
auto_ban_enabled: true,
ban_min_requests: 1000,
ban_max_avg_quota: 5,
ban_max_total_quota: 5000,
notify_admin: true,
};
const RiskControlRulesModal = ({
visible,
onClose,
rules,
saveRule,
triggerScanNow,
t,
}) => {
const [form, setForm] = useState(defaultValues);
const [saving, setSaving] = useState(false);
// 仅在 Modal 由"关闭→打开"的瞬间同步表单,避免 rules 异步到达时覆盖用户正在编辑的值
const prevVisibleRef = useRef(false);
useEffect(() => {
if (visible && !prevVisibleRef.current) {
if (rules && rules.length > 0) {
setForm({ ...defaultValues, ...rules[0] });
} else {
setForm(defaultValues);
}
}
prevVisibleRef.current = visible;
}, [visible, rules]);
const setField = (k, v) => setForm((f) => ({ ...f, [k]: v }));
const handleSave = async (alsoScan) => {
// 仅在启用了自动封号时校验封号阈值
if (form.auto_ban_enabled) {
if (
form.ban_min_requests > 0 &&
form.ban_min_requests < form.warn_min_requests
) {
Modal.warning({
title: t('阈值不合法'),
content: t('封号最低请求数不能低于预警阈值'),
});
return;
}
if (
form.ban_max_avg_quota > 0 &&
form.warn_max_avg_quota > 0 &&
form.ban_max_avg_quota > form.warn_max_avg_quota
) {
Modal.warning({
title: t('阈值不合法'),
content: t('封号平均消耗上限不能高于预警阈值'),
});
return;
}
}
setSaving(true);
const ok = await saveRule(form);
setSaving(false);
if (!ok) return;
if (alsoScan) {
await triggerScanNow();
}
onClose();
};
return (
<Modal
title={t('风控规则配置')}
visible={visible}
onCancel={onClose}
footer={
<Space>
<Button onClick={onClose}>{t('取消')}</Button>
<Button onClick={() => handleSave(false)} loading={saving}>
{t('仅保存')}
</Button>
<Button
type='primary'
theme='solid'
onClick={() => handleSave(true)}
loading={saving}
>
{t('保存并立即扫描')}
</Button>
</Space>
}
width={620}
>
<Banner
type='info'
description={t(
'命中"预警阈值"仅通知管理员,不动账号;命中"封号阈值"会自动封号并禁用该用户全部 Token。',
)}
className='mb-4'
/>
<div className='space-y-4'>
<Form.Slot label={t('规则名称')}>
<Input value={form.name} onChange={(v) => setField('name', v)} />
</Form.Slot>
<div className='flex items-center gap-3'>
<Switch
checked={form.enabled}
onChange={(v) => setField('enabled', v)}
/>
<Text>{t('启用此规则')}</Text>
</div>
<div className='grid grid-cols-2 gap-4'>
<Form.Slot label={t('检测时间窗(分钟)')}>
<InputNumber
value={form.window_minutes}
onChange={(v) => setField('window_minutes', v || 1440)}
min={1}
style={{ width: '100%' }}
suffix={t('分钟')}
/>
</Form.Slot>
<Form.Slot label={t('扫描频率(分钟)')}>
<InputNumber
value={form.scan_interval_minutes}
onChange={(v) => setField('scan_interval_minutes', v || 10)}
min={1}
style={{ width: '100%' }}
suffix={t('分钟')}
/>
</Form.Slot>
</div>
<div className='border-l-4 border-orange-400 pl-3 py-1'>
<Text strong className='text-orange-600'>
⚠ {t('预警阈值(仅通知,不封号)')}
</Text>
</div>
<div className='grid grid-cols-2 gap-4'>
<Form.Slot label={t('最低请求数')}>
<InputNumber
value={form.warn_min_requests}
onChange={(v) => setField('warn_min_requests', v || 0)}
min={1}
style={{ width: '100%' }}
/>
</Form.Slot>
<Form.Slot
label={
<span>
{t('平均消耗 ≤')}{' '}
<Text type='tertiary' size='small'>
({t('quota')}, ≈ {renderQuota(form.warn_max_avg_quota, 6)})
</Text>
</span>
}
>
<InputNumber
value={form.warn_max_avg_quota}
onChange={(v) => setField('warn_max_avg_quota', v || 0)}
min={0}
style={{ width: '100%' }}
/>
</Form.Slot>
</div>
<div className='border-l-4 border-red-500 pl-3 py-1 flex items-center justify-between'>
<Text strong className='text-red-600'>
🚨 {t('封号阈值(自动封号 + 禁用所有 Token)')}
</Text>
<div className='flex items-center gap-2'>
<Text size='small' type='tertiary'>
{t('启用自动封号')}
</Text>
<Switch
size='small'
checked={form.auto_ban_enabled}
onChange={(v) => setField('auto_ban_enabled', v)}
/>
</div>
</div>
{!form.auto_ban_enabled && (
<Banner
type='warning'
description={t(
'已关闭自动封号:命中阈值的用户仅会收到管理员预警通知,不会被自动处置。',
)}
className='mb-2'
/>
)}
<div
className={`grid grid-cols-2 gap-4 ${form.auto_ban_enabled ? '' : 'opacity-50 pointer-events-none'}`}
>
<Form.Slot label={t('最低请求数')}>
<InputNumber
value={form.ban_min_requests}
onChange={(v) => setField('ban_min_requests', v == null ? 0 : v)}
min={0}
style={{ width: '100%' }}
disabled={!form.auto_ban_enabled}
/>
</Form.Slot>
<Form.Slot
label={
<span>
{t('平均消耗 ≤')}{' '}
<Text type='tertiary' size='small'>
({t('quota')}, ≈ {renderQuota(form.ban_max_avg_quota, 6)})
</Text>
</span>
}
>
<InputNumber
value={form.ban_max_avg_quota}
onChange={(v) => setField('ban_max_avg_quota', v || 0)}
min={0}
style={{ width: '100%' }}
disabled={!form.auto_ban_enabled}
/>
</Form.Slot>
</div>
<Form.Slot
label={
<span>
{t('总消耗 ≤')}{' '}
<Text type='tertiary' size='small'>
<Trans
t={t}
i18nKey='(quota, ≈ {{value}}, 防误伤大客户)'
values={{ value: renderQuota(form.ban_max_total_quota, 4) }}
/>
</Text>
</span>
}
>
<InputNumber
value={form.ban_max_total_quota}
onChange={(v) => setField('ban_max_total_quota', v || 0)}
min={0}
style={{ width: '100%' }}
disabled={!form.auto_ban_enabled}
/>
</Form.Slot>
<div className='flex items-center gap-3 pt-2'>
<Switch
checked={form.notify_admin}
onChange={(v) => setField('notify_admin', v)}
/>
<Text>{t('触发时通知管理员(站内 / 邮件 / Webhook)')}</Text>
</div>
</div>
</Modal>
);
};
export default RiskControlRulesModal;