Tauri 的插件体系以「前端调用、后端实现」为核心,所有操作都需要受安全模型约束。官方提供了一批经过审计的核心插件,覆盖文件、网络、桌面功能等高频需求。下面列出常用的插件,并附上关键用途与最小示例。
1. tauri-plugin-shell
用途:执行系统命令、在默认浏览器中打开 URL、管理子进程。
注意:命令执行需要开启 shell.execute 权限,且作用域受 scope 限制。
- 前端:
import { Command } from '@tauri-apps/plugin-shell';
const output = await Command.create('echo', ['Hello World']).execute();
- Rust 注册:
# Cargo.toml
[dependencies]
tauri-plugin-shell = "2"
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.run(tauri::generate_context!());
}
2. tauri-plugin-fs
用途:读写本地文件、目录遍历、文件元数据获取。
权限控制:必须通过 scope 明确允许访问的路径或通配符,默认禁止所有文件操作。
- 前端:
import { readTextFile, writeTextFile } from '@tauri-apps/plugin-fs';
const content = await readTextFile('/path/to/file.txt', { baseDir: 'home' });
await writeTextFile('note.txt', '新内容', { baseDir: 'appData' });
- 关键说明:
baseDir为home、appData、desktop等逻辑路径,具体映射由 Tauri 处理。
3. tauri-plugin-dialog
用途:弹出原生文件打开/保存对话框、消息确认框。
- 前端:
import { open, save, message } from '@tauri-apps/plugin-dialog';
const file = await open({ multiple: false, filters: [{ name: 'Markdown', extensions: ['md'] }] });
const savePath = await save({ defaultPath: 'note.md' });
await message('操作成功', { title: '提示', kind: 'info' });
4. tauri-plugin-http
用途:发送 HTTP 请求(GET / POST 等),支持自定义 Header、超时等。
特点:比浏览器 fetch 更灵活,可绕过 CORS 限制,但同样需要权限声明。
- 前端:
import { fetch } from '@tauri-apps/plugin-http';
const res = await fetch('https://api.example.com/data', { method: 'GET' });
const json = await res.json();
- 权限配置需在
capabilities中允许对应域名。
5. tauri-plugin-clipboard
用途:读写系统剪贴板文本和图片。
- 前端:
import { readText, writeText } from '@tauri-apps/plugin-clipboard';
await writeText('Hello Tauri');
const text = await readText();
6. tauri-plugin-global-shortcut
用途:注册全局热键,即使应用窗口未聚焦也能响应。
- 前端:
import { register } from '@tauri-apps/plugin-global-shortcut';
await register('CommandOrControl+Shift+K', () => { console.log('快捷键触发'); });
7. tauri-plugin-notification
用途:发送系统原生通知(需用户授权)。
- 前端:
import { sendNotification, requestPermission } from '@tauri-apps/plugin-notification';
const hasPermission = await requestPermission();
if (hasPermission === 'granted') {
sendNotification({ title: '提醒', body: '任务完成' });
}
8. tauri-plugin-process
用途:重启应用、退出进程、获取环境变量。
- 前端:
import { relaunch } from '@tauri-apps/plugin-process';
await relaunch(); // 重启应用
9. tauri-plugin-updater
用途:应用自动更新检查、下载、安装(通过静态 JSON 文件或自定义服务器)。
- 前端:
import { check } from '@tauri-apps/plugin-updater';
const update = await check();
if (update) {
await update.downloadAndInstall();
// 需配合 process 插件重启生效
}
10. tauri-plugin-store
用途:跨窗口持久化键值对存储(类似 localStorage 但更稳定,支持 JSON 格式)。
- 前端:
import { Store } from '@tauri-apps/plugin-store';
const store = await Store.load('settings.json');
await store.set('theme', 'dark');
await store.save();
const val = await store.get('theme');
11. tauri-plugin-log
用途:将前端日志写入文件或控制台,便于调试和收集崩溃信息。
- 前端:
import { info, error } from '@tauri-apps/plugin-log';
info('应用已启动');
error('发生异常');
12. tauri-plugin-sql
用途:在 Rust 后端操作 SQLite 数据库(也支持 MySQL/PostgreSQL),提供前端安全调用接口。
- 前端:
import Database from '@tauri-apps/plugin-sql';
const db = await Database.load('sqlite:mydb.db');
await db.execute('INSERT INTO notes (title) VALUES (?)', ['新笔记']);
const rows = await db.select('SELECT * FROM notes');
通用配置提醒
每个插件都需要在前端 package.json 中安装对应的 npm 包,并在 Rust 项目中注册 .plugin()。同时,在 Tauri 的能力文件(capabilities)中必须显式授予相应权限。例如,要使用 fs 插件读写用户文档目录:
// src-tauri/capabilities/default.json
{
"permissions": [
"fs:allow-read-text-file",
"fs:allow-write-text-file"
],
"scope": ["$APPDATA/**", "$HOME/Documents/**"]
}
这套插件组合已经覆盖了 90% 以上的桌面应用需求,且均由 Tauri 官方维护,安全性和兼容性都有保障。