diff --git a/src-tauri/src/api_client.rs b/src-tauri/src/api_client.rs index 85e6ffc..81f5139 100644 --- a/src-tauri/src/api_client.rs +++ b/src-tauri/src/api_client.rs @@ -14,6 +14,7 @@ use crate::api_crypto::{self, ENCRYPTED_HEADER}; use crate::app_config; use crate::auth_session::AuthSession; use crate::device_serial; +use crate::oem_context; pub async fn request_json( method: &str, @@ -48,6 +49,7 @@ pub async fn request_json( if let Ok(val) = HeaderValue::from_str(device_serial::device_serial()) { headers.insert(HeaderName::from_static("device_serial"), val); } + oem_context::apply_install_headers(&mut headers); for (k, v) in extra_headers { if let (Ok(name), Ok(val)) = ( HeaderName::from_bytes(k.as_bytes()), diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4471906..8720cfd 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -43,7 +43,9 @@ pub fn run() { let heartbeat_handle = handle.clone(); let oem_id = oem_id::load_oem_id(&handle); let agent_id = agent_id::load_agent_id(&handle); - app.manage(oem_context::OemContext { oem_id, agent_id }); + let install_ctx = oem_context::OemContext { oem_id, agent_id }; + oem_context::init(install_ctx.clone()); + app.manage(install_ctx); tauri::async_runtime::block_on(async move { let app_config = handle.state::(); diff --git a/src-tauri/src/oem_context.rs b/src-tauri/src/oem_context.rs index 1c7f6cb..aee94b6 100644 --- a/src-tauri/src/oem_context.rs +++ b/src-tauri/src/oem_context.rs @@ -1,8 +1,37 @@ //! 启动时从 `resources/data/oem`、`resources/data/agent` 解析的安装配置。 +use once_cell::sync::OnceCell; +use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; + +static INSTALL_CTX: OnceCell = OnceCell::new(); + #[derive(Debug, Clone)] pub struct OemContext { pub oem_id: i64, /// 代理版安装包配置;无 `agent` 文件时为 `None`。 pub agent_id: Option, } + +/// 应用启动时写入,供 `api_client` 等模块读取请求头。 +pub fn init(ctx: OemContext) { + let _ = INSTALL_CTX.set(ctx); +} + +pub fn current() -> Option<&'static OemContext> { + INSTALL_CTX.get() +} + +/// 向 HTTP 请求头写入 `oem_id`、`agent_id`(与 `device_serial` 一致)。 +pub fn apply_install_headers(headers: &mut HeaderMap) { + let Some(ctx) = current() else { + return; + }; + if let Ok(val) = HeaderValue::from_str(&ctx.oem_id.to_string()) { + headers.insert(HeaderName::from_static("oem_id"), val); + } + if let Some(agent_id) = ctx.agent_id { + if let Ok(val) = HeaderValue::from_str(&agent_id.to_string()) { + headers.insert(HeaderName::from_static("agent_id"), val); + } + } +}