Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | // src/api/torrent.js
|
| 2 | import { api } from './auth'; // 复用已有的axios实例
|
| 3 |
|
22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame] | 4 | export 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 | },
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 21 | });
|
| 22 | };
|
| 23 |
|
| 24 | export const getTorrents = (page = 1, size = 5) => {
|
| 25 | return api.get('/torrent', {
|
| 26 | params: { page, size }
|
| 27 | });
|
| 28 | };
|
| 29 |
|
| 30 | export 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: {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 39 | torrent: response.data.data.torrent, // 直接使用后端返回的格式化数据
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 40 | comments: response.data.data.comments || []
|
| 41 | }
|
| 42 | }
|
| 43 | };
|
| 44 | }
|
| 45 | return response;
|
| 46 | });
|
| 47 | };
|
| 48 |
|
| 49 | export const likeTorrent = (torrentId) => {
|
| 50 | return api.post(`/torrent/${torrentId}/like`);
|
| 51 | };
|
| 52 |
|
| 53 | export const addTorrentComment = (torrentId, commentData) => {
|
| 54 | return api.post(`/torrent/${torrentId}/comments`, commentData);
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 55 | };
|
| 56 |
|
| 57 | export const searchTorrents = (keyword, page = 1, size = 5) => {
|
| 58 | return api.get('/torrent/search', {
|
| 59 | params: { keyword, page, size }
|
| 60 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 61 | }; |