初始化,可用。

This commit is contained in:
cheney
2026-07-07 17:53:20 +08:00
commit 73b9e679e6
26 changed files with 8325 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
use serde::Serialize;
/// 系统信息返回结构,序列化为 JSON 传回前端
#[derive(Serialize)]
struct SysInfo {
/// 目标操作系统(如 android、windows)
os: String,
/// 目标 CPU 架构(如 aarch64、x86_64)
arch: String,
/// 应用版本号
app_version: String,
}
/// 向前端返回一句问候语
///
/// # 参数
/// - `name`: 前端传入的名字
///
/// # 返回
/// 拼接后的中文问候字符串
#[tauri::command]
fn greet(name: &str) -> String {
format!("你好,{}!这条消息来自 Rust 后端 👋", name)
}
/// 汇总当前运行环境的系统信息
///
/// # 返回
/// 格式化后的 JSON 字符串(包含 os、arch、app_version)
///
/// # 注意
/// os 与 arch 取自编译期常量,反映的是构建目标平台。
#[tauri::command]
fn system_summary() -> String {
let info = SysInfo {
os: std::env::consts::OS.to_string(),
arch: std::env::consts::ARCH.to_string(),
app_version: env!("CARGO_PKG_VERSION").to_string(),
};
serde_json::to_string_pretty(&info).unwrap_or_else(|_| "{}".to_string())
}
/// 应用运行入口:注册插件与命令并启动 Tauri
///
/// # 注意
/// 使用 `#[cfg_attr(mobile, ...)]` 标记,移动端会由平台生成的入口调用此函数。
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_http::init())
.invoke_handler(tauri::generate_handler![greet, system_summary])
.run(tauri::generate_context!())
.expect("启动 Tauri 应用失败");
}
+7
View File
@@ -0,0 +1,7 @@
// 桌面端可执行文件入口:直接调用库中的 run 函数
// 移动端不会使用此文件,而是通过生成的原生工程调用 lib 中的入口。
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
tauri_demo_lib::run()
}