|
@@ -1,32 +1,30 @@
|
|
|
// stores/room.ts - 管理房间状态
|
|
|
import { defineStore } from 'pinia'
|
|
|
import { ref, reactive, computed } from 'vue'
|
|
|
-import { roomAPI, type Room, type RoomUser, type RecentRoom } from '@/services/api/room'
|
|
|
+import { roomService } from '@/services/api/room'
|
|
|
+import { type IRoom, type IRoomUser, type IRecentRoom } from '@/types/room'
|
|
|
import { useUserStore } from '@/stores/user'
|
|
|
import Taro from '@tarojs/taro'
|
|
|
|
|
|
-// 重新导出类型以便其他组件使用
|
|
|
-export type { Room, RoomUser, RecentRoom } from '@/services/api/room'
|
|
|
-
|
|
|
export const useRoomStore = defineStore('room', () => {
|
|
|
// 获取userStore
|
|
|
const userStore = useUserStore()
|
|
|
|
|
|
// 当前房间信息
|
|
|
- const currentRoom = ref<Room | null>(null)
|
|
|
+ const currentRoom = ref<IRoom | null>(null)
|
|
|
|
|
|
// 最近加入的房间
|
|
|
- const recentRooms = ref<RecentRoom[]>([])
|
|
|
+ const recentRooms = ref<IRecentRoom[]>([])
|
|
|
|
|
|
// 历史记录状态
|
|
|
const historyState = reactive({
|
|
|
- activeRooms: [] as Room[],
|
|
|
+ activeRooms: [] as IRoom[],
|
|
|
endedGames: [] as any[],
|
|
|
isLoading: false
|
|
|
})
|
|
|
|
|
|
// 设置当前房间
|
|
|
- function setCurrentRoom(room: Room | null) {
|
|
|
+ function setCurrentRoom(room: IRoom | null) {
|
|
|
currentRoom.value = room
|
|
|
}
|
|
|
|
|
@@ -38,7 +36,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 从已加载的活跃房间和历史记录构建最近房间列表
|
|
|
- const recentList: RecentRoom[] = [
|
|
|
+ const recentList: IRecentRoom[] = [
|
|
|
// 活跃房间
|
|
|
...historyState.activeRooms.map(room => ({
|
|
|
id: room.id,
|
|
@@ -69,7 +67,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
// 加载活跃房间 - 统一数据源
|
|
|
async function loadActiveRooms() {
|
|
|
try {
|
|
|
- historyState.activeRooms = await roomAPI.getActiveRooms(userStore.openid)
|
|
|
+ historyState.activeRooms = await roomService.getActiveRooms(userStore.openid)
|
|
|
return historyState.activeRooms
|
|
|
} catch (error) {
|
|
|
console.error('加载活跃房间失败:', error)
|
|
@@ -80,7 +78,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
// 加载已结束游戏 - 统一数据源
|
|
|
async function loadEndedGames() {
|
|
|
try {
|
|
|
- historyState.endedGames = await roomAPI.getEndedGames(userStore.openid)
|
|
|
+ historyState.endedGames = await roomService.getEndedGames(userStore.openid)
|
|
|
return historyState.endedGames
|
|
|
} catch (error) {
|
|
|
console.error('加载已结束游戏失败:', error)
|
|
@@ -100,7 +98,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 创建房间 - 调用API并更新store
|
|
|
- async function createRoom(roomData: Room) {
|
|
|
+ async function createRoom(roomData: IRoom) {
|
|
|
try {
|
|
|
// 检查用户是否已注册
|
|
|
const userCheck = await checkUserRegistered()
|
|
@@ -108,7 +106,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
return { roomId: null, success: false, message: userCheck.message }
|
|
|
}
|
|
|
|
|
|
- const result = await roomAPI.createRoom(roomData, {
|
|
|
+ const result = await roomService.createRoom(roomData, {
|
|
|
openid: userStore.openid,
|
|
|
nickname: userStore.nickname,
|
|
|
avatar: userStore.avatar
|
|
@@ -116,7 +114,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
|
|
|
if (result.success && result.roomId) {
|
|
|
// 获取完整房间信息
|
|
|
- const room = await roomAPI.getRoomInfo(result.roomId)
|
|
|
+ const room = await roomService.getRoomInfo(result.roomId)
|
|
|
if (room) {
|
|
|
setCurrentRoom(room)
|
|
|
|
|
@@ -142,7 +140,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 构建用户数据
|
|
|
- const userData: RoomUser = {
|
|
|
+ const userData: IRoomUser = {
|
|
|
id: userStore.openid,
|
|
|
name: userStore.nickname,
|
|
|
avatar: userStore.avatar,
|
|
@@ -151,7 +149,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 调用API加入房间
|
|
|
- const result = await roomAPI.joinRoom(roomId, userData)
|
|
|
+ const result = await roomService.joinRoom(roomId, userData)
|
|
|
|
|
|
if (result.success && result.roomData) {
|
|
|
// 更新当前房间
|
|
@@ -175,7 +173,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
// 退出房间 - 调用API并更新store
|
|
|
async function leaveRoom(roomId: string) {
|
|
|
try {
|
|
|
- const result = await roomAPI.leaveRoom(roomId, userStore.openid)
|
|
|
+ const result = await roomService.leaveRoom(roomId, userStore.openid)
|
|
|
|
|
|
if (result.success) {
|
|
|
// 清除当前房间信息
|
|
@@ -195,7 +193,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
// 加载房间信息 - 通过API获取
|
|
|
async function loadRoomInfo(roomId: string) {
|
|
|
try {
|
|
|
- const room = await roomAPI.getRoomInfo(roomId)
|
|
|
+ const room = await roomService.getRoomInfo(roomId)
|
|
|
|
|
|
if (room) {
|
|
|
setCurrentRoom(room)
|
|
@@ -210,7 +208,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 添加用户到当前房间
|
|
|
- function addUserToRoom(user: RoomUser) {
|
|
|
+ function addUserToRoom(user: IRoomUser) {
|
|
|
if (currentRoom.value) {
|
|
|
// 检查是否已存在
|
|
|
const existingUserIndex = currentRoom.value.users.findIndex(u => u.id === user.id)
|
|
@@ -235,7 +233,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 更新用户信息
|
|
|
- function updateUserInRoom(userId: string, data: Partial<RoomUser>) {
|
|
|
+ function updateUserInRoom(userId: string, data: Partial<IRoomUser>) {
|
|
|
if (currentRoom.value) {
|
|
|
const userIndex = currentRoom.value.users.findIndex(u => u.id === userId)
|
|
|
if (userIndex !== -1) {
|
|
@@ -296,7 +294,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 更新房间设置
|
|
|
- function updateRoomSettings(settings: Partial<Room>) {
|
|
|
+ function updateRoomSettings(settings: Partial<IRoom>) {
|
|
|
if (currentRoom.value) {
|
|
|
currentRoom.value = {
|
|
|
...currentRoom.value,
|
|
@@ -365,7 +363,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
success: boolean,
|
|
|
message?: string,
|
|
|
needPassword?: boolean,
|
|
|
- roomData?: Room // 添加房间数据
|
|
|
+ roomData?: IRoom // 添加房间数据
|
|
|
}> {
|
|
|
try {
|
|
|
// 1. 检查用户是否注册
|
|
@@ -375,7 +373,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
// 2. 验证房间
|
|
|
- const checkResult = await roomAPI.checkRoomExist(roomId)
|
|
|
+ const checkResult = await roomService.checkRoomExist(roomId)
|
|
|
if (!checkResult || !checkResult.exists) {
|
|
|
return { success: false, message: '房间不存在' }
|
|
|
}
|
|
@@ -386,7 +384,7 @@ export const useRoomStore = defineStore('room', () => {
|
|
|
}
|
|
|
|
|
|
if (checkResult.needPassword && password) {
|
|
|
- const pwdResult = await roomAPI.verifyRoomPassword(roomId, password)
|
|
|
+ const pwdResult = await roomService.verifyRoomPassword(roomId, password)
|
|
|
if (!pwdResult || !pwdResult.valid) {
|
|
|
return { success: false, message: '房间密码错误' }
|
|
|
}
|