This commit is contained in:
Baobhan Sith
2025-04-28 20:36:03 +08:00
parent c43b2c63f1
commit c6f1c63270
2 changed files with 17 additions and 5 deletions
@@ -2,6 +2,7 @@ import { Request, Response } from 'express';
import * as ConnectionService from '../services/connection.service'; import * as ConnectionService from '../services/connection.service';
import * as SshService from '../services/ssh.service'; import * as SshService from '../services/ssh.service';
import * as ImportExportService from '../services/import-export.service'; import * as ImportExportService from '../services/import-export.service';
import * as ConnectionRepository from '../repositories/connection.repository'; // +++ 导入 ConnectionRepository +++
@@ -290,6 +291,17 @@ export const getRdpSessionToken = async (req: Request, res: Response): Promise<v
return; return;
} }
// +++ 在确认是 RDP 连接后,立即更新 last_connected_at +++
try {
const currentTimeSeconds = Math.floor(Date.now() / 1000);
await ConnectionRepository.updateLastConnected(connectionId, currentTimeSeconds);
console.log(`[Controller:getRdpSessionToken] 已更新 RDP 连接 ${connectionId} 的 last_connected_at 为 ${currentTimeSeconds}`);
} catch (updateError) {
// 记录更新时间戳的错误,但不阻止获取令牌的流程
console.error(`[Controller:getRdpSessionToken] 更新 RDP 连接 ${connectionId} 的 last_connected_at 时出错:`, updateError);
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 3. 验证 RDP 连接是否使用密码认证 // 3. 验证 RDP 连接是否使用密码认证
if (connection.auth_method !== 'password' || !decryptedPassword) { if (connection.auth_method !== 'password' || !decryptedPassword) {
console.warn(`[Controller:getRdpSessionToken] RDP connection ${connectionId} does not use password auth or password decryption failed.`); console.warn(`[Controller:getRdpSessionToken] RDP connection ${connectionId} does not use password auth or password decryption failed.`);
@@ -26,23 +26,23 @@ const maxRecentLogs = 5;
// --- 最近连接 --- // --- 最近连接 ---
const recentConnections = computed(() => { const recentConnections = computed(() => {
console.log('[Dashboard] Raw connections from store:', JSON.parse(JSON.stringify(connections.value))); console.log('[仪表盘] 从 Store 获取的原始连接列表:', JSON.parse(JSON.stringify(connections.value)));
// 优先尝试按 last_connected_at 过滤和排序 // 优先尝试按 last_connected_at 过滤和排序
const connected = connections.value.filter(c => c.last_connected_at); const connected = connections.value.filter(c => c.last_connected_at);
console.log('[Dashboard] Filtered connections (with last_connected_at):', JSON.parse(JSON.stringify(connected))); console.log('[仪表盘] 过滤后的连接 (包含 last_connected_at):', JSON.parse(JSON.stringify(connected)));
if (connected.length > 0) { if (connected.length > 0) {
connected.sort((a, b) => (b.last_connected_at ?? 0) - (a.last_connected_at ?? 0)); connected.sort((a, b) => (b.last_connected_at ?? 0) - (a.last_connected_at ?? 0));
const result = connected.slice(0, maxRecentConnections); const result = connected.slice(0, maxRecentConnections);
console.log('[Dashboard] Final recent connections (using last_connected_at):', JSON.parse(JSON.stringify(result))); console.log('[仪表盘] 最终最近连接 (使用 last_connected_at):', JSON.parse(JSON.stringify(result)));
return result; return result;
} else { } else {
// 如果没有带 last_connected_at 的连接,则按 updated_at 排序显示最近更新的 // 如果没有带 last_connected_at 的连接,则按 updated_at 排序显示最近更新的
console.log('[Dashboard] No connections with last_connected_at found. Falling back to sorting by updated_at.'); console.log('[仪表盘] 未找到包含 last_connected_at 的连接,回退到按 updated_at 排序。');
const sortedByUpdate = [...connections.value].sort((a, b) => (b.updated_at ?? 0) - (a.updated_at ?? 0)); const sortedByUpdate = [...connections.value].sort((a, b) => (b.updated_at ?? 0) - (a.updated_at ?? 0));
const result = sortedByUpdate.slice(0, maxRecentConnections); const result = sortedByUpdate.slice(0, maxRecentConnections);
console.log('[Dashboard] Final recent connections (fallback using updated_at):', JSON.parse(JSON.stringify(result))); console.log('[仪表盘] 最终最近连接 (回退使用 updated_at):', JSON.parse(JSON.stringify(result)));
return result; return result;
} }
}); });