diff --git a/src/components/ServerCard.tsx b/src/components/ServerCard.tsx index 68069ff..101fef3 100644 --- a/src/components/ServerCard.tsx +++ b/src/components/ServerCard.tsx @@ -66,9 +66,18 @@ export default function ServerCard({ now, serverInfo, cycleStats }: ServerCardPr // 获取匹配当前服务器的流量计费周期 const getServerCycleData = () => { - if (!cycleStats) return null + if (!cycleStats) { + console.log('cycleStats is null or undefined'); + return null; + } + + // 确保服务器ID的所有可能形式 + const serverId = String(serverInfo.id); + const serverIdNum = Number(serverInfo.id); + + console.log(`ServerCard: Looking for server "${serverInfo.name}" with ID:`, serverId, 'type:', typeof serverInfo.id); + console.log('All cycleStats:', cycleStats); - const serverId = serverInfo.id.toString() const matchedCycles: Array<{ name: string; from: string; @@ -80,15 +89,60 @@ export default function ServerCard({ now, serverInfo, cycleStats }: ServerCardPr }> = [] // 遍历所有流量周期,查找匹配当前服务器ID的数据 - Object.values(cycleStats).forEach(cycleData => { - if ( - cycleData.server_name && - cycleData.server_name[serverId] && - cycleData.transfer && - cycleData.transfer[serverId] !== undefined - ) { - const transfer = cycleData.transfer[serverId] - const progress = (transfer / cycleData.max) * 100 + Object.entries(cycleStats).forEach(([cycleId, cycleData]) => { + console.log(`\nChecking cycle ${cycleId}:`, cycleData.name); + + if (!cycleData.server_name) { + console.log(` No server_name in this cycle`); + return; + } + + const serverIdsInCycle = Object.keys(cycleData.server_name); + console.log(` Server IDs in this cycle:`, serverIdsInCycle); + + // 检查各种可能的ID形式 + let matchedId = null; + + // 1. 直接匹配字符串ID + if (serverIdsInCycle.includes(serverId)) { + matchedId = serverId; + console.log(` ✓ Direct string match: ${serverId}`); + } + // 2. 尝试匹配数字ID (如果API返回的是数字ID) + else if (serverIdsInCycle.includes(String(serverIdNum))) { + matchedId = String(serverIdNum); + console.log(` ✓ Numeric match: ${serverIdNum}`); + } + // 3. 通过名称匹配 + else { + // 检查名称是否匹配 + const serverNames = Object.entries(cycleData.server_name); + for (const [id, name] of serverNames) { + if (name === serverInfo.name) { + matchedId = id; + console.log(` ✓ Name match: ${serverInfo.name} -> ID: ${id}`); + break; + } + } + + // 如果还没匹配,尝试循环比较所有ID + if (!matchedId) { + for (const id of serverIdsInCycle) { + console.log(` Comparing IDs: ${id} vs ${serverId}`); + if (Number(id) === serverIdNum) { + matchedId = id; + console.log(` ✓ Found match after conversion: ${id}`); + break; + } + } + } + } + + // 如果找到匹配的ID,且有对应的传输数据 + if (matchedId && cycleData.transfer && cycleData.transfer[matchedId] !== undefined) { + console.log(` ✓ Found valid transfer data for server ${serverInfo.name} (ID: ${matchedId}) in cycle ${cycleId}`); + const transfer = cycleData.transfer[matchedId]; + const progress = (transfer / cycleData.max) * 100; matchedCycles.push({ name: cycleData.name, @@ -96,13 +150,16 @@ export default function ServerCard({ now, serverInfo, cycleStats }: ServerCardPr to: cycleData.to, max: cycleData.max, transfer: transfer, - nextUpdate: cycleData.next_update?.[serverId] || "", + nextUpdate: cycleData.next_update?.[matchedId] || "", progress: progress - }) + }); + } else { + console.log(` ✗ No valid transfer data found for this server in this cycle`); } - }) + }); - return matchedCycles.length > 0 ? matchedCycles : null + console.log('Matched cycles result:', matchedCycles); + return matchedCycles.length > 0 ? matchedCycles : null; } const serverCycleData = getServerCycleData() @@ -171,7 +228,7 @@ export default function ServerCard({ now, serverInfo, cycleStats }: ServerCardPr {/* 添加流量使用统计 */} - {serverCycleData && ( + {serverCycleData && serverCycleData.length > 0 && (