Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 1 | // import axios from 'axios'; |
| 2 | |
| 3 | // // 获取所有小组 |
| 4 | // export const fetchGroups = async (params) => { |
| 5 | // const response = await axios.post('/echo/groups/getAllGroups', params); |
| 6 | // return response.data; |
| 7 | // }; |
| 8 | |
| 9 | // // 加入小组 |
| 10 | // export const joinGroup = async (groupId, userId) => { |
| 11 | // const response = await axios.post(`/echo/groups/${groupId}/join`, { user_id: userId }); |
| 12 | // return response.data; |
| 13 | // }; |
| 14 | |
| 15 | // // 创建帖子 |
| 16 | // export const createPost = async (groupId, userId, content, title, images) => { |
| 17 | // const formData = new FormData(); |
| 18 | // formData.append('user_id', userId); |
| 19 | // formData.append('content', content); |
| 20 | // formData.append('title', title); |
| 21 | |
| 22 | // if (images && images.length > 0) { |
| 23 | // for (let i = 0; i < images.length; i++) { |
| 24 | // formData.append('images', images[i]); |
| 25 | // } |
| 26 | // } |
| 27 | |
| 28 | // const response = await axios.post(`/echo/groups/${groupId}/createPost`, formData, { |
| 29 | // headers: { 'Content-Type': 'multipart/form-data' } |
| 30 | // }); |
| 31 | |
| 32 | // return response.data; |
| 33 | // }; |
| 34 | |
| 35 | import axios from 'axios'; |
| 36 | |
| 37 | // 获取所有小组 |
| 38 | export const fetchGroups = async (params) => { |
| 39 | try { |
| 40 | const response = await axios.post('/echo/groups/getAllGroups', params); |
| 41 | return response.data; |
| 42 | } catch (error) { |
| 43 | console.error('获取小组列表失败:', error.response?.data || error.message); |
| 44 | return { status: 'failure', message: '获取小组失败' }; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | // 加入小组 |
| 49 | export const joinGroup = async (groupId, userId) => { |
| 50 | try { |
| 51 | const response = await axios.post( |
| 52 | `/echo/groups/${groupId}/join`, |
| 53 | { user_id: userId }, |
| 54 | { |
| 55 | headers: { |
| 56 | 'Content-Type': 'application/json' // ✅ 明确设置为 JSON |
| 57 | } |
| 58 | } |
| 59 | ); |
| 60 | return response.data; |
| 61 | } catch (error) { |
| 62 | console.error('加入小组失败:', error.response?.data || error.message); |
| 63 | return { status: 'failure', message: '加入小组失败' }; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | // 创建帖子 |
| 69 | export const createPost = async (groupId, userId, content, title, images) => { |
| 70 | try { |
| 71 | const formData = new FormData(); |
| 72 | formData.append('user_id', userId); |
| 73 | formData.append('content', content); |
| 74 | formData.append('title', title); |
| 75 | |
| 76 | if (images && images.length > 0) { |
| 77 | for (let i = 0; i < images.length; i++) { |
| 78 | formData.append('images', images[i]); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const response = await axios.post(`/echo/groups/${groupId}/createPost`, formData, { |
| 83 | headers: { 'Content-Type': 'multipart/form-data' } |
| 84 | }); |
| 85 | |
| 86 | return response.data; |
| 87 | } catch (error) { |
| 88 | console.error('创建帖子失败:', error.response?.data || error.message); |
| 89 | return { status: 'failure', message: '创建帖子失败' }; |
| 90 | } |
| 91 | }; |