|
|
@@ -0,0 +1,172 @@
|
|
|
+import React, { useState, useEffect } from 'react';
|
|
|
+import { Form, Input, Select, Button, message } from 'antd';
|
|
|
+
|
|
|
+const { TextArea } = Input;
|
|
|
+const { Option } = Select;
|
|
|
+
|
|
|
+const KNOWLEDGE_TYPES = [
|
|
|
+ { value: 1, label: '文章' },
|
|
|
+ { value: 2, label: '视频' },
|
|
|
+ { value: 3, label: '文档' },
|
|
|
+ { value: 4, label: '课程' },
|
|
|
+ { value: 5, label: '经验分享' },
|
|
|
+ { value: 6, label: '案例分享' },
|
|
|
+ { value: 7, label: '研究报告' }
|
|
|
+];
|
|
|
+
|
|
|
+interface PublishKnowledgeFormProps {
|
|
|
+ onSuccess: () => void;
|
|
|
+ onCancel: () => void;
|
|
|
+}
|
|
|
+
|
|
|
+const PublishKnowledgeForm: React.FC<PublishKnowledgeFormProps> = ({ onSuccess, onCancel }) => {
|
|
|
+ const [form] = Form.useForm();
|
|
|
+ const [loading, setLoading] = useState(false);
|
|
|
+ const [categories, setCategories] = useState<Array<{id: number; name: string}>>([]);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const fetchCategories = async () => {
|
|
|
+ try {
|
|
|
+ const response = await fetch('/api/v1/silver-users/knowledge-categories');
|
|
|
+ const data = await response.json();
|
|
|
+ setCategories(data.data || []);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取分类失败:', error);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ fetchCategories();
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const handleSubmit = async (values: any) => {
|
|
|
+ setLoading(true);
|
|
|
+ try {
|
|
|
+ // 获取当前用户信息
|
|
|
+ const userResponse = await fetch('/api/v1/auth/me');
|
|
|
+ const userData = await userResponse.json();
|
|
|
+
|
|
|
+ if (!userData.data?.id) {
|
|
|
+ message.error('请先登录');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const formData = {
|
|
|
+ userId: userData.data.id,
|
|
|
+ title: values.title,
|
|
|
+ content: values.content,
|
|
|
+ type: values.type,
|
|
|
+ categoryId: values.categoryId || null,
|
|
|
+ summary: values.summary || null,
|
|
|
+ keywords: values.keywords || null,
|
|
|
+ source: values.source || null,
|
|
|
+ author: values.author || null,
|
|
|
+ tags: values.tags ? JSON.stringify(values.tags.split(',').map((tag: string) => tag.trim())) : null,
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await fetch('/api/v1/silver-users/knowledges', {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ },
|
|
|
+ body: JSON.stringify(formData),
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response.json();
|
|
|
+
|
|
|
+ if (response.ok) {
|
|
|
+ message.success('智库发布成功!');
|
|
|
+ form.resetFields();
|
|
|
+ onSuccess();
|
|
|
+ } else {
|
|
|
+ message.error(result.message || '发布失败');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ message.error('发布失败,请重试');
|
|
|
+ } finally {
|
|
|
+ setLoading(false);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Form
|
|
|
+ form={form}
|
|
|
+ layout="vertical"
|
|
|
+ onFinish={handleSubmit}
|
|
|
+ >
|
|
|
+ <Form.Item
|
|
|
+ label="知识标题"
|
|
|
+ name="title"
|
|
|
+ rules={[{ required: true, message: '请输入知识标题' }]}
|
|
|
+ >
|
|
|
+ <Input placeholder="请输入知识标题" />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item
|
|
|
+ label="知识类型"
|
|
|
+ name="type"
|
|
|
+ rules={[{ required: true, message: '请选择知识类型' }]}
|
|
|
+ >
|
|
|
+ <Select placeholder="请选择知识类型">
|
|
|
+ {KNOWLEDGE_TYPES.map(type => (
|
|
|
+ <Option key={type.value} value={type.value}>
|
|
|
+ {type.label}
|
|
|
+ </Option>
|
|
|
+ ))}
|
|
|
+ </Select>
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item label="知识分类" name="categoryId">
|
|
|
+ <Select placeholder="请选择知识分类" allowClear>
|
|
|
+ {categories.map(category => (
|
|
|
+ <Option key={category.id} value={category.id}>
|
|
|
+ {category.name}
|
|
|
+ </Option>
|
|
|
+ ))}
|
|
|
+ </Select>
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item
|
|
|
+ label="知识内容"
|
|
|
+ name="content"
|
|
|
+ rules={[{ required: true, message: '请输入知识内容' }]}
|
|
|
+ >
|
|
|
+ <TextArea rows={6} placeholder="请详细描述您的知识内容..." />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item label="知识摘要" name="summary">
|
|
|
+ <TextArea rows={3} placeholder="请输入知识摘要(可选)" maxLength={200} />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item label="关键词" name="keywords">
|
|
|
+ <Input placeholder="健康,养生,老年人" />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item label="标签" name="tags">
|
|
|
+ <Input placeholder="用逗号分隔多个标签,如:健康养生,老年人" />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item label="知识来源" name="source">
|
|
|
+ <Input placeholder="如:中国老年学会" />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item label="原作者" name="author">
|
|
|
+ <Input placeholder="请输入原作者" />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <div style={{ display: 'flex', gap: '16px', marginTop: '24px' }}>
|
|
|
+ <Button onClick={onCancel} style={{ flex: 1 }}>
|
|
|
+ 取消
|
|
|
+ </Button>
|
|
|
+ <Button
|
|
|
+ type="primary"
|
|
|
+ htmlType="submit"
|
|
|
+ loading={loading}
|
|
|
+ style={{ flex: 1 }}
|
|
|
+ >
|
|
|
+ 发布智库
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ </Form>
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
+export default PublishKnowledgeForm;
|