ym923 | 3bf06b1 | 2025-06-09 18:18:51 +0800 | [diff] [blame^] | 1 | import axios from 'axios'; |
| 2 | |
| 3 | const BASE_URL = 'http://localhost:8080/chat'; |
| 4 | |
| 5 | // 创建聊天记录 |
| 6 | export const createChat = (chatData) => { |
| 7 | return axios.post(`${BASE_URL}/create`, chatData); |
| 8 | }; |
| 9 | |
| 10 | // 删除聊天记录 |
| 11 | export const deleteChat = (chatId) => { |
| 12 | return axios.delete(`${BASE_URL}/delete/${chatId}`); |
| 13 | }; |
| 14 | |
| 15 | // 获取某个用户的所有聊天记录(与所有人的) |
| 16 | export async function getChatsByUser(userId) { |
| 17 | try { |
| 18 | const response = await axios.get(`${BASE_URL}/user/${userId}`); |
| 19 | return Array.isArray(response.data) ? response.data : []; |
| 20 | } catch (error) { |
| 21 | console.error('获取用户聊天记录失败', error); |
| 22 | return []; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // 获取两个用户之间的聊天记录(请求参数形式) |
| 27 | export async function getChatsBetweenUsers(user1Id, user2Id) { |
| 28 | try { |
| 29 | const response = await axios.get(`${BASE_URL}/between`, { |
| 30 | params: { |
| 31 | user1: user1Id, |
| 32 | user2: user2Id |
| 33 | } |
| 34 | }); |
| 35 | return Array.isArray(response.data) ? response.data : []; |
| 36 | } catch (error) { |
| 37 | console.error('获取两人聊天记录失败', error); |
| 38 | return []; |
| 39 | } |
| 40 | } |