完成上传下载连接,公告管理与详情页面,求种区页面,轮播图折扣显示,修改部分bug

Change-Id: I86fc294e32911cb3426a8b16f90aca371f975c11
diff --git a/src/api/torrent.js b/src/api/torrent.js
index 8a00e54..4a623d7 100644
--- a/src/api/torrent.js
+++ b/src/api/torrent.js
@@ -1,26 +1,74 @@
 // src/api/torrent.js

 import { api } from './auth'; // 复用已有的axios实例

 

-export const createTorrent = (torrentData, file) => {

+/**

+ * 创建并上传一个种子

+ * @param {File} file 种子文件 (.torrent)

+ * @param {Object} bodyObj 包含种子信息的对象,格式如下:

+ * {

+ *   torrentName: "测试下载",

+ *   description: "A high-quality 1080p version of Example Movie.",

+ *   category: "电影",

+ *   region: "USA",

+ *   resolution: "1080p",

+ *   subtitle: "English",

+ *   filePath: "D:/大学/大三_下/torrentFrom/[电影天堂www.dytt89.com]两杆大烟枪BD中英双字.mp4.torrent"

+ * }

+ */

+export async function createTorrent(file, bodyObj) {

   const formData = new FormData();

-  const token = localStorage.getItem('token');

+  formData.append('file', file);

 

-  // 添加文件

-  if (file) {

-    formData.append('file', file);

+  // 关键修改:将JSON字符串转换为Blob对象,并设置正确的Content-Type

+  formData.append('body', new Blob([JSON.stringify(bodyObj)], {

+    type: 'application/json'

+  }));

+

+  try {

+    const response = await api.post('/torrent', formData, {

+      headers: {

+      }

+    });

+    return response.data;

+  } catch (error) {

+    console.error('上传种子失败:', error);

+    throw error;

   }

-  // 通过这个方式就可以指定 ContentType 了

-  formData.append('body', JSON.stringify(torrentData))

-

-  console.log(formData);

-  return api.post('/torrent', formData, {

-    headers: {

-      'Content-Type': 'multipart/form-data',

-      'Authorization': `${token}`

-    },

-  });

 };

 

+/**

+ * 下载种子文件

+ * @param {number} torrentId 种子ID

+ * @param {string} downloadPath 下载路径

+ */

+export async function downloadTorrent(torrentId, downloadPath) {

+  try {

+    const response = await api.get(`/torrent/downloadTorrent`, {

+      params: {

+        id: torrentId,

+        downloadPath: downloadPath

+      }

+    });

+    return response.data;

+  } catch (error) {

+    console.error('下载种子失败:', error);

+    throw error;

+  }

+}

+

+/**

+ * 获取下载进度

+ */

+export async function getDownloadProgress() {

+  try {

+    const response = await api.get('/torrent/getProgress');

+    return response.data;

+  } catch (error) {

+    console.error('获取下载进度失败:', error);

+    throw error;

+  }

+}

+

 export const getTorrents = (page = 1, size = 5) => {

   return api.get('/torrent', {

     params: { page, size }

@@ -54,6 +102,61 @@
   return api.post(`/torrent/${torrentId}/comments`, commentData);

 };

 

+export const deleteTorrent = (torrentId) => {

+  return api.delete(`/torrent/deleteTorrent/${torrentId}`);

+};

+

+// // 上传种子接口

+// export const uploadTorrent = async (torrentFile, torrentData, onProgress) => {

+//   // 1. 基础验证

+//   if (!torrentFile || !torrentFile.name.endsWith('.torrent')) {

+//     throw new Error('请选择有效的.torrent文件');

+//   }

+

+//   // 2. 构建FormData

+//   const formData = new FormData();

+//   formData.append('file', torrentFile);

+  

+//   // 确保JSON内容使用正确的Content-Type

+//   const metadataBlob = new Blob(

+//     [JSON.stringify(torrentData)], 

+//     { type: 'application/json' }

+//   );

+//   formData.append('body', metadataBlob);

+

+//   // 3. 获取认证token(根据你的实际存储方式调整)

+//   const token = localStorage.getItem('token') || '';

+

+//   try {

+//     const response = await api.post('/api/torrents', formData, {

+//       headers: {

+//         'Authorization': `Bearer ${token}`,

+//       },

+//       onUploadProgress: (progressEvent) => {

+//         if (onProgress && progressEvent.total) {

+//           const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total);

+//           onProgress(percent);

+//         }

+//       }

+//     });

+

+//     return response.data;

+//   } catch (error) {

+//     console.error('上传失败:', error);

+    

+//     // 增强错误处理

+//     let errorMessage = '上传失败';

+//     if (error.response) {

+//       errorMessage = error.response.data?.message || 

+//                      `服务器错误: ${error.response.status}`;

+//     } else if (error.request) {

+//       errorMessage = '网络错误,请检查连接';

+//     }

+

+//     throw new Error(errorMessage);

+//   }

+// };

+

 export const searchTorrents = (keyword, page = 1, size = 5) => {

   return api.get('/torrent/search', {

     params: { keyword, page, size }