blob: 949afb43d4a48927b5c08d96253524ddb387454a [file] [log] [blame]
Akane12173a7bb972025-06-01 01:05:27 +08001// src/api/personal.js
2import { api } from './auth';
3
4/**
5 * 获取用户信息
6 * @returns {Promise<Object>} 用户信息对象
7 */
8export const getUserInfo = async () => {
9 try {
10 const response = await api.get('/user/userInfo');
11 if (response.data.code === 200) {
12 const userData = response.data.data;
13 return {
14 username: userData.username,
15 level: userData.level,
16 registTime: formatDate(userData.registTime),
17 magicPoints: userData.magicPoints,
18 upload: userData.upload,
19 download: userData.download,
20 shareRate: userData.shareRate.toFixed(2)
21 };
22 }
23 throw new Error(response.data.message || '获取用户信息失败');
24 } catch (error) {
25 console.error('获取用户信息失败:', error);
26 throw error;
27 }
28};
29
30export const formatFileSize = (bytes) => {
31 if (bytes < 1024) {
32 return bytes + ' B';
33 }
34 const kb = bytes / 1024;
35 if (kb < 1024) {
36 return kb.toFixed(2) + ' KB';
37 }
38 const mb = kb / 1024;
39 if (mb < 1024) {
40 return mb.toFixed(2) + ' MB';
41 }
42 const gb = mb / 1024;
43 return gb.toFixed(2) + ' GB';
44 };
45
46
47 export const getDownloadQuota = async () => {
48 try {
49 const response = await api.get('/user/allowDownload');
50 if (response.data.code === 200) {
51 const data = response.data.data;
52 return {
53 total: data.total, // 已经是字节
54 used: data.used,
55 remaining: data.remaining
56 };
57 }
58 throw new Error(response.data.message || '获取下载额度失败');
59 } catch (error) {
60 console.error('获取下载额度失败:', error);
61 throw error;
62 }
63 };
64
65// 修正后的时间格式化(正确处理时区)
66const formatDate = (dateString) => {
67 const date = new Date(dateString);
68 const year = date.getFullYear();
69 const month = String(date.getMonth() + 1).padStart(2, '0');
70 const day = String(date.getDate()).padStart(2, '0');
71 return `${year}-${month}-${day}`;
72 };
73
74
75 export const getDownloadProgress = async () => {
76 try {
77 const response = await api.get('/torrent/getProgress');
78 if (response.data.code === 200) {
79 return response.data.data.progresses;
80 }
81 throw new Error(response.data.message || '获取下载进度失败');
82 } catch (error) {
83 console.error('获取下载进度失败:', error);
84 throw error;
85 }
86 };
87
88 export const getUserTorrents = async (page = 1, size = 5) => {
89 try {
90 const response = await api.get('/torrent/get/torrentMyself', {
91 params: { page, size }
92 });
93 if (response.data.code === 200) {
94 const records = response.data.data.records.map(item => ({
95 ...item.torrent,
96 downloadCount: item.downloadCount,
97 formattedSize: item.formattedSize
98 }));
99 return {
100 records: records,
101 total: response.data.data.total
102 };
103 }
104 throw new Error(response.data.message || '获取上传记录失败');
105 } catch (error) {
106 console.error('获取上传记录失败:', error);
107 throw error;
108 }
109 };
110
111
112 export const deleteTorrent = async (id) => {
113 try {
114 const response = await api.delete(`/torrent/deleteTorrent/${id}`);
115 if (response.data.code === 200) {
116 return response.data;
117 }
118 throw new Error(response.data.message || '删除种子失败');
119 } catch (error) {
120 console.error('删除种子失败:', error);
121 throw error;
122 }
123 };
124
125
126 export const generateInviteCode = async () => {
127 try {
128 const response = await api.post('/invitecode/generate');
129 if (response.data.code === 200) {
130 return response.data.data.inviteCode;
131 }
132 throw new Error(response.data.message || '生成邀请码失败');
133 } catch (error) {
134 console.error('生成邀请码失败:', error);
135 throw error;
136 }
137 };
138
139 export const getUserInviteCodes = async () => {
140 try {
141 const response = await api.get('/invitecode/userInviteCode');
142 if (response.data.code === 200) {
143 return response.data.data.inviteCode;
144 }
145 throw new Error(response.data.message || '获取邀请码列表失败');
146 } catch (error) {
147 console.error('获取邀请码列表失败:', error);
148 throw error;
149 }
150 };
151
152 export const exchangeUpload = async (magicPoints) => {
153 try {
154 const response = await api.post('/user/exchangeUpload', {
155 magicPoint: magicPoints
156 });
157 if (response.data.code === 200) {
158 return response.data;
159 }
160 throw new Error(response.data.message || '兑换上传量失败');
161 } catch (error) {
162 console.error('兑换上传量失败:', error);
163 throw error;
164 }
165 };
166
167 export const updatePassword = async (oldPassword, newPassword) => {
168 try {
169 const response = await api.put('/user/password', {
170 oldPassword,
171 newPassword
172 });
173 if (response.data.code === 200) {
174 return response.data;
175 }
176 throw new Error(response.data.message || '修改密码失败');
177 } catch (error) {
178 console.error('修改密码失败:', error);
179 throw error;
180 }
181 };