This commit is contained in:
fengchuanhn@gmail.com
2026-05-17 22:05:34 +08:00
parent 9dfeaa9e18
commit c70995fbc9
14 changed files with 428 additions and 148 deletions

View File

@@ -0,0 +1,63 @@
//! 前端 API 代理:由 Rust 发起 HTTP并与 Python 端 AES 加解密对齐。
use std::collections::HashMap;
use serde_json::Value;
use crate::api_client;
fn default_method() -> String {
"GET".into()
}
/// 将 HTTP 结果整理为前端 `apiRequest` 约定结构。
fn normalize_response(status: u16, value: Value) -> Value {
if let Some(obj) = value.as_object() {
if obj.contains_key("ok") {
return value;
}
}
if status == 401 {
return serde_json::json!({ "ok": false, "message": "未登录或登录已过期" });
}
if status == 403 {
let msg = value
.get("detail")
.and_then(|d| d.as_str())
.unwrap_or("无权限执行此操作");
return serde_json::json!({ "ok": false, "message": msg });
}
if status == 422 {
let msg = value
.get("detail")
.and_then(|d| d.as_array())
.and_then(|a| a.first())
.and_then(|e| e.get("msg"))
.and_then(|m| m.as_str())
.unwrap_or("请求参数无效");
return serde_json::json!({ "ok": false, "message": msg });
}
let fallback = format!("请求失败({status}");
let message = value
.get("message")
.and_then(|m| m.as_str())
.unwrap_or(fallback.as_str());
serde_json::json!({ "ok": false, "message": message })
}
#[tauri::command]
pub async fn api_request(
path: String,
method: Option<String>,
body: Option<String>,
headers: Option<HashMap<String, String>>,
) -> Result<Value, String> {
let method = method.unwrap_or_else(|| default_method());
let headers = headers.unwrap_or_default();
let (status, value) =
api_client::request_json(&method, &path, body.as_deref(), headers).await?;
Ok(normalize_response(status, value))
}

View File

@@ -1,3 +1,4 @@
pub mod api;
pub mod app_config;
pub mod auth;
pub mod nodejs;