1
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
//! 按本地 oem 文件中的 ID 拉取公开品牌信息。
|
||||
//! 按本地 oem 文件中的 ID 拉取公开品牌信息,并同步窗口/任务栏标题与图标。
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use tauri::{image::Image, WebviewWindow};
|
||||
use tauri::State;
|
||||
|
||||
use crate::api_client;
|
||||
use crate::oem_context::OemContext;
|
||||
|
||||
const DEFAULT_TITLE: &str = "aiclient";
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SoftwareInfo {
|
||||
@@ -52,20 +55,86 @@ fn parse_software_info(oem_id: i64, value: &Value) -> SoftwareInfo {
|
||||
}
|
||||
}
|
||||
|
||||
async fn apply_window_branding(window: &WebviewWindow, info: &SoftwareInfo) {
|
||||
let title = info.software_name.trim();
|
||||
let title = if title.is_empty() {
|
||||
DEFAULT_TITLE
|
||||
} else {
|
||||
title
|
||||
};
|
||||
if let Err(e) = window.set_title(title) {
|
||||
log::warn!("设置窗口标题失败: {e}");
|
||||
}
|
||||
|
||||
let Some(logo_url) = info.logo_url.as_deref().filter(|s| !s.is_empty()) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let client = match reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(30))
|
||||
.build()
|
||||
{
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
log::warn!("创建 HTTP 客户端失败: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let response = match client.get(logo_url).send().await {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
log::warn!("下载 logo 失败: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
log::warn!("下载 logo HTTP {}", response.status());
|
||||
return;
|
||||
}
|
||||
|
||||
let bytes = match response.bytes().await {
|
||||
Ok(b) => b,
|
||||
Err(e) => {
|
||||
log::warn!("读取 logo 内容失败: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if bytes.is_empty() {
|
||||
log::warn!("logo 内容为空");
|
||||
return;
|
||||
}
|
||||
|
||||
match Image::from_bytes(&bytes) {
|
||||
Ok(icon) => {
|
||||
if let Err(e) = window.set_icon(icon) {
|
||||
log::warn!("设置窗口图标失败: {e}");
|
||||
}
|
||||
}
|
||||
Err(e) => log::warn!("解析 logo 图片失败: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_software_info(ctx: State<'_, OemContext>) -> Result<SoftwareInfo, String> {
|
||||
pub async fn get_software_info(
|
||||
window: WebviewWindow,
|
||||
ctx: State<'_, OemContext>,
|
||||
) -> Result<SoftwareInfo, String> {
|
||||
let oem_id = ctx.oem_id;
|
||||
let path = format!("/public/oem-branding/{oem_id}");
|
||||
let (status, value) =
|
||||
api_client::request_json("GET", &path, None, HashMap::new()).await?;
|
||||
|
||||
if status >= 400 {
|
||||
return Ok(empty_info(oem_id));
|
||||
}
|
||||
let info = if status >= 400 {
|
||||
empty_info(oem_id)
|
||||
} else if value.get("ok").and_then(|v| v.as_bool()) == Some(false) {
|
||||
empty_info(oem_id)
|
||||
} else {
|
||||
parse_software_info(oem_id, &value)
|
||||
};
|
||||
|
||||
if value.get("ok").and_then(|v| v.as_bool()) == Some(false) {
|
||||
return Ok(empty_info(oem_id));
|
||||
}
|
||||
|
||||
Ok(parse_software_info(oem_id, &value))
|
||||
apply_window_branding(&window, &info).await;
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user