整体链接
Change-Id: Id3379c6188613acdc95548964f19e317eda8dc4f
diff --git a/src/views/upload/upload.tsx b/src/views/upload/upload.tsx
index 19e1197..7ec8850 100644
--- a/src/views/upload/upload.tsx
+++ b/src/views/upload/upload.tsx
@@ -5,11 +5,13 @@
import { useLocation, useNavigate } from 'react-router-dom'; // 用于跳转
import { getTagsByMainTag } from '@/utils/common';
import { Checkbox, Row, Col } from 'antd'; // 使用 antd 的 Checkbox 组件
-import { MainTag } from '@/types/common';
+import { MainTag, MainPostTag } from '@/types/common';
import { Select } from 'antd';
import { UploadOutlined } from '@ant-design/icons';
import type { UploadProps } from 'antd';
import { Button, message, Upload as UploadArea } from 'antd';
+import request from '@/utils/request';
+import {useApi} from '@/hooks/request';
const CreatePost = () => {
@@ -22,27 +24,41 @@
const [postType, setPostType] = useState<string>(type || ''); // 初始化 postType
const navigate = useNavigate();
const [tags, setTags] = useState<Map<string, number>>(new Map());
+ const [torrentId, setTorrentId] = useState<number>(0); // 用于存储上传的种子 ID
+ const [messageApi, contextHolder] = message.useMessage();
+
+ const {refresh} = useApi((payload)=> request.post('/post', payload), false)
const props: UploadProps = {
name: 'file',
- action: process.env.REACT_APP_BASE_URL + postTorrentUpload, // 替换为实际的上传接口地址
+ action: process.env.API_BASE_URL + postTorrentUpload, // 替换为实际的上传接口地址
headers: {
- authorization: 'authorization-text',
+ authorization: `Bearer ${localStorage.getItem('token')}`, // 使用本地存储的 token
},
onChange(info) {
- if (info.file.status !== 'uploading') {
- console.log(info.file, info.fileList);
- }
+
if (info.file.status === 'done') {
- message.success(`${info.file.name} file uploaded successfully`);
+ const response = info.file.response;
+ if(response && response.code !== 200) {
+ messageApi.error(response.message || '上传失败');
+ return;
+ }
+ else {
+ console.log('上传成功:', response);
+ messageApi.success('上传成功')
+ console.log(response.data.torrentId);
+ setTorrentId(response.data.torrentId); // 假设返回的数据中有 torrentId
+ }
} else if (info.file.status === 'error') {
- message.error(`${info.file.name} file upload failed.`);
+ messageApi.error(`${info.file.name} 文件上传失败`);
}
},
+
};
const handleSubmit = async () => {
+ console.log(torrentId)
if (!postTitle.trim() || !postType || !postContent.trim()) {
alert('请填写完整内容(资源名、类型、内容介绍)');
return;
@@ -55,33 +71,23 @@
const payload = {
post: {
- postId: 0,
- userId: 0,
postTitle,
postContent,
- createdAt: Date.now(),
- postType,
- viewCount: 0,
- hotScore: 5,
- lastCalculated: Date.now()
+ torrentId,
+ postType:'resource'
},
- tagIds: [0]
+ tagIds: new Array(...tagIds, MainPostTag[postType as keyof typeof MainPostTag]), // 确保 tagIds 是一个数组
};
-
- try {
- const res = await instance.post(Upload, payload);
-
- console.log('后端返回内容:', res.code);
-
- // 判断返回内容是否成功(根据你 mock 接口返回的 code 字段)
- if (res.code !== 0) throw new Error('发布失败');
-
- alert('发布成功!');
- navigate(-1); // 返回上一页(homepage)
- } catch (error) {
- alert('发布失败,请稍后重试');
- console.error(error);
+ try{
+ const res = await refresh(payload)
+ messageApi.success('发布成功', 3).then(()=>{
+ navigate('/')
+ });
+ ; // 发布成功后跳转到首页
+ } catch (err){
+ messageApi.error((err as Error).message || '发布失败,请稍后再试');
}
+
};
useEffect(() => {
@@ -97,6 +103,7 @@
return (
<div className={styles.container}>
+ {contextHolder}
<div className={styles.formGroup}>
<label>资源名:</label>
<input