This commit is contained in:
fengchuanhn@gmail.com
2026-05-22 18:13:05 +08:00
parent 1368db488f
commit 0eb5dbde5b
3 changed files with 34 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ use crate::api_crypto::{self, ENCRYPTED_HEADER};
use crate::app_config; use crate::app_config;
use crate::auth_session::AuthSession; use crate::auth_session::AuthSession;
use crate::device_serial; use crate::device_serial;
use crate::oem_context;
pub async fn request_json( pub async fn request_json(
method: &str, method: &str,
@@ -48,6 +49,7 @@ pub async fn request_json(
if let Ok(val) = HeaderValue::from_str(device_serial::device_serial()) { if let Ok(val) = HeaderValue::from_str(device_serial::device_serial()) {
headers.insert(HeaderName::from_static("device_serial"), val); headers.insert(HeaderName::from_static("device_serial"), val);
} }
oem_context::apply_install_headers(&mut headers);
for (k, v) in extra_headers { for (k, v) in extra_headers {
if let (Ok(name), Ok(val)) = ( if let (Ok(name), Ok(val)) = (
HeaderName::from_bytes(k.as_bytes()), HeaderName::from_bytes(k.as_bytes()),

View File

@@ -43,7 +43,9 @@ pub fn run() {
let heartbeat_handle = handle.clone(); let heartbeat_handle = handle.clone();
let oem_id = oem_id::load_oem_id(&handle); let oem_id = oem_id::load_oem_id(&handle);
let agent_id = agent_id::load_agent_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 { tauri::async_runtime::block_on(async move {
let app_config = handle.state::<app_config::AppConfig>(); let app_config = handle.state::<app_config::AppConfig>();

View File

@@ -1,8 +1,37 @@
//! 启动时从 `resources/data/oem`、`resources/data/agent` 解析的安装配置。 //! 启动时从 `resources/data/oem`、`resources/data/agent` 解析的安装配置。
use once_cell::sync::OnceCell;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
static INSTALL_CTX: OnceCell<OemContext> = OnceCell::new();
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct OemContext { pub struct OemContext {
pub oem_id: i64, pub oem_id: i64,
/// 代理版安装包配置;无 `agent` 文件时为 `None`。 /// 代理版安装包配置;无 `agent` 文件时为 `None`。
pub agent_id: Option<i64>, pub agent_id: Option<i64>,
} }
/// 应用启动时写入,供 `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);
}
}
}