diff --git a/frontend/src/api/invitations.ts b/frontend/src/api/invitations.ts index aeb6aad..48429bd 100644 --- a/frontend/src/api/invitations.ts +++ b/frontend/src/api/invitations.ts @@ -100,6 +100,13 @@ export async function respondInvitation( // 更新用户状态为 in_team await pb.collection('users').update(user.id, { status: 'in_team' }) + + // 同步更新本地 userStore + const { useUserStore } = await import('@/stores/user') + const userStore = useUserStore() + if (userStore.user) { + userStore.user.status = 'in_team' + } } // 更新邀请状态 diff --git a/frontend/src/api/sessions.ts b/frontend/src/api/sessions.ts index 0e1c107..9fbb104 100644 --- a/frontend/src/api/sessions.ts +++ b/frontend/src/api/sessions.ts @@ -51,7 +51,9 @@ export async function updateTeamStatus(sessionId: string, status: TeamStatus): P // 结束游戏(解散临时小组 + 重置成员状态) export async function endGame(sessionId: string) { - const session = await pb.collection('team_sessions').getOne(sessionId) as any + const session = await pb.collection('team_sessions').getOne(sessionId, { + $autoCancel: false + }) as any const members: string[] = session.members || [] // 解散临时小组 @@ -60,10 +62,7 @@ export async function endGame(sessionId: string) { // 重置所有成员状态为 idle for (const memberId of members) { try { - const user = await pb.collection('users').getOne(memberId) as any - if (user && user.status === 'in_team') { - await pb.collection('users').update(memberId, { status: 'idle' }) - } + await pb.collection('users').update(memberId, { status: 'idle' }) } catch (_) {} } } diff --git a/frontend/src/stores/team.ts b/frontend/src/stores/team.ts index d35f98c..69d2119 100644 --- a/frontend/src/stores/team.ts +++ b/frontend/src/stores/team.ts @@ -5,6 +5,7 @@ import type { TeamSession, TeamStatus } from '@/types' import { getActiveTeamSession, createTeamSession, updateTeamStatus, endGame } from '@/api/sessions' import { pb } from '@/api/pocketbase' import { getPendingInvitations, sendBulkInvitations } from '@/api/invitations' +import { useUserStore } from '@/stores/user' export const useTeamStore = defineStore('team', () => { // 状态 @@ -74,6 +75,15 @@ export const useTeamStore = defineStore('team', () => { try { const updated = await updateTeamStatus(currentSession.value.id, status) currentSession.value = updated + + // 同步更新创建者状态 + const userStore = useUserStore() + if (status === 'playing') { + await userStore.setStatus('in_team') + } else if (status === 'dissolved' || status === 'finished') { + await userStore.setStatus('idle') + } + return updated } catch (error: any) { console.error('更新小组状态失败:', error) @@ -89,6 +99,10 @@ export const useTeamStore = defineStore('team', () => { loading.value = true await endGame(currentSession.value.id) currentSession.value = null + + // 重置本地用户状态 + const userStore = useUserStore() + await userStore.setStatus('idle') } catch (error: any) { console.error('结束游戏失败:', error) throw error