2025-06-18 14:37:42 +08:00
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import { Form, Input, Switch, Tabs, message, Button } from 'antd';
|
2025-06-30 19:00:57 +08:00
|
|
|
|
import { useIntl } from 'umi';
|
2025-06-18 14:37:42 +08:00
|
|
|
|
import WangEditor from '@/components/WangEidtor/WangEidtor';
|
|
|
|
|
import { TopStatus, EnglishSetting } from '@/dicts/noticeManageDict';
|
|
|
|
|
import { addNotice, updateNotice, getNoticeDetail } from '@/servers/api/notice';
|
2025-06-23 08:33:14 +08:00
|
|
|
|
import styles from './noticeManage.less';
|
2025-06-18 14:37:42 +08:00
|
|
|
|
|
|
|
|
|
const { TabPane } = Tabs;
|
|
|
|
|
|
|
|
|
|
interface NoticeManageFormProps {
|
|
|
|
|
id?: string;
|
|
|
|
|
isEdit: boolean;
|
|
|
|
|
onSuccess: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const NoticeManageForm: React.FC<NoticeManageFormProps> = ({ id, isEdit, onSuccess }) => {
|
2025-06-30 19:00:57 +08:00
|
|
|
|
const intl = useIntl();
|
2025-06-18 14:37:42 +08:00
|
|
|
|
const [form] = Form.useForm();
|
|
|
|
|
const [activeTabKey, setActiveTabKey] = useState<string>('zh');
|
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
// 获取详情数据
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchNoticeDetail = async (noticeId: string) => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
const response = await getNoticeDetail(noticeId);
|
|
|
|
|
if (response && response.success) {
|
|
|
|
|
const detail = response.data;
|
|
|
|
|
form.setFieldsValue({
|
|
|
|
|
isTop: detail.isTop === TopStatus.YES,
|
|
|
|
|
titleZh: detail.title,
|
|
|
|
|
titleEn: detail.titleEn,
|
|
|
|
|
contentZh: detail.content,
|
|
|
|
|
contentEn: detail.contentEn,
|
2025-06-30 18:46:50 +08:00
|
|
|
|
settingEn: detail.settingEn === '1',
|
2025-06-18 14:37:42 +08:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
message.error(response.message || '获取详情失败');
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取通知详情失败:', error);
|
|
|
|
|
message.error('获取详情失败');
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (isEdit && id) {
|
|
|
|
|
fetchNoticeDetail(id);
|
|
|
|
|
}
|
|
|
|
|
}, [isEdit, id, form]);
|
|
|
|
|
|
|
|
|
|
// 处理Tab切换
|
|
|
|
|
const handleTabChange = (key: string) => {
|
|
|
|
|
setActiveTabKey(key);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 提交表单
|
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
// 一次性验证所有表单项
|
|
|
|
|
try {
|
|
|
|
|
await form.validateFields();
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
// 如果有验证错误,自动切换到相应Tab
|
|
|
|
|
if (error.errorFields && error.errorFields.length > 0) {
|
|
|
|
|
// 获取第一个错误字段
|
|
|
|
|
const firstError = error.errorFields[0];
|
|
|
|
|
const fieldName = firstError.name[0];
|
|
|
|
|
|
|
|
|
|
// 根据字段名判断应该切换到哪个Tab
|
|
|
|
|
if (fieldName === 'titleZh' || fieldName === 'contentZh') {
|
|
|
|
|
setActiveTabKey('zh');
|
|
|
|
|
} else if (fieldName === 'titleEn' || fieldName === 'contentEn') {
|
|
|
|
|
setActiveTabKey('en');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 19:00:57 +08:00
|
|
|
|
message.error(intl.formatMessage({ id: 'notice.message.validate.fail' }));
|
2025-06-18 14:37:42 +08:00
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取所有表单值
|
|
|
|
|
const values = form.getFieldsValue();
|
|
|
|
|
|
|
|
|
|
// 转换表单数据为API需要的格式
|
|
|
|
|
const requestData = {
|
|
|
|
|
title: values.titleZh,
|
|
|
|
|
titleEn: values.titleEn || '',
|
|
|
|
|
content: values.contentZh,
|
|
|
|
|
contentEn: values.contentEn || '',
|
|
|
|
|
isTop: values.isTop ? TopStatus.YES : TopStatus.NO,
|
2025-06-30 18:46:50 +08:00
|
|
|
|
settingEn: values.settingEn ? EnglishSetting.YES : EnglishSetting.NO,
|
2025-06-18 14:37:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let response;
|
|
|
|
|
if (isEdit && id) {
|
|
|
|
|
response = await updateNotice(id, requestData);
|
|
|
|
|
} else {
|
|
|
|
|
response = await addNotice(requestData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response && response.success) {
|
2025-06-30 19:00:57 +08:00
|
|
|
|
message.success(intl.formatMessage({
|
|
|
|
|
id: isEdit ? 'notice.message.update.success' : 'notice.message.add.success'
|
|
|
|
|
}));
|
2025-06-18 14:37:42 +08:00
|
|
|
|
onSuccess(); // 回调父组件
|
|
|
|
|
} else {
|
2025-06-30 19:00:57 +08:00
|
|
|
|
message.error(response.message || intl.formatMessage({
|
|
|
|
|
id: isEdit ? 'notice.message.update.fail' : 'notice.message.add.fail'
|
|
|
|
|
}));
|
2025-06-18 14:37:42 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('表单验证或提交失败:', error);
|
|
|
|
|
// 错误已在前面处理
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<Form form={form} layout="vertical" name="noticeForm" preserve={false}>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<Form.Item name="isTop" label={intl.formatMessage({ id: 'notice.field.isTop' })} valuePropName="checked">
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Switch />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<Form.Item name="settingEn" label={intl.formatMessage({ id: 'notice.field.settingEn' })} valuePropName="checked">
|
2025-06-30 18:46:50 +08:00
|
|
|
|
<Switch />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Tabs activeKey={activeTabKey} onChange={handleTabChange}>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<TabPane tab={intl.formatMessage({ id: 'notice.tab.chinese' })} key="zh" forceRender={true}>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
name="titleZh"
|
2025-06-30 19:00:57 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'notice.field.title' })}
|
|
|
|
|
rules={[{ required: true, message: intl.formatMessage({ id: 'notice.form.title.required' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<Input placeholder={intl.formatMessage({ id: 'notice.form.title.placeholder' })} />
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
name="contentZh"
|
2025-06-30 19:00:57 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'notice.field.content' })}
|
|
|
|
|
rules={[{ required: true, message: intl.formatMessage({ id: 'notice.form.content.required' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<WangEditor
|
|
|
|
|
language="zh-CN"
|
|
|
|
|
height="300px"
|
|
|
|
|
placeholder={intl.formatMessage({ id: 'notice.form.content.placeholder' })}
|
|
|
|
|
/>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
</TabPane>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<TabPane tab={intl.formatMessage({ id: 'notice.tab.english' })} key="en" forceRender={true}>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
name="titleEn"
|
2025-06-30 19:00:57 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'notice.field.titleEn' })}
|
|
|
|
|
rules={[{ required: form.getFieldValue('settingEn'), message: intl.formatMessage({ id: 'notice.form.titleEn.required' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<Input placeholder={intl.formatMessage({ id: 'notice.form.titleEn.placeholder' })} />
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
name="contentEn"
|
2025-06-30 19:00:57 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'notice.field.contentEn' })}
|
|
|
|
|
rules={[{ required: form.getFieldValue('settingEn'), message: intl.formatMessage({ id: 'notice.form.contentEn.required' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
|
|
|
|
<WangEditor
|
|
|
|
|
language="en"
|
|
|
|
|
height="300px"
|
2025-06-30 19:00:57 +08:00
|
|
|
|
placeholder={intl.formatMessage({ id: 'notice.form.contentEn.placeholder' })}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
/>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
</TabPane>
|
|
|
|
|
</Tabs>
|
|
|
|
|
</Form>
|
2025-06-23 08:33:14 +08:00
|
|
|
|
<div className={styles.formActions}>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
<Button onClick={() => onSuccess()}>
|
|
|
|
|
{intl.formatMessage({ id: 'notice.button.cancel' })}
|
|
|
|
|
</Button>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Button
|
|
|
|
|
type="primary"
|
|
|
|
|
loading={loading}
|
|
|
|
|
onClick={handleSubmit}
|
|
|
|
|
>
|
2025-06-30 19:00:57 +08:00
|
|
|
|
{intl.formatMessage({ id: 'notice.button.submit' })}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default NoticeManageForm;
|