feat: 添加 WEIXIN_BASE_URL 环境变量支持
- WeChatClient::new() 中增加 WEIXIN_BASE_URL 环境变量检查 - 优先级: 显式参数 > WEIXIN_BASE_URL > 默认值 - FIXED_BASE_URL 改为函数 fixed_base_url(),同样支持环境变量覆盖 - 与 DEEPSEEK_BASE_URL 的环境变量模式保持一致
This commit is contained in:
+12
-6
@@ -39,15 +39,21 @@ impl WeChatClient {
|
|||||||
const ILINK_APP_ID: &'static str = "";
|
const ILINK_APP_ID: &'static str = "";
|
||||||
/// 默认 bot_type
|
/// 默认 bot_type
|
||||||
const DEFAULT_BOT_TYPE: &'static str = "3";
|
const DEFAULT_BOT_TYPE: &'static str = "3";
|
||||||
/// 默认 API 地址
|
/// 默认 API 地址(可通过 WEIXIN_BASE_URL 环境变量覆盖)
|
||||||
const DEFAULT_BASE_URL: &'static str = "https://ilinkai.weixin.qq.com";
|
const DEFAULT_BASE_URL: &'static str = "https://ilinkai.weixin.qq.com";
|
||||||
/// 二维码固定 API 地址
|
/// 二维码固定 API 地址(可通过 WEIXIN_BASE_URL 环境变量覆盖)
|
||||||
const FIXED_BASE_URL: &'static str = "https://ilinkai.weixin.qq.com";
|
fn fixed_base_url() -> String {
|
||||||
|
std::env::var("WEIXIN_BASE_URL")
|
||||||
|
.unwrap_or_else(|_| "https://ilinkai.weixin.qq.com".to_string())
|
||||||
|
}
|
||||||
/// 版本号编码:0x00MMNNPP
|
/// 版本号编码:0x00MMNNPP
|
||||||
const CLIENT_VERSION: u32 = 0x000100_00; // 1.0.0
|
const CLIENT_VERSION: u32 = 0x000100_00; // 1.0.0
|
||||||
|
|
||||||
pub fn new(base_url: Option<String>) -> Self {
|
pub fn new(base_url: Option<String>) -> 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 {
|
Self {
|
||||||
http: HttpClient::builder()
|
http: HttpClient::builder()
|
||||||
@@ -70,7 +76,7 @@ impl WeChatClient {
|
|||||||
pub async fn get_qrcode(&self) -> Result<(String, String), String> {
|
pub async fn get_qrcode(&self) -> Result<(String, String), String> {
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/ilink/bot/get_bot_qrcode?bot_type={}",
|
"{}/ilink/bot/get_bot_qrcode?bot_type={}",
|
||||||
Self::FIXED_BASE_URL,
|
&Self::fixed_base_url(),
|
||||||
Self::DEFAULT_BOT_TYPE
|
Self::DEFAULT_BOT_TYPE
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -176,7 +182,7 @@ impl WeChatClient {
|
|||||||
|
|
||||||
let deadline = tokio::time::Instant::now() + Duration::from_secs(timeout_secs);
|
let deadline = tokio::time::Instant::now() + Duration::from_secs(timeout_secs);
|
||||||
let mut scanned = false;
|
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 {
|
while tokio::time::Instant::now() < deadline {
|
||||||
let status = self.poll_qr_status(&qrcode, ¤t_base).await?;
|
let status = self.poll_qr_status(&qrcode, ¤t_base).await?;
|
||||||
|
|||||||
Reference in New Issue
Block a user