"use client" import { Card, CardBody, CardHeader, CardIcon, CardTool, } from "@/components/window/Card" import { ToolButton } from "@/components/window/ToolButton" import { Chip, Skeleton } from "@heroui/react" import { Refresh, SettingConfig } from "@icon-park/react" // import { version } from "@tauri-apps/plugin-os" import { type AllSystemInfo, allSysInfo } from "tauri-plugin-system-info-api" import { invoke } from "@tauri-apps/api/core" import useSWR, { useSWRConfig } from "swr" export default function Page() { return (
硬件 {/* 云同步 */}
) } function HardwareInfo() { const { mutate } = useSWRConfig() return ( { // 使用 SWR 的 mutate 来刷新数据 mutate("/api/hardware-info") }}> 刷新 ) } interface ComputerInfo { OsName?: string OSDisplayVersion?: string BiosSMBIOSBIOSVersion?: string CsManufacturer?: string CsName?: string } interface HardwareData { allSysData: AllSystemInfo computerInfo: ComputerInfo } // 硬件信息 fetcher const hardwareInfoFetcher = async (): Promise => { // 并行获取系统信息和 PowerShell 信息 const [sys, computerInfoData] = await Promise.all([ allSysInfo(), invoke("get_computer_info").catch((error) => { console.error("获取 PowerShell 信息失败:", error) return {} as ComputerInfo }) ]) console.log("系统信息:", sys) console.log("PowerShell 信息:", computerInfoData) if (sys?.cpus) { console.log("CPU数据:", sys.cpus) console.log("第一个CPU:", sys.cpus[0]) if (sys.cpus[0]) { console.log("CPU字段:", Object.keys(sys.cpus[0])) } } return { allSysData: sys, computerInfo: computerInfoData } } function HardwareInfoContent() { const { data, isLoading } = useSWR( "/api/hardware-info", hardwareInfoFetcher, { revalidateOnFocus: false, revalidateOnReconnect: false, dedupingInterval: 5 * 60 * 1000, // 5分钟内相同请求去重 } ) const allSysData = data?.allSysData const computerInfo = data?.computerInfo || {} const formatBytes = (bytes?: number) => { if (!bytes) return "未知" const gb = bytes / 1024 / 1024 / 1024 if (gb >= 1) { return `${gb.toFixed(2)}GB` } const mb = bytes / 1024 / 1024 return `${mb.toFixed(2)}MB` } // 如果 PowerShell 提供了 OSDisplayVersion,直接使用;否则尝试从 kernel_version 推断 const windowsVersionCode = computerInfo.OSDisplayVersion || null const memoryUsagePercent = allSysData?.total_memory && allSysData?.used_memory !== undefined ? Math.round((allSysData.used_memory / allSysData.total_memory) * 100) : null // 计算所有CPU核心的平均频率(统一转换为GHz) const averageCpuFrequency = allSysData?.cpus && allSysData.cpus.length > 0 ? (() => { // 尝试多个可能的频率字段名 const frequencies = allSysData.cpus .map(cpu => { // 尝试不同的字段名 const freq = (cpu as any).frequency ?? (cpu as any).freq ?? (cpu as any).clock_speed return freq }) .filter((freq): freq is number => { // 确保是有效的数字且大于0 return typeof freq === 'number' && !isNaN(freq) && freq > 0 }) if (frequencies.length === 0) { console.log("未找到有效的CPU频率数据,CPU对象:", allSysData.cpus[0]) return null } const sum = frequencies.reduce((acc, freq) => acc + freq, 0) const avg = sum / frequencies.length // 判断单位并统一转换为GHz // 如果值在2-10范围,可能是GHz // 如果值在2000-10000范围,可能是MHz(需要除以1000) // 如果值在百万级别(2000000+),可能是Hz(需要除以1,000,000) let freqInGhz: number if (avg >= 1_000_000) { // Hz单位,转换为GHz freqInGhz = avg / 1_000_000 } else if (avg >= 1000) { // MHz单位,转换为GHz freqInGhz = avg / 1000 } else { // 已经是GHz单位 freqInGhz = avg } console.log("CPU频率数据:", frequencies, "原始平均值:", avg, "转换为GHz:", freqInGhz) return freqInGhz })() : null // 如果正在加载,显示 Skeleton 骨架屏 if (isLoading) { return (
{/* 系统信息 Skeleton */}
{/* 主板信息 Skeleton */}
{/* CPU 信息 Skeleton */}
{/* 内存信息 Skeleton */}
{/* GPU 信息 Skeleton */}
) } return (
{/* 系统信息 */}
系统信息
{computerInfo.OsName && ( 系统: {computerInfo.OsName} {windowsVersionCode && ( ({windowsVersionCode}) )} )} {!computerInfo.OsName && ( 系统: {allSysData?.name || "未知"} {allSysData?.os_version || ""} {allSysData?.kernel_version && ( <> {" "}{allSysData.kernel_version} {windowsVersionCode && ( ({windowsVersionCode}) )} )} )} {computerInfo.CsName && ( 主机名: {computerInfo.CsName} )} {!computerInfo.CsName && allSysData?.hostname && ( 主机名: {allSysData.hostname} )}
{/* 主板信息 */} {(computerInfo.CsManufacturer || computerInfo.BiosSMBIOSBIOSVersion) && (
主板
{computerInfo.CsManufacturer && ( 制造商: {computerInfo.CsManufacturer} )} {computerInfo.BiosSMBIOSBIOSVersion && ( BIOS版本: {computerInfo.BiosSMBIOSBIOSVersion} )}
)} {/* CPU 信息 */}
处理器
型号: {allSysData?.cpus?.[0]?.brand || "未知"} {averageCpuFrequency !== null && ( 频率: {averageCpuFrequency.toFixed(2)} GHz )} 核心数: {allSysData?.cpu_count || "未知"}
{/* 内存信息 */} {allSysData?.total_memory && (
内存
总容量: {formatBytes(allSysData.total_memory)} {allSysData.used_memory !== undefined && ( 已用: {formatBytes(allSysData.used_memory)} {memoryUsagePercent !== null && ( ({memoryUsagePercent}%) )} )} {allSysData.total_memory !== undefined && allSysData.used_memory !== undefined && ( 可用: {formatBytes(allSysData.total_memory - allSysData.used_memory)} )}
)} {/* GPU 信息 */} {allSysData?.components && allSysData.components.length > 0 && ( (() => { const gpuComponents = allSysData.components.filter((comp) => comp.label?.toLowerCase().includes('gpu') || comp.label?.toLowerCase().includes('graphics') || comp.label?.toLowerCase().includes('显卡') ) if (gpuComponents.length === 0) return null return (
显卡
{gpuComponents.map((gpu, index) => ( GPU{index > 0 ? ` ${index + 1}` : ""}: {gpu.label || "未知"} {gpu.temperature !== undefined && ( ({gpu.temperature}°C{gpu.max !== undefined ? ` / ${gpu.max}°C` : ""}) )} ))}
) })() )} {/* 电池信息 */} {allSysData?.batteries && allSysData.batteries.length > 0 && (
电池
{allSysData.batteries.map((battery, index) => ( 电池{index > 0 ? ` ${index + 1}` : ""}: {battery.state && `${battery.state} `} {battery.state_of_charge !== undefined && `${battery.state_of_charge}% `} {battery.energy_full !== undefined && battery.energy !== undefined && ( ({formatBytes(battery.energy)} / {formatBytes(battery.energy_full)}) )} ))}
)}
) }