blob: 8a00e545fedf2bd2d9ef5f8a584d3502fb93b776 [file] [log] [blame]
Akane121765b61a72025-05-17 13:52:25 +08001// src/api/torrent.js
2import { api } from './auth'; // 复用已有的axios实例
3
22301080a93bebb2025-05-27 19:48:11 +08004export const createTorrent = (torrentData, file) => {
5 const formData = new FormData();
6 const token = localStorage.getItem('token');
7
8 // 添加文件
9 if (file) {
10 formData.append('file', file);
11 }
12 // 通过这个方式就可以指定 ContentType 了
13 formData.append('body', JSON.stringify(torrentData))
14
15 console.log(formData);
16 return api.post('/torrent', formData, {
17 headers: {
18 'Content-Type': 'multipart/form-data',
19 'Authorization': `${token}`
20 },
Akane121765b61a72025-05-17 13:52:25 +080021 });
22};
23
24export const getTorrents = (page = 1, size = 5) => {
25 return api.get('/torrent', {
26 params: { page, size }
27 });
28};
29
30export const getTorrentDetail = (torrentId) => {
31 return api.get(`/torrent/${torrentId}`).then(response => {
32 // 确保数据结构一致
33 if (response.data && response.data.data) {
34 return {
35 ...response,
36 data: {
37 ...response.data,
38 data: {
Akane12173a7bb972025-06-01 01:05:27 +080039 torrent: response.data.data.torrent, // 直接使用后端返回的格式化数据
Akane121765b61a72025-05-17 13:52:25 +080040 comments: response.data.data.comments || []
41 }
42 }
43 };
44 }
45 return response;
46 });
47};
48
49export const likeTorrent = (torrentId) => {
50 return api.post(`/torrent/${torrentId}/like`);
51};
52
53export const addTorrentComment = (torrentId, commentData) => {
54 return api.post(`/torrent/${torrentId}/comments`, commentData);
Akane12173a7bb972025-06-01 01:05:27 +080055};
56
57export const searchTorrents = (keyword, page = 1, size = 5) => {
58 return api.get('/torrent/search', {
59 params: { keyword, page, size }
60 });
Akane121765b61a72025-05-17 13:52:25 +080061};