2025-06-18 14:37:42 +08:00
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import { Form, Input, Switch, Tabs, message, Button, Select } from 'antd';
|
2025-06-18 16:37:25 +08:00
|
|
|
|
import { useIntl } from 'umi';
|
2025-06-18 14:37:42 +08:00
|
|
|
|
import WangEditor from '@/components/WangEidtor/WangEidtor';
|
|
|
|
|
import { TopStatus, EnglishSetting, categoryOptions } from '@/dicts/helpManageDict';
|
|
|
|
|
import { addHelp, updateHelp, getHelpDetail } from '@/servers/api/help';
|
2025-06-23 08:33:14 +08:00
|
|
|
|
import styles from './helpManage.less';
|
2025-06-18 14:37:42 +08:00
|
|
|
|
|
|
|
|
|
const { TabPane } = Tabs;
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
|
|
|
|
|
interface HelpManageFormProps {
|
|
|
|
|
id?: string;
|
|
|
|
|
isEdit: boolean;
|
|
|
|
|
onSuccess: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HelpManageForm: React.FC<HelpManageFormProps> = ({ id, isEdit, onSuccess }) => {
|
|
|
|
|
const [form] = Form.useForm();
|
|
|
|
|
const [activeTabKey, setActiveTabKey] = useState<string>('zh');
|
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
2025-06-18 16:37:25 +08:00
|
|
|
|
const intl = useIntl();
|
2025-06-18 14:37:42 +08:00
|
|
|
|
|
|
|
|
|
// 获取详情数据
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchHelpDetail = async (helpId: string) => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
const response = await getHelpDetail(helpId);
|
|
|
|
|
if (response && response.success) {
|
|
|
|
|
const detail = response.data;
|
|
|
|
|
form.setFieldsValue({
|
|
|
|
|
isTop: detail.isTop === TopStatus.YES,
|
|
|
|
|
type: detail.type,
|
|
|
|
|
titleZh: detail.title,
|
|
|
|
|
titleEn: detail.titleEn,
|
|
|
|
|
contentZh: detail.content,
|
|
|
|
|
contentEn: detail.contentEn,
|
|
|
|
|
answerContentZh: detail.answerContent,
|
|
|
|
|
answerContentEn: detail.answerContentNe,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2025-06-18 16:37:25 +08:00
|
|
|
|
message.error(response.message || intl.formatMessage({ id: 'helpManage.fetchDetailFailed' }));
|
2025-06-18 14:37:42 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取帮助详情失败:', error);
|
2025-06-18 16:37:25 +08:00
|
|
|
|
message.error(intl.formatMessage({ id: 'helpManage.fetchDetailFailed' }));
|
2025-06-18 14:37:42 +08:00
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (isEdit && id) {
|
|
|
|
|
fetchHelpDetail(id);
|
|
|
|
|
}
|
2025-06-18 16:37:25 +08:00
|
|
|
|
}, [isEdit, id, form, intl]);
|
2025-06-18 14:37:42 +08:00
|
|
|
|
|
|
|
|
|
// 处理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' || fieldName === 'answerContentZh') {
|
|
|
|
|
setActiveTabKey('zh');
|
|
|
|
|
} else if (fieldName === 'titleEn' || fieldName === 'contentEn' || fieldName === 'answerContentEn') {
|
|
|
|
|
setActiveTabKey('en');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取所有表单值
|
|
|
|
|
const values = form.getFieldsValue();
|
|
|
|
|
|
|
|
|
|
// 转换表单数据为API需要的格式
|
|
|
|
|
const requestData = {
|
|
|
|
|
title: values.titleZh,
|
|
|
|
|
titleEn: values.titleEn || '',
|
|
|
|
|
content: values.contentZh,
|
|
|
|
|
contentEn: values.contentEn || '',
|
|
|
|
|
type: values.type,
|
|
|
|
|
isTop: values.isTop ? TopStatus.YES : TopStatus.NO,
|
|
|
|
|
settingEn: values.titleEn && values.contentEn ? EnglishSetting.YES : EnglishSetting.NO,
|
|
|
|
|
answerContent: values.answerContentZh || '',
|
|
|
|
|
answerContentNe: values.answerContentEn || '',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let response;
|
|
|
|
|
if (isEdit && id) {
|
|
|
|
|
response = await updateHelp(id, requestData);
|
|
|
|
|
} else {
|
|
|
|
|
response = await addHelp(requestData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response && response.success) {
|
2025-06-18 16:37:25 +08:00
|
|
|
|
message.success(isEdit
|
|
|
|
|
? intl.formatMessage({ id: 'helpManage.updateSuccess' })
|
|
|
|
|
: intl.formatMessage({ id: 'helpManage.addSuccess' }));
|
2025-06-18 14:37:42 +08:00
|
|
|
|
onSuccess(); // 回调父组件
|
|
|
|
|
} else {
|
2025-06-18 16:37:25 +08:00
|
|
|
|
message.error(response.message || (isEdit
|
|
|
|
|
? intl.formatMessage({ id: 'helpManage.updateFailed' })
|
|
|
|
|
: intl.formatMessage({ id: 'helpManage.addFailed' })));
|
2025-06-18 14:37:42 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('表单验证或提交失败:', error);
|
|
|
|
|
// 错误已在前面处理
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-06-23 08:33:14 +08:00
|
|
|
|
<div className={styles.helpForm}>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Form form={form} layout="vertical" name="helpForm" preserve={false}>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<Form.Item name="isTop" label={intl.formatMessage({ id: 'helpManage.form.isTop' })} valuePropName="checked">
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Switch />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
name="type"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'helpManage.form.category' })}
|
|
|
|
|
rules={[{ required: true, message: intl.formatMessage({ id: 'helpManage.form.categoryRequired' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<Select placeholder={intl.formatMessage({ id: 'helpManage.form.categoryPlaceholder' })}>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
{categoryOptions.map(option => (
|
|
|
|
|
<Option key={option.value} value={option.value}>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
{typeof option.label === 'function' ? option.label() : option.label}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Option>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Tabs activeKey={activeTabKey} onChange={handleTabChange}>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<TabPane tab={intl.formatMessage({ id: 'helpManage.form.chineseTab' })} key="zh" forceRender={true}>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
name="titleZh"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'helpManage.form.titleZh' })}
|
|
|
|
|
rules={[{ required: true, message: intl.formatMessage({ id: 'helpManage.form.titleZhRequired' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<Input placeholder={intl.formatMessage({ id: 'helpManage.form.titleZhPlaceholder' })} />
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
name="contentZh"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'helpManage.form.contentZh' })}
|
|
|
|
|
rules={[{ required: true, message: intl.formatMessage({ id: 'helpManage.form.contentZhRequired' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<WangEditor
|
|
|
|
|
language="zh-CN"
|
|
|
|
|
height="250px"
|
|
|
|
|
placeholder={intl.formatMessage({ id: 'helpManage.form.contentZhPlaceholder' })}
|
|
|
|
|
/>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
name="answerContentZh"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'helpManage.form.answerContentZh' })}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<WangEditor
|
|
|
|
|
language="zh-CN"
|
|
|
|
|
height="200px"
|
|
|
|
|
placeholder={intl.formatMessage({ id: 'helpManage.form.answerContentZhPlaceholder' })}
|
|
|
|
|
/>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
</TabPane>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<TabPane tab={intl.formatMessage({ id: 'helpManage.form.englishTab' })} key="en" forceRender={true}>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
name="titleEn"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'helpManage.form.titleEn' })}
|
|
|
|
|
rules={[{ required: true, message: intl.formatMessage({ id: 'helpManage.form.titleEnRequired' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
<Input placeholder={intl.formatMessage({ id: 'helpManage.form.titleEnPlaceholder' })} />
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
name="contentEn"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'helpManage.form.contentEn' })}
|
|
|
|
|
rules={[{ required: true, message: intl.formatMessage({ id: 'helpManage.form.contentEnRequired' }) }]}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
|
|
|
|
<WangEditor
|
|
|
|
|
language="en"
|
|
|
|
|
height="250px"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
placeholder={intl.formatMessage({ id: 'helpManage.form.contentEnPlaceholder' })}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
/>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
name="answerContentEn"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
label={intl.formatMessage({ id: 'helpManage.form.answerContentEn' })}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
>
|
|
|
|
|
<WangEditor
|
|
|
|
|
language="en"
|
|
|
|
|
height="200px"
|
2025-06-18 16:37:25 +08:00
|
|
|
|
placeholder={intl.formatMessage({ id: 'helpManage.form.answerContentEnPlaceholder' })}
|
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-18 16:37:25 +08:00
|
|
|
|
<Button onClick={() => onSuccess()}>
|
|
|
|
|
{intl.formatMessage({ id: 'helpManage.cancel' })}
|
|
|
|
|
</Button>
|
2025-06-18 14:37:42 +08:00
|
|
|
|
<Button
|
|
|
|
|
type="primary"
|
|
|
|
|
loading={loading}
|
|
|
|
|
onClick={handleSubmit}
|
|
|
|
|
>
|
2025-06-18 16:37:25 +08:00
|
|
|
|
{intl.formatMessage({ id: 'helpManage.submit' })}
|
2025-06-18 14:37:42 +08:00
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default HelpManageForm;
|