blob: 7ec885076a8a862ac9050449ca080c4c1592f8db [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 组件
San3yuan292794c2025-06-08 20:02:46 +08008import { MainTag, MainPostTag } from '@/types/common';
San3yuan30e245f2025-06-07 20:04:23 +08009import { Select } from 'antd';
10import { UploadOutlined } from '@ant-design/icons';
11import type { UploadProps } from 'antd';
12import { Button, message, Upload as UploadArea } from 'antd';
San3yuan292794c2025-06-08 20:02:46 +080013import request from '@/utils/request';
14import {useApi} from '@/hooks/request';
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080015
San3yuan30e245f2025-06-07 20:04:23 +080016
17const CreatePost = () => {
18 const location = useLocation();
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080019 const [postTitle, setPostTitle] = useState('');
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080020 const [postContent, setPostContent] = useState('');
21 const [isChecked, setIsChecked] = useState(false);
San3yuan30e245f2025-06-07 20:04:23 +080022 const {type} = location.state || {type: ''}; // 获取路由传递的参数
23 const [tagIds, setTagIds] = useState<number[]>([]);
24 const [postType, setPostType] = useState<string>(type || ''); // 初始化 postType
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080025 const navigate = useNavigate();
San3yuan30e245f2025-06-07 20:04:23 +080026 const [tags, setTags] = useState<Map<string, number>>(new Map());
San3yuan292794c2025-06-08 20:02:46 +080027 const [torrentId, setTorrentId] = useState<number>(0); // 用于存储上传的种子 ID
28 const [messageApi, contextHolder] = message.useMessage();
29
30 const {refresh} = useApi((payload)=> request.post('/post', payload), false)
San3yuan30e245f2025-06-07 20:04:23 +080031
32 const props: UploadProps = {
33 name: 'file',
San3yuan292794c2025-06-08 20:02:46 +080034 action: process.env.API_BASE_URL + postTorrentUpload, // 替换为实际的上传接口地址
San3yuan30e245f2025-06-07 20:04:23 +080035 headers: {
San3yuan292794c2025-06-08 20:02:46 +080036 authorization: `Bearer ${localStorage.getItem('token')}`, // 使用本地存储的 token
San3yuan30e245f2025-06-07 20:04:23 +080037 },
38 onChange(info) {
San3yuan292794c2025-06-08 20:02:46 +080039
San3yuan30e245f2025-06-07 20:04:23 +080040 if (info.file.status === 'done') {
San3yuan292794c2025-06-08 20:02:46 +080041 const response = info.file.response;
42 if(response && response.code !== 200) {
43 messageApi.error(response.message || '上传失败');
44 return;
45 }
46 else {
47 console.log('上传成功:', response);
48 messageApi.success('上传成功')
49 console.log(response.data.torrentId);
50 setTorrentId(response.data.torrentId); // 假设返回的数据中有 torrentId
51 }
San3yuan30e245f2025-06-07 20:04:23 +080052 } else if (info.file.status === 'error') {
San3yuan292794c2025-06-08 20:02:46 +080053 messageApi.error(`${info.file.name} 文件上传失败`);
San3yuan30e245f2025-06-07 20:04:23 +080054 }
55 },
San3yuan292794c2025-06-08 20:02:46 +080056
San3yuan30e245f2025-06-07 20:04:23 +080057 };
58
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080059
60 const handleSubmit = async () => {
San3yuan292794c2025-06-08 20:02:46 +080061 console.log(torrentId)
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080062 if (!postTitle.trim() || !postType || !postContent.trim()) {
63 alert('请填写完整内容(资源名、类型、内容介绍)');
64 return;
65 }
66
67 if (!isChecked) {
68 alert('请先确认您已知晓以上内容');
69 return;
70 }
71
72 const payload = {
73 post: {
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080074 postTitle,
75 postContent,
San3yuan292794c2025-06-08 20:02:46 +080076 torrentId,
77 postType:'resource'
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080078 },
San3yuan292794c2025-06-08 20:02:46 +080079 tagIds: new Array(...tagIds, MainPostTag[postType as keyof typeof MainPostTag]), // 确保 tagIds 是一个数组
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080080 };
San3yuan292794c2025-06-08 20:02:46 +080081 try{
82 const res = await refresh(payload)
83 messageApi.success('发布成功', 3).then(()=>{
84 navigate('/')
85 });
86 ; // 发布成功后跳转到首页
87 } catch (err){
88 messageApi.error((err as Error).message || '发布失败,请稍后再试');
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080089 }
San3yuan292794c2025-06-08 20:02:46 +080090
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080091 };
92
San3yuan30e245f2025-06-07 20:04:23 +080093 useEffect(() => {
94 setTags(getTagsByMainTag(type)); // 获取对应类型的标签
95 },[]);
96
97 const handlePostTypeChange = useCallback((value:string) => {
98 setPostType(value);
99 setTags(getTagsByMainTag(value)); // 更新 tags
100 setTagIds([]); // 清空已选的标签 ID
101 }, [postType, setTags, setTagIds]);
102
103
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800104 return (
105 <div className={styles.container}>
San3yuan292794c2025-06-08 20:02:46 +0800106 {contextHolder}
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800107 <div className={styles.formGroup}>
108 <label>资源名:</label>
109 <input
110 type="text"
111 value={postTitle}
112 placeholder="请输入文本"
113 onChange={(e) => setPostTitle(e.target.value)}
114 className={styles.input}
115 />
116 </div>
San3yuan30e245f2025-06-07 20:04:23 +0800117
118 <div className={styles.formGroup}>
119 <label>种子类型:</label>
120 <Select
121 value={postType}
122 onChange={handlePostTypeChange}
123
124 options ={ MainTag.map((item) => ({
125 label: item,
126 value: item
127 }))} />
128 </div>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800129
130 <div className={styles.formGroup}>
131 <label>类型选择:</label>
San3yuan30e245f2025-06-07 20:04:23 +0800132 <Checkbox.Group
133 value={tagIds}
134 onChange={(checkedValues) => setTagIds(checkedValues as number[])}
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800135 >
San3yuan30e245f2025-06-07 20:04:23 +0800136 <Row gutter={[12, 12]}>
137 {[...tags.entries()].map(([name, id]) => (
138 <Col key={id} xs={12} sm={8} md={6} lg={4}>
139 <Checkbox value={id}>{name}</Checkbox>
140 </Col>
141 ))}
142 </Row>
143 </Checkbox.Group>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800144 </div>
145
146 {/* 暂时移除上传文件表单 */}
San3yuan30e245f2025-06-07 20:04:23 +0800147 <div className={styles.formGroup}>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800148 <label>上传资源:</label>
San3yuan30e245f2025-06-07 20:04:23 +0800149 <UploadArea {...props}>
150 <Button icon={<UploadOutlined />}>Upload</Button>
151 </UploadArea>
152 </div>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800153
154 <div className={styles.formGroup}>
155 <label>内容介绍:</label>
156 <textarea
157 placeholder="请输入内容介绍"
158 value={postContent}
159 onChange={(e) => setPostContent(e.target.value)}
160 maxLength={200}
161 className={styles.textarea}
162 />
163 <div className={styles.charCount}>{postContent.length}/200</div>
164 </div>
165
166 <div className={styles.requirement}>【发布内容要求】</div>
167
168 <div className={styles.checkbox}>
169 <input
170 type="checkbox"
171 checked={isChecked}
172 onChange={() => setIsChecked(!isChecked)}
173 />
174 <span>我已知晓以上内容</span>
175 </div>
176
San3yuan30e245f2025-06-07 20:04:23 +0800177 <button onClick={handleSubmit} className={styles.submitBtn} disabled={!isChecked}>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800178 我已知晓
179 </button>
180 </div>
181 );
182};
183
San3yuan30e245f2025-06-07 20:04:23 +0800184export default CreatePost;