ym923 | f0485a5 | 2025-06-09 20:18:10 +0800 | [diff] [blame^] | 1 | import axios from 'axios'; |
| 2 | |
| 3 | const BASE_URL = 'http://localhost:8080/request'; |
| 4 | |
| 5 | // 创建求助帖(支持上传图片) |
| 6 | export const createRequest = (formData) => { |
| 7 | return axios.post(`${BASE_URL}/create`, formData, { |
| 8 | headers: { |
| 9 | 'Content-Type': 'multipart/form-data', |
| 10 | }, |
| 11 | }); |
| 12 | }; |
| 13 | |
| 14 | // 修改求助帖金额 |
| 15 | export const updateMoney = (requestid, money) => { |
| 16 | return axios.put(`${BASE_URL}/updateMoney/${requestid}`, null, { |
| 17 | params: { money }, |
| 18 | }); |
| 19 | }; |
| 20 | |
| 21 | // 根据名称批量更新被协助用户 ID |
| 22 | export const updateLoaduserByName = (name, loaduser) => { |
| 23 | return axios.post(`${BASE_URL}/updateLoaduserByName`, null, { |
| 24 | params: { name, loaduser }, |
| 25 | }); |
| 26 | }; |
| 27 | |
| 28 | // ✅ 新增:根据 requestid 更新 torrentid |
| 29 | export const updateTorrentid = (requestid, torrentid) => { |
| 30 | return axios.put(`${BASE_URL}/updateTorrentid/${requestid}`, null, { |
| 31 | params: { torrentid }, |
| 32 | }); |
| 33 | }; |
| 34 | |
| 35 | // ✅ 新增:根据 requestid 获取 torrentid |
| 36 | export const getTorrentid = async (requestid) => { |
| 37 | try { |
| 38 | const response = await axios.get(`${BASE_URL}/getTorrentid/${requestid}`); |
| 39 | return response.data; |
| 40 | } catch (error) { |
| 41 | console.error('获取 torrentid 失败', error); |
| 42 | return null; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | // 删除求助帖 |
| 47 | export const deleteRequest = (requestid) => { |
| 48 | return axios.delete(`${BASE_URL}/delete/${requestid}`); |
| 49 | }; |
| 50 | |
| 51 | // 根据名称查找求助帖 |
| 52 | export const findByName = async (name) => { |
| 53 | try { |
| 54 | const response = await axios.get(`${BASE_URL}/findByName`, { |
| 55 | params: { name }, |
| 56 | }); |
| 57 | return Array.isArray(response.data) ? response.data : []; |
| 58 | } catch (error) { |
| 59 | console.error('按名称查找求助帖失败', error); |
| 60 | return []; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | // 根据发帖用户 ID 查找求助帖 |
| 65 | export const findByUserid = async (userid) => { |
| 66 | try { |
| 67 | const response = await axios.get(`${BASE_URL}/findByUserid`, { |
| 68 | params: { userid }, |
| 69 | }); |
| 70 | return Array.isArray(response.data) ? response.data : []; |
| 71 | } catch (error) { |
| 72 | console.error('按用户ID查找求助帖失败', error); |
| 73 | return []; |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | // 根据被协助用户 ID 查找求助帖 |
| 78 | export const findByLoaduser = async (loaduser) => { |
| 79 | try { |
| 80 | const response = await axios.get(`${BASE_URL}/findByLoaduser`, { |
| 81 | params: { loaduser }, |
| 82 | }); |
| 83 | return Array.isArray(response.data) ? response.data : []; |
| 84 | } catch (error) { |
| 85 | console.error('按被协助用户ID查找求助帖失败', error); |
| 86 | return []; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | // 获取某名称的总金额 |
| 91 | export const getTotalMoneyByName = async (name) => { |
| 92 | try { |
| 93 | const response = await axios.get(`${BASE_URL}/totalMoneyByName`, { |
| 94 | params: { name }, |
| 95 | }); |
| 96 | return typeof response.data === 'number' ? response.data : 0; |
| 97 | } catch (error) { |
| 98 | console.error('获取总金额失败', error); |
| 99 | return 0; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | // 获取所有求助帖 |
| 104 | export const getAllRequests = async () => { |
| 105 | try { |
| 106 | const response = await axios.get(`${BASE_URL}/all`); |
| 107 | return Array.isArray(response.data) ? response.data : []; |
| 108 | } catch (error) { |
| 109 | console.error('获取全部求助帖失败', error); |
| 110 | return []; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | // ✅ 新增:根据 requestid 获取求助帖的部分信息(torrentid、money、loaduser) |
| 115 | export const getInfoByRequestId = async (requestid) => { |
| 116 | try { |
| 117 | const response = await axios.get(`${BASE_URL}/info/${requestid}`); |
| 118 | return response.data || {}; |
| 119 | } catch (error) { |
| 120 | console.error('获取求助帖信息失败', error); |
| 121 | return {}; |
| 122 | } |
| 123 | }; |