233 lines
7.5 KiB
React
233 lines
7.5 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, { useRef, useEffect } from 'react';
|
|
import { Button, Form, Toast } from '@/components/ui';
|
|
import { Check, Copy } from 'lucide-react';
|
|
import { FormQuotaInput } from '../../../common/ui/QuotaInput';
|
|
import { copy } from '../../../../helpers';
|
|
import SectionHeader from '../components/SectionHeader';
|
|
|
|
// Webhook 请求结构示例(只读展示,与原 CodeViewer 展示的内容相同)
|
|
const WEBHOOK_EXAMPLE = {
|
|
type: 'quota_exceed',
|
|
title: '额度预警通知',
|
|
content: '您的额度即将用尽,当前剩余额度为 {{value}}',
|
|
values: ['¥0.99'],
|
|
timestamp: 1739950503,
|
|
};
|
|
|
|
const NotificationSection = ({
|
|
t,
|
|
notificationSettings,
|
|
handleNotificationSettingChange,
|
|
saveNotificationSettings,
|
|
}) => {
|
|
const formApiRef = useRef(null);
|
|
|
|
useEffect(() => {
|
|
if (formApiRef.current && notificationSettings) {
|
|
formApiRef.current.setValues(notificationSettings);
|
|
}
|
|
}, [notificationSettings]);
|
|
|
|
const handleFormChange = (field, value) => {
|
|
handleNotificationSettingChange(field, value);
|
|
};
|
|
|
|
const handleSubmit = () => {
|
|
if (formApiRef.current) {
|
|
formApiRef.current
|
|
.validate()
|
|
.then(() => {
|
|
saveNotificationSettings();
|
|
})
|
|
.catch((errors) => {
|
|
console.log('表单验证失败:', errors);
|
|
Toast.error(t('请检查表单填写是否正确'));
|
|
});
|
|
} else {
|
|
saveNotificationSettings();
|
|
}
|
|
};
|
|
|
|
// 复制示例:沿用原 CodeViewer 的复制方式与提示
|
|
const handleCopyExample = async () => {
|
|
try {
|
|
const success = await copy(JSON.stringify(WEBHOOK_EXAMPLE, null, 2));
|
|
Toast.success(t('已复制到剪贴板'));
|
|
if (!success) {
|
|
throw new Error('Copy operation failed');
|
|
}
|
|
} catch (err) {
|
|
Toast.error(t('复制失败'));
|
|
console.error('Copy failed:', err);
|
|
}
|
|
};
|
|
|
|
// 通知方式选项(仅保留邮件通知和Webhook)
|
|
const notifyOptions = [
|
|
{
|
|
value: 'email',
|
|
label: t('邮件提醒'),
|
|
desc: t('发送到账户邮箱或指定邮箱'),
|
|
},
|
|
{
|
|
value: 'webhook',
|
|
label: 'Webhook',
|
|
desc: t('将提醒推送到外部系统'),
|
|
},
|
|
];
|
|
|
|
return (
|
|
<div className='ui-notify-section'>
|
|
<SectionHeader
|
|
title={t('通知设置')}
|
|
description={t('设置额度预警和系统提醒接收方式')}
|
|
extra={
|
|
<Button theme='solid' onClick={handleSubmit}>
|
|
{t('保存设置')}
|
|
</Button>
|
|
}
|
|
/>
|
|
|
|
<Form
|
|
className='ui-notify-form'
|
|
requiredMark={false}
|
|
getFormApi={(api) => (formApiRef.current = api)}
|
|
initValues={notificationSettings}
|
|
onSubmit={handleSubmit}
|
|
>
|
|
{/* 通知方式选择 */}
|
|
<div className='ui-notify-methods-field'>
|
|
<div className='ui-notify-label'>{t('通知方式')}</div>
|
|
<div className='ui-notify-methods'>
|
|
{notifyOptions.map((option) => {
|
|
const isSelected =
|
|
notificationSettings.warningType === option.value;
|
|
return (
|
|
<button
|
|
key={option.value}
|
|
type='button'
|
|
aria-pressed={isSelected}
|
|
onClick={() => handleFormChange('warningType', option.value)}
|
|
className={`ui-notify-method ${isSelected ? 'selected' : ''}`}
|
|
>
|
|
<span className='ui-notify-method-text'>
|
|
<strong>{option.label}</strong>
|
|
<span>{option.desc}</span>
|
|
</span>
|
|
{isSelected && (
|
|
<Check size={16} className='ui-notify-method-check' />
|
|
)}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* 预警阈值 */}
|
|
<FormQuotaInput
|
|
field='warningThreshold'
|
|
label={t('额度预警阈值')}
|
|
onChange={(val) => handleFormChange('warningThreshold', val)}
|
|
extraText={t('当剩余额度低于此数值时,通过所选方式发送通知')}
|
|
style={{ width: 260, maxWidth: '100%' }}
|
|
rules={[
|
|
{ required: true, message: t('请输入预警阈值') },
|
|
{
|
|
validator: (rule, value) => {
|
|
const numValue = Number(value);
|
|
if (isNaN(numValue) || numValue <= 0) {
|
|
return Promise.reject(t('预警阈值必须为正数'));
|
|
}
|
|
return Promise.resolve();
|
|
},
|
|
},
|
|
]}
|
|
/>
|
|
|
|
{/* 邮件通知设置 */}
|
|
{notificationSettings.warningType === 'email' && (
|
|
<div className='ui-notify-panel'>
|
|
<Form.Input
|
|
field='notificationEmail'
|
|
label={t('通知邮箱')}
|
|
placeholder={t('留空则使用账号绑定的邮箱')}
|
|
onChange={(val) => handleFormChange('notificationEmail', val)}
|
|
extraText={t('不填写则使用账号绑定的邮箱')}
|
|
showClear
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* Webhook通知设置 */}
|
|
{notificationSettings.warningType === 'webhook' && (
|
|
<div className='ui-notify-panel'>
|
|
<Form.Input
|
|
field='webhookUrl'
|
|
label={t('Webhook 地址')}
|
|
placeholder='https://example.com/webhook'
|
|
onChange={(val) => handleFormChange('webhookUrl', val)}
|
|
extraText={t('仅支持 HTTPS,系统以 POST 方式发送通知')}
|
|
showClear
|
|
rules={[
|
|
{
|
|
required: notificationSettings.warningType === 'webhook',
|
|
message: t('请输入Webhook地址'),
|
|
},
|
|
{
|
|
pattern: /^https:\/\/.+/,
|
|
message: t('Webhook地址必须以https://开头'),
|
|
},
|
|
]}
|
|
/>
|
|
|
|
<Form.Input
|
|
field='webhookSecret'
|
|
label={t('接口凭证')}
|
|
mode='password'
|
|
placeholder={t('请输入密钥')}
|
|
onChange={(val) => handleFormChange('webhookSecret', val)}
|
|
extraText={t('密钥以 Bearer 方式添加到请求头中')}
|
|
/>
|
|
|
|
<div className='ui-notify-code-head'>
|
|
<span>{t('Webhook 请求结构说明')}</span>
|
|
<button
|
|
type='button'
|
|
className='ui-notify-copy'
|
|
onClick={handleCopyExample}
|
|
>
|
|
{t('复制')}
|
|
<Copy size={14} />
|
|
</button>
|
|
</div>
|
|
<pre className='ui-notify-code'>
|
|
{JSON.stringify(WEBHOOK_EXAMPLE, null, 2)}
|
|
</pre>
|
|
</div>
|
|
)}
|
|
</Form>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default NotificationSection;
|