fix: 修复移动端恢复不了挂起会话的问题

This commit is contained in:
Baobhan Sith
2025-05-10 13:43:40 +08:00
parent 3980145188
commit 0031c2f717
3 changed files with 39 additions and 10 deletions
@@ -23,7 +23,7 @@ const findConnectionInfo = (connectionId: number | string, connectionsStore: Ret
// --- Actions ---
export const openNewSession = (
connectionId: number | string,
connectionOrId: ConnectionInfo | number | string,
dependencies: {
connectionsStore: ReturnType<typeof useConnectionsStore>;
t: ReturnType<typeof useI18n>['t'];
@@ -31,16 +31,26 @@ export const openNewSession = (
existingSessionId?: string // 新增:可选的预定义会话 ID
) => {
const { connectionsStore, t } = dependencies;
console.log(`[SessionActions] 请求打开新会话: ${connectionId}${existingSessionId ? `, 使用预定义 ID: ${existingSessionId}` : ''}`);
const connInfo = findConnectionInfo(connectionId, connectionsStore);
let connInfo: ConnectionInfo | undefined;
let connIdForLog: string | number;
if (typeof connectionOrId === 'object' && connectionOrId !== null && 'id' in connectionOrId) {
connInfo = connectionOrId as ConnectionInfo;
connIdForLog = connInfo.id;
} else {
connIdForLog = connectionOrId as number | string;
connInfo = findConnectionInfo(connIdForLog, connectionsStore);
}
console.log(`[SessionActions] 请求打开新会话: ${connIdForLog}${existingSessionId ? `, 使用预定义 ID: ${existingSessionId}` : ''}`);
if (!connInfo) {
console.error(`[SessionActions] 无法打开新会话:找不到 ID 为 ${connectionId} 的连接信息。`);
console.error(`[SessionActions] 无法打开新会话:找不到 ID 为 ${connIdForLog} 的连接信息。`);
// TODO: 向用户显示错误
return;
}
const newSessionId = existingSessionId || generateSessionId();
const dbConnId = String(connInfo.id);
const dbConnId = String(connInfo.id); // connInfo is now guaranteed to be defined here
// 1. 创建管理器实例
const isResume = !!existingSessionId; // 如果提供了 existingSessionId,则为恢复流程
@@ -225,11 +225,18 @@ export const resumeSshSession = async (suspendSessionId: string): Promise<void>
const newFrontendSessionId = uuidv4(); // 为恢复的会话生成新的前端 ID
try {
console.log(`[${t('term.sshSuspend')}] 准备恢复会话 ${suspendSessionId}。将创建新前端会话 ${newFrontendSessionId} 并连接 WebSocket。`);
// +++ 先从 connectionsStore 获取完整的 ConnectionInfo +++
const connectionInfo = connectionsStore.connections.find(c => c.id === originalConnectionId);
if (!connectionInfo) {
console.error(`[${t('term.sshSuspend')}] 恢复操作失败:在 Connection Store 中未找到原始连接配置 (ID: ${originalConnectionId})。`);
uiNotificationsStore.addNotification({ type: 'error', message: t('sshSuspend.notifications.resumeErrorConnectionConfigNotFound', { id: String(originalConnectionId) }) });
return;
}
console.log(`[${t('term.sshSuspend')}] 已找到原始连接配置 (ID: ${originalConnectionId}),准备使用它恢复会话 ${suspendSessionId}。将创建新前端会话 ${newFrontendSessionId} 并连接 WebSocket。`);
// 1. 调用 openNewSession 创建前端会话状态、WebSocket 连接等
// 1. 调用 openNewSession 创建前端会话状态、WebSocket 连接等,传入完整的 connectionInfo
openNewSession(
originalConnectionId,
connectionInfo, // +++ 传入完整的 ConnectionInfo 对象 +++
{ connectionsStore, t }, // 传递依赖
newFrontendSessionId // 将 newFrontendSessionId 作为 existingSessionId 传递
);
@@ -111,6 +111,7 @@ import { ref, onMounted, onUnmounted, computed, nextTick, watch } from 'vue'; //
import { useI18n } from 'vue-i18n';
import { storeToRefs } from 'pinia';
import { useSessionStore } from '../stores/session.store';
import { useConnectionsStore } from '../stores/connections.store'; // +++ 导入 Connections Store +++
import type { SuspendedSshSession } from '../types/ssh-suspend.types';
import { useWorkspaceEventEmitter } from '../composables/workspaceEvents'; // +++ 导入事件发射器 +++
@@ -252,10 +253,21 @@ const removeSession = (session: SuspendedSshSession) => { // 参数类型改为
let fetchIntervalId: number | undefined;
onMounted(async () => {
// 立即获取一次数据 (显示加载指示器)
const connectionsStore = useConnectionsStore(); // +++ 获取 Connections Store 实例 +++
// 确保连接列表已加载或正在加载
// 通常 store 的 fetch 方法会处理重复调用或自行管理加载状态
try {
console.log('[SuspendedSshSessionsView] Ensuring connections are fetched.');
await connectionsStore.fetchConnections(); // +++ 获取连接列表 +++
} catch (error) {
console.error('[SuspendedSshSessionsView] Error fetching connections:', error);
// 根据需要处理错误,例如显示通知
}
// 立即获取一次挂起会话数据 (显示加载指示器)
await sessionStore.fetchSuspendedSshSessions();
// 设置定时器,每3秒获取一次数据 (不显示加载指示器)
// 设置定时器,每3秒获取一次挂起会话数据 (不显示加载指示器)
fetchIntervalId = window.setInterval(async () => {
await sessionStore.fetchSuspendedSshSessions({ showLoadingIndicator: false });
}, 3000);