diff --git a/pages/user/main/index.vue b/pages/user/main/index.vue index 9ad6ed7..43e74b1 100644 --- a/pages/user/main/index.vue +++ b/pages/user/main/index.vue @@ -1,13 +1,174 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/server/api/system/info.ts b/server/api/system/info.ts new file mode 100644 index 0000000..17e2e3c --- /dev/null +++ b/server/api/system/info.ts @@ -0,0 +1,92 @@ +import os from 'os'; +import { execSync } from 'child_process'; + +export default defineEventHandler(async (event) => { + try { + // 获取操作系统信息 + const platform = os.platform(); + const release = os.release(); + const hostname = os.hostname(); + + // 获取Node.js版本 + const nodeVersion = process.version; + + // 硬件信息 + const cpuInfo = os.cpus(); + const totalMemory = os.totalmem(); + const freeMemory = os.freemem(); + + // 计算CPU使用率 + const cpuUsage = getCpuUsage(); + + // 计算内存使用率 + const memoryUsage = { + total: formatBytes(totalMemory), + free: formatBytes(freeMemory), + used: formatBytes(totalMemory - freeMemory), + percent: Math.round(((totalMemory - freeMemory) / totalMemory) * 100) + }; + + return { + success: true, + data: { + system: { + platform, + release, + hostname + }, + node: nodeVersion, + hardware: { + cpu: { + model: cpuInfo[0]?.model || 'Unknown', + cores: cpuInfo.length, + usage: cpuUsage + }, + memory: memoryUsage + } + } + }; + } catch (error) { + console.error('Error fetching system info:', error); + return { + success: false, + message: '获取系统信息失败' + }; + } +}); + +// 格式化字节数为可读格式 +function formatBytes(bytes: number) { + if (bytes === 0) return '0 Bytes'; + + const k = 1024; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; +} + +// 获取CPU使用率 +function getCpuUsage() { + try { + const cpus = os.cpus(); + let totalIdle = 0; + let totalTick = 0; + + cpus.forEach(cpu => { + for (const type in cpu.times) { + totalTick += cpu.times[type as keyof typeof cpu.times]; + } + totalIdle += cpu.times.idle; + }); + + const idle = totalIdle / cpus.length; + const total = totalTick / cpus.length; + const usagePercent = Math.round(100 - (idle / total * 100)); + + return usagePercent; + } catch (error) { + console.error('Error calculating CPU usage:', error); + return 0; + } +} \ No newline at end of file diff --git a/server/api/system/performance.ts b/server/api/system/performance.ts new file mode 100644 index 0000000..62f6ef0 --- /dev/null +++ b/server/api/system/performance.ts @@ -0,0 +1,70 @@ +import os from 'os'; + +export default defineEventHandler(async (event) => { + try { + // 获取CPU信息 + const cpuUsage = getCpuUsage(); + + // 获取内存信息 + const totalMemory = os.totalmem(); + const freeMemory = os.freemem(); + const memoryUsage = { + total: formatBytes(totalMemory), + free: formatBytes(freeMemory), + used: formatBytes(totalMemory - freeMemory), + percent: Math.round(((totalMemory - freeMemory) / totalMemory) * 100) + }; + + return { + success: true, + data: { + cpu: { + usage: cpuUsage + }, + memory: memoryUsage + } + }; + } catch (error) { + console.error('Error fetching performance info:', error); + return { + success: false, + message: '获取性能数据失败' + }; + } +}); + +// 格式化字节数为可读格式 +function formatBytes(bytes: number) { + if (bytes === 0) return '0 Bytes'; + + const k = 1024; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; +} + +// 获取CPU使用率 +function getCpuUsage() { + try { + const cpus = os.cpus(); + let totalIdle = 0; + let totalTick = 0; + + cpus.forEach(cpu => { + for (const type in cpu.times) { + totalTick += cpu.times[type as keyof typeof cpu.times]; + } + totalIdle += cpu.times.idle; + }); + + const idle = totalIdle / cpus.length; + const total = totalTick / cpus.length; + const usagePercent = Math.round(100 - (idle / total * 100)); + + return usagePercent; + } catch (error) { + console.error('Error calculating CPU usage:', error); + return 0; + } +} \ No newline at end of file