feat(sysinfo): return structured system info

This commit is contained in:
SpencerLiang0114 2026-05-25 12:51:30 +08:00
parent 2a397d7ef6
commit 0dca41bcf7
No known key found for this signature in database
GPG Key ID: AD095D4B3FBA0615
5 changed files with 51 additions and 16 deletions

1
Cargo.lock generated
View File

@ -7571,6 +7571,7 @@ dependencies = [
"deelevate",
"libc",
"parking_lot",
"serde",
"sysinfo",
"tauri",
"tauri-plugin-clipboard-manager",

View File

@ -8,6 +8,7 @@ rust-version = "1.91"
tauri = { workspace = true }
tauri-plugin-clipboard-manager = { workspace = true }
parking_lot = { workspace = true }
serde = { workspace = true, features = ["derive"] }
# sysinfo 0.38.2 conflicts with dark-light
# see https://github.com/GuillaumeGomez/sysinfo/issues/1623
sysinfo = { version = "0.39.2", features = ["network", "system"] }

View File

@ -1,13 +1,39 @@
use parking_lot::RwLock;
use serde::Serialize;
use tauri::{AppHandle, Runtime, State, command};
use tauri_plugin_clipboard_manager::{ClipboardExt as _, Error};
use crate::Platform;
// TODO 迁移,让新的结构体允许通过 tauri command 正确使用 structure.field 方式获取信息
#[derive(Serialize)]
pub struct SystemInfo {
pub system_name: String,
pub system_version: String,
pub system_kernel_version: String,
pub system_arch: String,
pub app_version: String,
pub app_core_mode: String,
pub app_is_admin: bool,
}
impl From<&Platform> for SystemInfo {
fn from(platform: &Platform) -> Self {
Self {
system_name: platform.sysinfo.system_name.clone(),
system_version: platform.sysinfo.system_version.clone(),
system_kernel_version: platform.sysinfo.system_kernel_version.clone(),
system_arch: platform.sysinfo.system_arch.clone(),
app_version: platform.appinfo.app_version.clone(),
app_core_mode: platform.appinfo.app_core_mode.clone(),
app_is_admin: platform.appinfo.app_is_admin,
}
}
}
#[command]
pub fn get_system_info(state: State<'_, RwLock<Platform>>) -> Result<String, Error> {
Ok(state.inner().read().to_string())
pub fn get_system_info(state: State<'_, RwLock<Platform>>) -> Result<SystemInfo, Error> {
let platform = state.inner().read();
Ok(SystemInfo::from(&*platform))
}
/// 获取应用的运行时间(毫秒)

View File

@ -46,20 +46,17 @@ export const SystemInfoCard = () => {
useEffect(() => {
getSystemInfo()
.then((info) => {
const lines = info.split('\n')
if (lines.length > 0) {
const sysName = lines[0].split(': ')[1] || ''
let sysVersion = lines[1].split(': ')[1] || ''
const sysName = info.system_name
let sysVersion = info.system_version
if (
sysName &&
sysVersion.toLowerCase().startsWith(sysName.toLowerCase())
) {
sysVersion = sysVersion.substring(sysName.length).trim()
}
setOsInfo(`${sysName} ${sysVersion}`)
if (
sysName &&
sysVersion.toLowerCase().startsWith(sysName.toLowerCase())
) {
sysVersion = sysVersion.substring(sysName.length).trim()
}
setOsInfo(`${sysName} ${sysVersion}`)
})
.catch(console.error)
}, [])

View File

@ -404,8 +404,18 @@ export async function exportDiagnosticInfo() {
return invoke('export_diagnostic_info')
}
interface SystemInfo {
system_name: string
system_version: string
system_kernel_version: string
system_arch: string
app_version: string
app_core_mode: string
app_is_admin: boolean
}
export async function getSystemInfo() {
return invoke<string>('get_system_info')
return invoke<SystemInfo>('get_system_info')
}
export async function copyIconFile(