BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 1 | import { request } from '@umijs/max'; |
| 2 | import type { |
| 3 | SysUserMessage, |
| 4 | ChatContact, |
| 5 | ChatContactListParams, |
| 6 | ChatHistoryParams, |
| 7 | SendMessageParams, |
| 8 | } from './data.d'; |
| 9 | |
| 10 | // API 路径配置 - 可根据实际后端接口调整 |
| 11 | const API_CONFIG = { |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 12 | CHAT_CONTACTS: '/api/system/user/message/chat/users', |
| 13 | CHAT_HISTORY: '/api/system/user/message/list', |
| 14 | SEND_MESSAGE: '/api/system/user/message', |
| 15 | USER_INFO: '/api/system/user' |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 16 | }; |
| 17 | |
| 18 | /** 获取聊天对象列表 */ |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 19 | export async function getChatContactList() { |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 20 | // 默认数据 |
| 21 | const defaultData = [ |
| 22 | { |
| 23 | userId: 2, |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 24 | nickName: '张三' |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 25 | }, |
| 26 | { |
| 27 | userId: 3, |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 28 | nickName: '李四' |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 29 | }, |
| 30 | { |
| 31 | userId: 4, |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 32 | nickName: '王五' |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 33 | } |
| 34 | ]; |
| 35 | |
| 36 | try { |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 37 | const response = await request('/api/system/user/message/chat/users', { |
| 38 | method: 'get' |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 39 | }); |
| 40 | |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 41 | if (!response) { |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 42 | return defaultData; |
| 43 | } |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 44 | return response.data; |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 45 | } catch (error) { |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 46 | console.warn('获取聊天对象列表接口异常,使用默认数据:', error); |
| 47 | return defaultData; |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |
| 52 | /** 获取与指定用户的聊天记录 */ |
| 53 | export async function getChatHistory(params: ChatHistoryParams) { |
| 54 | // 默认数据 |
| 55 | const defaultData = [ |
| 56 | { |
| 57 | messageId: 1, |
| 58 | senderId: params.userId, |
| 59 | receiverId: params.currentUserId || 1, |
| 60 | content: `这是来自用户${params.userId}的第一条消息`, |
| 61 | createTime: new Date(Date.now() - 1000 * 60 * 60 * 2), |
| 62 | delFlag: '0' |
| 63 | }, |
| 64 | { |
| 65 | messageId: 2, |
| 66 | senderId: params.currentUserId || 1, |
| 67 | receiverId: params.userId, |
| 68 | content: '收到,感谢你的消息', |
| 69 | createTime: new Date(Date.now() - 1000 * 60 * 60), |
| 70 | delFlag: '0' |
| 71 | }, |
| 72 | { |
| 73 | messageId: 3, |
| 74 | senderId: params.userId, |
| 75 | receiverId: params.currentUserId || 1, |
| 76 | content: `这是来自用户${params.userId}的最新消息,包含更多详细信息`, |
| 77 | createTime: new Date(Date.now() - 1000 * 60 * 30), |
| 78 | delFlag: '0' |
| 79 | } |
| 80 | ]; |
| 81 | |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 82 | try { |
| 83 | const response = await request(`${API_CONFIG.CHAT_HISTORY}`, { |
| 84 | method: 'get', |
| 85 | params: { |
| 86 | userId1: params.currentUserId, |
| 87 | userId2: params.userId |
| 88 | } |
| 89 | }); |
| 90 | console.log(response); |
| 91 | if (!response || !response.data) { |
| 92 | return defaultData; |
| 93 | } |
| 94 | return response.data; |
| 95 | } catch (error) { |
| 96 | console.warn('获取聊天记录接口异常,使用默认数据:', error); |
| 97 | return defaultData; |
| 98 | } |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** 发送消息 */ |
| 102 | export async function sendMessage(params: SendMessageParams) { |
| 103 | try { |
| 104 | return await request(API_CONFIG.SEND_MESSAGE, { |
| 105 | method: 'post', |
| 106 | data: params, |
| 107 | }); |
| 108 | } catch (error) { |
| 109 | // 发送消息失败时记录错误但不返回默认数据,让组件处理错误 |
| 110 | console.error('发送消息接口异常:', error); |
| 111 | throw error; // 重新抛出错误,让调用方处理 |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** 获取用户信息(用于聊天对象显示) */ |
| 116 | export async function getUserInfo(userId: number) { |
| 117 | // 默认用户信息 |
| 118 | const defaultUserInfo = { |
| 119 | userId, |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 120 | nickName: `用户${userId}`, |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 121 | avatar: '' |
| 122 | }; |
| 123 | |
| 124 | try { |
| 125 | const response = await request(`${API_CONFIG.USER_INFO}/${userId}`, { |
| 126 | method: 'get', |
| 127 | }); |
| 128 | |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 129 | if (!response || !response.data) { |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 130 | return defaultUserInfo; |
| 131 | } |
BirdNETM | 2b78925 | 2025-06-03 18:08:04 +0800 | [diff] [blame^] | 132 | return response.data; |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame] | 133 | } catch (error) { |
| 134 | // 接口报错时返回默认用户信息 |
| 135 | console.warn(`获取用户${userId}信息接口异常,使用默认数据:`, error); |
| 136 | return defaultUserInfo; |
| 137 | } |
| 138 | } |