// src/api/torrent.js | |
import { api } from './auth'; // 复用已有的axios实例 | |
export const createTorrent = (torrentData, file) => { | |
const formData = new FormData(); | |
const token = localStorage.getItem('token'); | |
// 添加文件 | |
if (file) { | |
formData.append('file', file); | |
} | |
// 通过这个方式就可以指定 ContentType 了 | |
formData.append('body', JSON.stringify(torrentData)) | |
console.log(formData); | |
return api.post('/torrent', formData, { | |
headers: { | |
'Content-Type': 'multipart/form-data', | |
'Authorization': `${token}` | |
}, | |
}); | |
}; | |
export const getTorrents = (page = 1, size = 5) => { | |
return api.get('/torrent', { | |
params: { page, size } | |
}); | |
}; | |
export const getTorrentDetail = (torrentId) => { | |
return api.get(`/torrent/${torrentId}`).then(response => { | |
// 确保数据结构一致 | |
if (response.data && response.data.data) { | |
return { | |
...response, | |
data: { | |
...response.data, | |
data: { | |
torrent: response.data.data.torrent, // 直接使用后端返回的格式化数据 | |
comments: response.data.data.comments || [] | |
} | |
} | |
}; | |
} | |
return response; | |
}); | |
}; | |
export const likeTorrent = (torrentId) => { | |
return api.post(`/torrent/${torrentId}/like`); | |
}; | |
export const addTorrentComment = (torrentId, commentData) => { | |
return api.post(`/torrent/${torrentId}/comments`, commentData); | |
}; | |
export const searchTorrents = (keyword, page = 1, size = 5) => { | |
return api.get('/torrent/search', { | |
params: { keyword, page, size } | |
}); | |
}; |