[feat] better hw info (still no gpu)
This commit is contained in:
@@ -336,3 +336,47 @@ pub async fn download_app_update(
|
||||
pub fn install_app_update(installer_path: String) -> Result<(), String> {
|
||||
wrap_err!(install_update(&installer_path))
|
||||
}
|
||||
|
||||
/// 获取 PowerShell Get-ComputerInfo 信息(异步版本)
|
||||
#[tauri::command]
|
||||
#[cfg(target_os = "windows")]
|
||||
pub async fn get_computer_info() -> Result<serde_json::Value, String> {
|
||||
use tokio::process::Command;
|
||||
|
||||
// 异步执行 PowerShell 命令获取计算机信息并转换为 JSON
|
||||
let output = Command::new("powershell")
|
||||
.args(&[
|
||||
"-NoProfile",
|
||||
"-Command",
|
||||
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-ComputerInfo | Select-Object OsName, OSDisplayVersion, BiosSMBIOSBIOSVersion, CsManufacturer, CsName | ConvertTo-Json -Compress"
|
||||
])
|
||||
.output()
|
||||
.await
|
||||
.map_err(|e| format!("执行 PowerShell 命令失败: {}", e))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("PowerShell 命令执行失败: {}", stderr));
|
||||
}
|
||||
|
||||
// 处理 PowerShell 输出,移除 BOM 和空白字符
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let cleaned = stdout.trim().trim_start_matches('\u{feff}'); // 移除 BOM
|
||||
|
||||
// 如果输出为空,返回空对象
|
||||
if cleaned.is_empty() {
|
||||
return Ok(serde_json::json!({}));
|
||||
}
|
||||
|
||||
let json: serde_json::Value = serde_json::from_str(cleaned)
|
||||
.map_err(|e| format!("解析 JSON 失败: {},原始输出: {}", e, cleaned))?;
|
||||
|
||||
Ok(json)
|
||||
}
|
||||
|
||||
/// 获取 PowerShell Get-ComputerInfo 信息(非 Windows 平台返回空对象)
|
||||
#[tauri::command]
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
pub async fn get_computer_info() -> Result<serde_json::Value, String> {
|
||||
Ok(serde_json::json!({}))
|
||||
}
|
||||
@@ -169,6 +169,7 @@ fn main() {
|
||||
cmds::check_app_update,
|
||||
cmds::download_app_update,
|
||||
cmds::install_app_update,
|
||||
cmds::get_computer_info,
|
||||
on_button_clicked
|
||||
])
|
||||
.run(ctx)
|
||||
|
||||
Reference in New Issue
Block a user