blob: 19e11976f52de8891cb15f419204c4cc9b8a596b [file] [log] [blame]
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +08001import instance from '@/utils/axios';
San3yuan30e245f2025-06-07 20:04:23 +08002import React, { useCallback, useEffect, useState } from 'react';
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +08003import styles from './upload.module.css';
San3yuan30e245f2025-06-07 20:04:23 +08004import {postTorrentUpload } from '@/api/torrent';
5import { useLocation, useNavigate } from 'react-router-dom'; // 用于跳转
6import { getTagsByMainTag } from '@/utils/common';
7import { Checkbox, Row, Col } from 'antd'; // 使用 antd 的 Checkbox 组件
8import { MainTag } from '@/types/common';
9import { Select } from 'antd';
10import { UploadOutlined } from '@ant-design/icons';
11import type { UploadProps } from 'antd';
12import { Button, message, Upload as UploadArea } from 'antd';
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080013
San3yuan30e245f2025-06-07 20:04:23 +080014
15const CreatePost = () => {
16 const location = useLocation();
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080017 const [postTitle, setPostTitle] = useState('');
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080018 const [postContent, setPostContent] = useState('');
19 const [isChecked, setIsChecked] = useState(false);
San3yuan30e245f2025-06-07 20:04:23 +080020 const {type} = location.state || {type: ''}; // 获取路由传递的参数
21 const [tagIds, setTagIds] = useState<number[]>([]);
22 const [postType, setPostType] = useState<string>(type || ''); // 初始化 postType
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080023 const navigate = useNavigate();
San3yuan30e245f2025-06-07 20:04:23 +080024 const [tags, setTags] = useState<Map<string, number>>(new Map());
25
26 const props: UploadProps = {
27 name: 'file',
28 action: process.env.REACT_APP_BASE_URL + postTorrentUpload, // 替换为实际的上传接口地址
29 headers: {
30 authorization: 'authorization-text',
31 },
32 onChange(info) {
33 if (info.file.status !== 'uploading') {
34 console.log(info.file, info.fileList);
35 }
36 if (info.file.status === 'done') {
37 message.success(`${info.file.name} file uploaded successfully`);
38 } else if (info.file.status === 'error') {
39 message.error(`${info.file.name} file upload failed.`);
40 }
41 },
42 };
43
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080044
45 const handleSubmit = async () => {
46 if (!postTitle.trim() || !postType || !postContent.trim()) {
47 alert('请填写完整内容(资源名、类型、内容介绍)');
48 return;
49 }
50
51 if (!isChecked) {
52 alert('请先确认您已知晓以上内容');
53 return;
54 }
55
56 const payload = {
57 post: {
58 postId: 0,
59 userId: 0,
60 postTitle,
61 postContent,
62 createdAt: Date.now(),
63 postType,
64 viewCount: 0,
65 hotScore: 5,
66 lastCalculated: Date.now()
67 },
68 tagIds: [0]
69 };
70
71 try {
72 const res = await instance.post(Upload, payload);
73
阳菜,放晴!54d564d2025-06-08 00:02:22 +080074 console.log('后端返回内容:', res.code);
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080075
76 // 判断返回内容是否成功(根据你 mock 接口返回的 code 字段)
77 if (res.code !== 0) throw new Error('发布失败');
78
79 alert('发布成功!');
80 navigate(-1); // 返回上一页(homepage)
81 } catch (error) {
82 alert('发布失败,请稍后重试');
83 console.error(error);
84 }
85 };
86
San3yuan30e245f2025-06-07 20:04:23 +080087 useEffect(() => {
88 setTags(getTagsByMainTag(type)); // 获取对应类型的标签
89 },[]);
90
91 const handlePostTypeChange = useCallback((value:string) => {
92 setPostType(value);
93 setTags(getTagsByMainTag(value)); // 更新 tags
94 setTagIds([]); // 清空已选的标签 ID
95 }, [postType, setTags, setTagIds]);
96
97
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080098 return (
99 <div className={styles.container}>
100 <div className={styles.formGroup}>
101 <label>资源名:</label>
102 <input
103 type="text"
104 value={postTitle}
105 placeholder="请输入文本"
106 onChange={(e) => setPostTitle(e.target.value)}
107 className={styles.input}
108 />
109 </div>
San3yuan30e245f2025-06-07 20:04:23 +0800110
111 <div className={styles.formGroup}>
112 <label>种子类型:</label>
113 <Select
114 value={postType}
115 onChange={handlePostTypeChange}
116
117 options ={ MainTag.map((item) => ({
118 label: item,
119 value: item
120 }))} />
121 </div>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800122
123 <div className={styles.formGroup}>
124 <label>类型选择:</label>
San3yuan30e245f2025-06-07 20:04:23 +0800125 <Checkbox.Group
126 value={tagIds}
127 onChange={(checkedValues) => setTagIds(checkedValues as number[])}
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800128 >
San3yuan30e245f2025-06-07 20:04:23 +0800129 <Row gutter={[12, 12]}>
130 {[...tags.entries()].map(([name, id]) => (
131 <Col key={id} xs={12} sm={8} md={6} lg={4}>
132 <Checkbox value={id}>{name}</Checkbox>
133 </Col>
134 ))}
135 </Row>
136 </Checkbox.Group>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800137 </div>
138
139 {/* 暂时移除上传文件表单 */}
San3yuan30e245f2025-06-07 20:04:23 +0800140 <div className={styles.formGroup}>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800141 <label>上传资源:</label>
San3yuan30e245f2025-06-07 20:04:23 +0800142 <UploadArea {...props}>
143 <Button icon={<UploadOutlined />}>Upload</Button>
144 </UploadArea>
145 </div>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800146
147 <div className={styles.formGroup}>
148 <label>内容介绍:</label>
149 <textarea
150 placeholder="请输入内容介绍"
151 value={postContent}
152 onChange={(e) => setPostContent(e.target.value)}
153 maxLength={200}
154 className={styles.textarea}
155 />
156 <div className={styles.charCount}>{postContent.length}/200</div>
157 </div>
158
159 <div className={styles.requirement}>【发布内容要求】</div>
160
161 <div className={styles.checkbox}>
162 <input
163 type="checkbox"
164 checked={isChecked}
165 onChange={() => setIsChecked(!isChecked)}
166 />
167 <span>我已知晓以上内容</span>
168 </div>
169
San3yuan30e245f2025-06-07 20:04:23 +0800170 <button onClick={handleSubmit} className={styles.submitBtn} disabled={!isChecked}>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800171 我已知晓
172 </button>
173 </div>
174 );
175};
176
San3yuan30e245f2025-06-07 20:04:23 +0800177export default CreatePost;