fix: sync user status changes across team lifecycle

- Accept invitation: update local userStore status to in_team
- Start game: update userStore status to in_team
- End game: update userStore status to idle, simplify endGame logic
- Add $autoCancel:false to endGame session fetch

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
congsh
2026-04-18 10:59:10 +08:00
parent 8d3cce814a
commit 12b2cdbc02
3 changed files with 25 additions and 5 deletions
+7
View File
@@ -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'
}
}
// 更新邀请状态
+4 -5
View File
@@ -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 (_) {}
}
}
+14
View File
@@ -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