Client-Tauri客户端现已完整集成后端API功能,支持:
前端 (Vue3 + TypeScript)
↓ Tauri.invoke()
后端 (Rust)
↓ HTTP请求 + 加密
后端API (Python/FastAPI)
↓ 业务逻辑处理
数据库 (MySQL)
api_client.rs)pub struct BackendApiClient {
client: Client, // HTTP客户端
config: ApiConfig, // API配置
public_key: Option<String> // RSA公钥
}
// 用户相关
register() // POST /api/user/register
login() // POST /api/user/login
unbind() // POST /api/user/unbind
heartbeat() // POST /api/user/heartbeat
// 充值相关
activate_card() // POST /api/user/activate-card
// Steam账户相关
get_steam_accounts() // GET /api/user/steam-accounts
create_steam_account() // POST /api/user/steam-accounts
update_steam_account() // PUT /api/user/steam-accounts/{id}
delete_steam_account() // DELETE /api/user/steam-accounts/{id}
// 内容相关
get_announcements() // GET /api/announcement
get_public_key() // GET /api/keys/public
config.rs)#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AppConfig {
pub api: ApiConfig,
pub security: SecurityConfig,
}
// Windows
%APPDATA%\BuffClient\config.json
// macOS
~/Library/Application Support/BuffClient/config.json
// Linux
~/.local/share/BuffClient/config.json
commands.rs)#[tauri::command]
pub async fn user_login(username: String, password: String, machine_code: String) -> Result<String, String> {
let client = get_api_client_for_async()?;
let result = client.login(&username, &password, &machine_code).await?;
serde_json::to_string(&result)
}
| Tauri命令 | 对应API | HTTP方法 |
|---|---|---|
user_register |
/api/user/register |
POST |
user_login |
/api/user/login |
POST |
user_unbind |
/api/user/unbind |
POST |
user_heartbeat |
/api/user/heartbeat |
POST |
activate_card |
/api/user/activate-card |
POST |
get_steam_accounts |
/api/user/steam-accounts |
GET |
create_steam_account |
/api/user/steam-accounts |
POST |
update_steam_account |
/api/user/steam-accounts/{id} |
PUT |
delete_steam_account |
/api/user/steam-accounts/{id} |
DELETE |
get_announcements |
/api/announcement |
GET |
get_public_key |
/api/keys/public |
GET |
ApiConfig.vue)<template>
<div class="api-config">
<!-- API配置界面 -->
<div class="current-config">
<h3>当前配置</h3>
<div class="config-display">
<div class="config-item">
<label>API地址:</label>
<span>{{ currentConfig.api?.base_url }}</span>
</div>
</div>
</div>
</div>
</template>
// HTTP客户端配置
let client = Client::builder()
.timeout(Duration::from_secs(config.timeout))
.pool_max_idle_per_host(10) // 连接池
.tcp_keepalive(Duration::from_secs(60)) // TCP保活
.build()?;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_loading() {
let config = AppConfig::load().unwrap();
assert_eq!(config.api.base_url, "http://localhost:8000");
}
#[test]
fn test_api_client_creation() {
let api_config = ApiConfig {
base_url: "http://localhost:8000".to_string(),
timeout: 30,
software_code: "test".to_string(),
};
let client = BackendApiClient::new(api_config).unwrap();
assert!(client.public_key.is_none());
}
}
# API连接测试
npm run test-api-config
# 前端界面测试
npm run tauri:dev
# 编译时设置默认API地址
export BUFF_API_BASE_URL=https://api.production.com
export BUFF_API_TIMEOUT=30
# GitHub Actions
env:
BUFF_API_BASE_URL: ${{ secrets.API_BASE_URL }}
BUFF_API_TIMEOUT: 30
FROM tauri-app:latest
ENV BUFF_API_BASE_URL=https://api.production.com
EXPOSE 1420
# 1. 修改默认配置
vim config.example.json
# 2. 更新代码中的默认值
vim src/config.rs
# 3. 重新编译
npm run tauri:build
// 版本检查
const API_VERSION: &str = "v1.0";
let response = client.check_api_version().await?;
if response.version != API_VERSION {
return Err("API版本不兼容".into());
}
// 错误率监控
if error_count > threshold {
log::error!("API错误率过高: {}%", error_rate);
notify_admin("API服务异常");
}
Client-Tauri的API集成系统现已达到生产级别,具备:
🎉 你的Steam自动化客户端现在具备了完整的后端API集成能力!