// api/roomUser.js - 房间用户相关API import { GET, POST, PUT, DELETE } from '@/utils/request' /** * 房间用户相关API */ export default { /** * 获取房间内所有用户及其详细对局记录(已修复) * @param {number} roomId 房间ID */ async getUserScoresWithDetails(roomId) { try { console.log('获取房间用户详情,房间ID:', roomId); // 使用正确的API路径 const response = await GET(`/system/score/room/user/user-details/${roomId}`); console.log('房间用户详情API响应:', response); // 处理不同的响应格式 if (response.code === 200 || response.code === 0) { return response.data; } else if (response.success) { return response.data; } else { // 如果接口返回错误,尝试使用列表接口 console.warn('主接口失败,尝试使用列表接口'); return await this.getRoomUserList({ roomId }); } } catch (error) { console.error('获取房间用户详情失败:', error); // 降级方案1:使用列表接口 try { console.log('尝试使用列表接口作为降级方案'); const listResult = await this.getRoomUserList({ roomId }); if (listResult) { return listResult; } } catch (listError) { console.error('列表接口也失败:', listError); } // 降级方案2:返回空数据并提示 throw error; } }, /** * 根据RoomUserId查询 * @param {number} roomUserId 房间用户ID */ async getRoomUserById(roomUserId) { try { const response = await GET(`/system/score/room/user/RoomUserId/${roomUserId}`); return response.data || response; } catch (error) { console.error('获取房间用户信息失败:', error); throw error; } }, /** * 根据UserId查询 * @param {number} userId 用户ID */ async getRoomUserByUserId(userId) { try { const response = await GET(`/system/score/room/user/userid/${userId}`); return response.data || response; } catch (error) { console.error('根据用户ID获取房间用户失败:', error); throw error; } }, /** * 查询房间用户列表 * @param {Object} params 查询参数 */ async getRoomUserList(params) { try { console.log('获取房间用户列表,参数:', params); const response = await GET('/system/score/room/user/list', params); console.log('房间用户列表响应:', response); return response.data || response; } catch (error) { console.error('获取房间用户列表失败:', error); throw error; } }, /** * 新增房间用户 * @param {Object} roomUserData 房间用户数据 */ async addRoomUser(roomUserData) { try { console.log('新增房间用户:', roomUserData); const response = await POST('/system/score/room/user', roomUserData); console.log('新增房间用户响应:', response); return response.data || response; } catch (error) { console.error('新增房间用户失败:', error); throw error; } }, async updateRoomUsersSafe(roomUsers) { try { console.log('安全更新房间用户得分:', roomUsers); // 过滤和转换数据 const safeUsers = roomUsers.map(user => { let userId = user.userId; // 确保userId在int范围内 if (userId > 2147483647 || userId < -2147483648) { console.warn('用户ID超出范围,进行转换:', userId); // 使用哈希值 const strId = userId.toString(); let hash = 0; for (let i = 0; i < strId.length; i++) { hash = ((hash << 5) - hash) + strId.charCodeAt(i); hash = hash & hash; } userId = Math.abs(hash) % 1000000; } return { ...user, userId: userId }; }); const response = await PUT('/system/score/room/user', safeUsers); console.log('安全更新响应:', response); return response.data || response; } catch (error) { console.error('安全更新房间用户得分:', error); throw error; } }, /** * 批量更新用户得分 * @param {Array} roomUsers 房间用户数据数组 */ async updateRoomUsers(roomUsers) { try { console.log('更新房间用户得分:', roomUsers); const response = await PUT('/system/score/room/user', roomUsers); console.log('更新房间用户得分响应:', response); return response.data || response; } catch (error) { console.error('更新房间用户得分:', error); throw error; } }, /** * 批量删除房间用户 * @param {Array} roomUserIds 房间用户ID数组 */ async deleteRoomUsers(roomUserIds) { try { const response = await DELETE(`/system/score/room/user/${roomUserIds}`); return response.data || response; } catch (error) { console.error('删除房间用户失败:', error); throw error; } } }