From 079be45615a2cdf1b747793f2b732d11e97b65dc Mon Sep 17 00:00:00 2001 From: wunianxiao Date: Tue, 9 Jun 2026 20:59:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20WEIXIN=5FBASE=5FUR?= =?UTF-8?q?L=20=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - WeChatClient::new() 中增加 WEIXIN_BASE_URL 环境变量检查 - 优先级: 显式参数 > WEIXIN_BASE_URL > 默认值 - FIXED_BASE_URL 改为函数 fixed_base_url(),同样支持环境变量覆盖 - 与 DEEPSEEK_BASE_URL 的环境变量模式保持一致 --- src/wechat/client.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/wechat/client.rs b/src/wechat/client.rs index 1660646..07a0db8 100644 --- a/src/wechat/client.rs +++ b/src/wechat/client.rs @@ -39,15 +39,21 @@ impl WeChatClient { const ILINK_APP_ID: &'static str = ""; /// 默认 bot_type const DEFAULT_BOT_TYPE: &'static str = "3"; - /// 默认 API 地址 + /// 默认 API 地址(可通过 WEIXIN_BASE_URL 环境变量覆盖) const DEFAULT_BASE_URL: &'static str = "https://ilinkai.weixin.qq.com"; - /// 二维码固定 API 地址 - const FIXED_BASE_URL: &'static str = "https://ilinkai.weixin.qq.com"; + /// 二维码固定 API 地址(可通过 WEIXIN_BASE_URL 环境变量覆盖) + fn fixed_base_url() -> String { + std::env::var("WEIXIN_BASE_URL") + .unwrap_or_else(|_| "https://ilinkai.weixin.qq.com".to_string()) + } /// 版本号编码:0x00MMNNPP const CLIENT_VERSION: u32 = 0x000100_00; // 1.0.0 pub fn new(base_url: Option) -> Self { - let base = base_url.unwrap_or_else(|| Self::DEFAULT_BASE_URL.to_string()); + // 优先级: 显式参数 > WEIXIN_BASE_URL 环境变量 > 默认值 + let base = base_url + .or_else(|| std::env::var("WEIXIN_BASE_URL").ok()) + .unwrap_or_else(|| Self::DEFAULT_BASE_URL.to_string()); Self { http: HttpClient::builder() @@ -70,7 +76,7 @@ impl WeChatClient { pub async fn get_qrcode(&self) -> Result<(String, String), String> { let url = format!( "{}/ilink/bot/get_bot_qrcode?bot_type={}", - Self::FIXED_BASE_URL, + &Self::fixed_base_url(), Self::DEFAULT_BOT_TYPE ); @@ -176,7 +182,7 @@ impl WeChatClient { let deadline = tokio::time::Instant::now() + Duration::from_secs(timeout_secs); let mut scanned = false; - let mut current_base = Self::FIXED_BASE_URL.to_string(); + let mut current_base = Self::fixed_base_url(); while tokio::time::Instant::now() < deadline { let status = self.poll_qr_status(&qrcode, ¤t_base).await?;