This commit is contained in:
2026-01-16 00:32:23 +08:00
parent 7e8c3784c1
commit 64707768f8
4 changed files with 273 additions and 20 deletions

View File

@@ -252,10 +252,10 @@ def s2a_generate_auth_url(proxy_id: Optional[int] = None) -> Tuple[Optional[str]
session_id = data.get("session_id")
if auth_url and session_id:
log.success(f"生成 S2A 授权 URL 成功 (Session: {session_id[:16]}...)")
log.success(f"生成 S2A 授权链接成功 (会话: {session_id[:16]}...)")
return auth_url, session_id
log.error(f"生成 S2A 授权 URL 失败: HTTP {response.status_code}")
log.error(f"生成 S2A 授权链接失败: HTTP {response.status_code}")
return None, None
except Exception as e:
@@ -263,6 +263,67 @@ def s2a_generate_auth_url(proxy_id: Optional[int] = None) -> Tuple[Optional[str]
return None, None
def s2a_verify_account_in_pool(email: str, timeout: int = 10) -> Tuple[bool, Optional[Dict[str, Any]]]:
"""验证账号是否已成功入库到 S2A 账号池
通过请求 /admin/accounts 接口,检查第一个账号的 name 是否匹配邮箱
Args:
email: 要验证的邮箱地址
timeout: 超时时间 (秒)
Returns:
tuple: (是否成功, 账号数据或None)
"""
headers = build_s2a_headers()
try:
# 使用 search 参数搜索该邮箱
params = {
"page": 1,
"page_size": 20,
"platform": "",
"type": "",
"status": "",
"search": email,
"timezone": "Asia/Shanghai"
}
response = http_session.get(
f"{S2A_API_BASE}/admin/accounts",
headers=headers,
params=params,
timeout=timeout
)
if response.status_code == 200:
result = response.json()
if result.get("code") == 0:
data = result.get("data", {})
items = data.get("items", [])
if items:
# 检查第一个账号的 name 是否匹配
first_account = items[0]
account_name = first_account.get("name", "")
# 邮箱匹配检查 (忽略大小写)
if email.lower() in account_name.lower() or account_name.lower() in email.lower():
return True, first_account
return False, None
else:
log.warning(f"S2A 验证账号失败: {result.get('message', '未知错误')}")
else:
log.warning(f"S2A 验证账号失败: HTTP {response.status_code}")
return False, None
except Exception as e:
log.warning(f"S2A 验证账号异常: {e}")
return False, None
def s2a_create_account_from_oauth(
code: str,
session_id: str,
@@ -288,6 +349,9 @@ def s2a_create_account_from_oauth(
"priority": S2A_PRIORITY,
}
# 获取完整邮箱用于后续验证
full_email = name if "@" in name else ""
if name:
payload["name"] = name
if proxy_id is not None:
@@ -298,6 +362,7 @@ def s2a_create_account_from_oauth(
payload["group_ids"] = group_ids
try:
log.step("正在提交授权码到 S2A...")
response = http_session.post(
f"{S2A_API_BASE}/admin/openai/create-from-oauth",
headers=headers,
@@ -311,10 +376,25 @@ def s2a_create_account_from_oauth(
account_data = result.get("data", {})
account_id = account_data.get("id")
account_name = account_data.get("name")
log.success(f"S2A 账号创建成功 (ID: {account_id}, Name: {account_name})")
log.success(f"S2A 授权成功 (ID: {account_id}, 名称: {account_name})")
# 验证账号是否成功入库
if full_email or account_name:
verify_email = full_email or account_name
log.step(f"正在验证账号入库状态...")
verified, verified_data = s2a_verify_account_in_pool(verify_email)
if verified:
verified_id = verified_data.get("id", "")
verified_name = verified_data.get("name", "")
log.success(f"✅ 账号入库验证成功 (ID: {verified_id}, 名称: {verified_name})")
else:
log.warning(f"⚠️ 账号入库验证失败,但授权已成功")
return account_data
else:
log.error(f"S2A 账号创建失败: {result.get('message', 'Unknown error')}")
error_msg = result.get('message', '未知错误')
log.error(f"S2A 账号创建失败: {error_msg}")
else:
log.error(f"S2A 账号创建失败: HTTP {response.status_code}")