Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
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 '@douyinfe/semi-ui';
|
||||
import { IconMail, IconKey, IconLink } from '@douyinfe/semi-icons';
|
||||
import { Bell, Mail, Save, Webhook } from 'lucide-react';
|
||||
import { FormQuotaInput } from '../../../common/ui/QuotaInput';
|
||||
import CodeViewer from '../../../playground/CodeViewer';
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
||||
// 通知方式选项(仅保留邮件通知和Webhook)
|
||||
const notifyOptions = [
|
||||
{
|
||||
value: 'email',
|
||||
label: t('邮件提醒'),
|
||||
desc: t('发送到绑定邮箱或指定邮箱'),
|
||||
icon: Mail,
|
||||
selectedClass:
|
||||
'border-sky-300 bg-sky-50/82 dark:bg-sky-500/[0.10] dark:border-sky-400/25',
|
||||
iconClass: 'text-sky-500',
|
||||
textClass: 'text-sky-700 dark:text-sky-300',
|
||||
},
|
||||
{
|
||||
value: 'webhook',
|
||||
label: 'Webhook',
|
||||
desc: t('将提醒推送到外部系统'),
|
||||
icon: Webhook,
|
||||
selectedClass:
|
||||
'border-amber-300 bg-amber-50/82 dark:bg-amber-500/[0.10] dark:border-amber-400/25',
|
||||
iconClass: 'text-amber-500',
|
||||
textClass: 'text-amber-700 dark:text-amber-300',
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div className='space-y-6'>
|
||||
{/* 页面标题 */}
|
||||
<div className='flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 rounded-3xl border border-white/70 dark:border-white/10 bg-white/50 dark:bg-white/[0.045] backdrop-blur-xl px-5 py-4'>
|
||||
<div className='flex items-center gap-3'>
|
||||
<div className='w-11 h-11 rounded-2xl bg-gradient-to-br from-amber-500 to-yellow-400 flex items-center justify-center shadow-lg shadow-amber-500/20'>
|
||||
<Bell className='w-5 h-5 text-white' />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className='text-xl font-bold text-gray-900 dark:text-white'>
|
||||
{t('通知设置')}
|
||||
</h2>
|
||||
<p className='text-sm text-gray-500 dark:text-gray-400'>
|
||||
{t('设置额度预警和系统提醒接收方式')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
theme='solid'
|
||||
onClick={handleSubmit}
|
||||
icon={<Save size={15} />}
|
||||
className='!bg-gradient-to-r !from-amber-500 !to-yellow-400 !border-0 !rounded-xl !shadow-lg !shadow-amber-500/20'
|
||||
>
|
||||
{t('保存设置')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* 表单内容 */}
|
||||
<div className='bg-white/50 dark:bg-white/[0.045] backdrop-blur-xl rounded-[28px] border border-white/70 dark:border-white/10 p-5 sm:p-6'>
|
||||
<Form
|
||||
getFormApi={(api) => (formApiRef.current = api)}
|
||||
initValues={notificationSettings}
|
||||
onSubmit={handleSubmit}
|
||||
>
|
||||
{/* 通知方式选择 */}
|
||||
<div className='mb-6'>
|
||||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3'>
|
||||
{t('通知方式')}
|
||||
</label>
|
||||
<div className='grid grid-cols-1 sm:grid-cols-2 gap-3'>
|
||||
{notifyOptions.map((option) => {
|
||||
const Icon = option.icon;
|
||||
const isSelected =
|
||||
notificationSettings.warningType === option.value;
|
||||
return (
|
||||
<button
|
||||
key={option.value}
|
||||
type='button'
|
||||
onClick={() =>
|
||||
handleFormChange('warningType', option.value)
|
||||
}
|
||||
className={`p-4 rounded-2xl border transition-all text-left ${
|
||||
isSelected
|
||||
? option.selectedClass
|
||||
: 'border-white/70 dark:border-white/10 bg-white/48 dark:bg-white/[0.035] hover:bg-white/72 dark:hover:bg-white/[0.07]'
|
||||
}`}
|
||||
>
|
||||
<div className='flex items-center gap-3'>
|
||||
<div className='w-10 h-10 rounded-2xl bg-white/70 dark:bg-white/[0.07] flex items-center justify-center'>
|
||||
<Icon
|
||||
size={20}
|
||||
className={isSelected ? option.iconClass : 'text-gray-400'}
|
||||
/>
|
||||
</div>
|
||||
<div className='min-w-0'>
|
||||
<div
|
||||
className={`text-sm font-semibold ${
|
||||
isSelected
|
||||
? option.textClass
|
||||
: 'text-gray-700 dark:text-gray-200'
|
||||
}`}
|
||||
>
|
||||
{option.label}
|
||||
</div>
|
||||
<div className='mt-0.5 text-xs text-gray-500 dark:text-gray-400 truncate'>
|
||||
{option.desc}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 预警阈值 */}
|
||||
<FormQuotaInput
|
||||
field='warningThreshold'
|
||||
label={t('额度预警阈值')}
|
||||
onChange={(val) => handleFormChange('warningThreshold', val)}
|
||||
extraText={t(
|
||||
'当剩余额度低于此数值时,系统将通过选择的方式发送通知',
|
||||
)}
|
||||
style={{ maxWidth: '320px', width: '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='mt-4 p-4 bg-sky-50/55 dark:bg-sky-500/[0.07] rounded-2xl border border-sky-100 dark:border-sky-400/20'>
|
||||
<Form.Input
|
||||
field='notificationEmail'
|
||||
label={t('通知邮箱')}
|
||||
placeholder={t('留空则使用账号绑定的邮箱')}
|
||||
onChange={(val) => handleFormChange('notificationEmail', val)}
|
||||
prefix={<IconMail />}
|
||||
extraText={t(
|
||||
'设置用于接收额度预警的邮箱地址,不填则使用账号绑定的邮箱',
|
||||
)}
|
||||
showClear
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Webhook通知设置 */}
|
||||
{notificationSettings.warningType === 'webhook' && (
|
||||
<div className='mt-4 p-4 bg-amber-50/55 dark:bg-amber-500/[0.07] rounded-2xl border border-amber-100 dark:border-amber-400/20 space-y-4'>
|
||||
<Form.Input
|
||||
field='webhookUrl'
|
||||
label={t('Webhook地址')}
|
||||
placeholder={t(
|
||||
'请输入Webhook地址,例如: https://example.com/webhook',
|
||||
)}
|
||||
onChange={(val) => handleFormChange('webhookUrl', val)}
|
||||
prefix={<IconLink />}
|
||||
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('接口凭证')}
|
||||
placeholder={t('请输入密钥')}
|
||||
onChange={(val) => handleFormChange('webhookSecret', val)}
|
||||
prefix={<IconKey />}
|
||||
extraText={t('密钥将以Bearer方式添加到请求头中')}
|
||||
showClear
|
||||
/>
|
||||
|
||||
<div>
|
||||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
||||
{t('Webhook请求结构说明')}
|
||||
</label>
|
||||
<div style={{ height: '180px' }}>
|
||||
<CodeViewer
|
||||
content={{
|
||||
type: 'quota_exceed',
|
||||
title: '额度预警通知',
|
||||
content: '您的额度即将用尽,当前剩余额度为 {{value}}',
|
||||
values: ['¥0.99'],
|
||||
timestamp: 1739950503,
|
||||
}}
|
||||
title='webhook'
|
||||
language='json'
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default NotificationSection;
|
||||
Reference in New Issue
Block a user