diff --git a/src-tauri/src/cmd/system.rs b/src-tauri/src/cmd/system.rs index 2ef6cf72..8ba1e6c6 100644 --- a/src-tauri/src/cmd/system.rs +++ b/src-tauri/src/cmd/system.rs @@ -1,26 +1,28 @@ +use std::sync::Arc; + use super::CmdResult; use crate::{ - core::{CoreManager, handle}, + core::{CoreManager, handle, manager::RunningMode}, logging, module::sysinfo::PlatformSpecification, utils::logging::Type, }; +#[cfg(target_os = "windows")] +use deelevate::{PrivilegeLevel, Token}; use once_cell::sync::Lazy; -use std::{ - sync::atomic::{AtomicI64, Ordering}, - time::{SystemTime, UNIX_EPOCH}, -}; use tauri_plugin_clipboard_manager::ClipboardExt; +use tokio::time::Instant; // 存储应用启动时间的全局变量 -static APP_START_TIME: Lazy = Lazy::new(|| { - // 获取当前系统时间,转换为毫秒级时间戳 - let now = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap_or_default() - .as_millis() as i64; - - AtomicI64::new(now) +static APP_START_TIME: Lazy = Lazy::new(Instant::now); +#[cfg(not(target_os = "windows"))] +static APPS_RUN_AS_ADMIN: Lazy = Lazy::new(|| unsafe { libc::geteuid() } == 0); +#[cfg(target_os = "windows")] +static APPS_RUN_AS_ADMIN: Lazy = Lazy::new(|| { + Token::with_current_process() + .and_then(|token| token.privilege_level()) + .map(|level| level != PrivilegeLevel::NotPrivileged) + .unwrap_or(false) }); #[tauri::command] @@ -45,52 +47,18 @@ pub async fn get_system_info() -> CmdResult { /// 获取当前内核运行模式 #[tauri::command] -pub async fn get_running_mode() -> Result { - Ok(CoreManager::global().get_running_mode().to_string()) +pub async fn get_running_mode() -> Result, String> { + Ok(CoreManager::global().get_running_mode()) } /// 获取应用的运行时间(毫秒) #[tauri::command] -pub fn get_app_uptime() -> CmdResult { - let start_time = APP_START_TIME.load(Ordering::Relaxed); - let now = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap_or_default() - .as_millis() as i64; - - Ok(now - start_time) +pub fn get_app_uptime() -> CmdResult { + Ok(APP_START_TIME.elapsed().as_millis()) } /// 检查应用是否以管理员身份运行 #[tauri::command] -#[cfg(target_os = "windows")] pub fn is_admin() -> CmdResult { - use deelevate::{PrivilegeLevel, Token}; - - let result = Token::with_current_process() - .and_then(|token| token.privilege_level()) - .map(|level| level != PrivilegeLevel::NotPrivileged) - .unwrap_or(false); - - Ok(result) -} - -/// 非Windows平台检测是否以管理员身份运行 -#[tauri::command] -#[cfg(not(target_os = "windows"))] -pub fn is_admin() -> CmdResult { - #[cfg(target_os = "macos")] - { - Ok(unsafe { libc::geteuid() } == 0) - } - - #[cfg(target_os = "linux")] - { - Ok(unsafe { libc::geteuid() } == 0) - } - - #[cfg(not(any(target_os = "macos", target_os = "linux")))] - { - Ok(false) - } + Ok(*APPS_RUN_AS_ADMIN) }