feat: 后端支持保存配置文件

This commit is contained in:
lanyeeee
2025-07-11 05:39:11 +08:00
parent 3ca3a48c38
commit cf64eb7cd5
3 changed files with 56 additions and 2 deletions
+42 -1
View File
@@ -1,6 +1,12 @@
use parking_lot::RwLock;
use tauri::AppHandle;
use crate::config::Config;
use crate::{
config::Config,
errors::{CommandError, CommandResult},
extensions::AppHandleExt,
logger,
};
#[tauri::command]
#[specta::specta]
@@ -14,3 +20,38 @@ pub fn greet(name: &str) -> String {
pub fn get_config(config: tauri::State<RwLock<Config>>) -> Config {
config.read().clone()
}
#[tauri::command(async)]
#[specta::specta]
#[allow(clippy::needless_pass_by_value)]
pub fn save_config(app: AppHandle, config: Config) -> CommandResult<()> {
let config_state = app.get_config();
let enable_file_logger = config.enable_file_logger;
let enable_file_logger_changed = config_state
.read()
.enable_file_logger
.ne(&enable_file_logger);
{
// 包裹在大括号中,以便自动释放写锁
let mut config_state = config_state.write();
*config_state = config;
config_state
.save(&app)
.map_err(|err| CommandError::from("保存配置失败", err))?;
tracing::debug!("保存配置成功");
}
if enable_file_logger_changed {
if enable_file_logger {
logger::reload_file_logger()
.map_err(|err| CommandError::from("重新加载文件日志失败", err))?;
} else {
logger::disable_file_logger()
.map_err(|err| CommandError::from("禁用文件日志失败", err))?;
}
}
Ok(())
}
+5 -1
View File
@@ -22,7 +22,11 @@ fn generate_context() -> tauri::Context<Wry> {
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let builder = tauri_specta::Builder::<Wry>::new()
.commands(tauri_specta::collect_commands![greet, get_config])
.commands(tauri_specta::collect_commands![
greet,
get_config,
save_config,
])
.events(tauri_specta::collect_events![LogEvent]);
#[cfg(debug_assertions)]