feat(DailyTasks): 进入日常页面时自动加载阵容数据- 新增 loadTeamDataWithConnection 函数,用于检查 WebSocket 连接状态并加载阵容数据

- 在页面进入时调用该函数,确保 WebSocket 连接成功后再加载阵容数据
- 增加重试机制,提高加载成功率
- 优化错误处理,提升用户体验
This commit is contained in:
steve
2025-09-03 19:04:41 +08:00
parent b5e7615564
commit 29ce1d4683
6 changed files with 1029 additions and 748 deletions

View File

@@ -267,6 +267,77 @@ const bulkActionOptions = [
}
]
// 等待WebSocket连接并加载阵容数据
const loadTeamDataWithConnection = async (tokenId, maxRetries = 3, retryDelay = 2000) => {
console.log('每日页面进入开始检查WebSocket连接状态...')
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
// 检查WebSocket连接状态
const wsStatus = tokenStore.getWebSocketStatus(tokenId)
console.log(`${attempt}次检查WebSocket状态:`, wsStatus)
if (wsStatus !== 'connected') {
console.log('WebSocket未连接尝试建立连接...')
// 尝试建立WebSocket连接
const tokenData = tokenStore.gameTokens.find(t => t.id === tokenId)
if (tokenData && tokenData.token) {
// 触发WebSocket连接
tokenStore.createWebSocketConnection(tokenId, tokenData.token, tokenData.wsUrl)
// 等待连接建立
await new Promise(resolve => setTimeout(resolve, retryDelay))
// 再次检查连接状态
const newStatus = tokenStore.getWebSocketStatus(tokenId)
if (newStatus !== 'connected') {
if (attempt < maxRetries) {
console.log(`连接未建立,${retryDelay / 1000}秒后重试...`)
continue
} else {
throw new Error('WebSocket连接超时')
}
}
} else {
throw new Error('未找到有效的Token数据或WebSocket URL')
}
}
// WebSocket已连接开始加载阵容数据
console.log('WebSocket已连接开始加载阵容数据...')
const result = await tokenStore.sendMessageWithPromise(
tokenId,
'presetteam_getinfo',
{},
8000
)
if (result) {
// 更新到游戏数据缓存中
tokenStore.$patch((state) => {
state.gameData = { ...(state.gameData ?? {}), presetTeam: result }
})
console.log('阵容数据加载成功:', result)
message.success('阵容数据已更新')
return result
}
} catch (error) {
console.error(`${attempt}次尝试失败:`, error)
if (attempt < maxRetries) {
console.log(`${retryDelay / 1000}秒后进行第${attempt + 1}次重试...`)
await new Promise(resolve => setTimeout(resolve, retryDelay))
} else {
console.error('所有重试均失败,阵容数据加载失败')
message.warning(`阵容数据加载失败: ${error.message || '未知错误'}`)
return null
}
}
}
}
// 方法
const refreshTasks = async () => {
if (!selectedRoleId.value) {
@@ -567,6 +638,11 @@ onMounted(async () => {
await gameRolesStore.fetchGameRoles()
}
// 页面进入时手动调用阵容加载接口确保WebSocket连接后再调用
if (tokenStore.selectedToken) {
await loadTeamDataWithConnection(tokenStore.selectedToken.id)
}
// 设置默认选中的角色
if (gameRolesStore.selectedRole) {
selectedRoleId.value = gameRolesStore.selectedRole.id