From 1bce26c81a07a572fb86b23b9e4b7f9c63abdeb3 Mon Sep 17 00:00:00 2001 From: cnlimiter Date: Fri, 20 Mar 2026 13:36:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(accounts):=20=E4=BD=BF=E7=94=A8=E4=B8=89?= =?UTF-8?q?=E7=BA=A7=E4=BB=A3=E7=90=86=E7=AD=96=E7=95=A5=EF=BC=88=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=88=97=E8=A1=A8=E2=86=92=E5=8A=A8=E6=80=81=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E2=86=92=E9=9D=99=E6=80=81=E9=85=8D=E7=BD=AE=EF=BC=89?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=88=B7=E6=96=B0/=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E7=AE=80=E5=8D=95=E4=BB=A3=E7=90=86=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/web/routes/accounts.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/web/routes/accounts.py b/src/web/routes/accounts.py index 60e2e75..44d4b7a 100644 --- a/src/web/routes/accounts.py +++ b/src/web/routes/accounts.py @@ -20,6 +20,7 @@ from ...core.upload.cpa_upload import generate_token_json, batch_upload_to_cpa, from ...core.upload.team_manager_upload import upload_to_team_manager, batch_upload_to_team_manager from ...core.upload.sub2api_upload import batch_upload_to_sub2api, upload_to_sub2api +from ...core.dynamic_proxy import get_proxy_url_for_task from ...database import crud from ...database.models import Account from ...database.session import get_db @@ -28,6 +29,20 @@ logger = logging.getLogger(__name__) router = APIRouter() +def _get_proxy(request_proxy: Optional[str] = None) -> Optional[str]: + """获取代理 URL,策略与注册流程一致:代理列表 → 动态代理 → 静态配置""" + if request_proxy: + return request_proxy + with get_db() as db: + proxy = crud.get_random_proxy(db) + if proxy: + return proxy.proxy_url + proxy_url = get_proxy_url_for_task() + if proxy_url: + return proxy_url + return get_settings().proxy_url + + # ============== Pydantic Models ============== class AccountResponse(BaseModel): @@ -585,8 +600,7 @@ class BatchValidateRequest(BaseModel): @router.post("/batch-refresh") async def batch_refresh_tokens(request: BatchRefreshRequest, background_tasks: BackgroundTasks): """批量刷新账号 Token""" - # 使用传入的代理或全局代理配置 - proxy = request.proxy if request.proxy else get_settings().proxy_url + proxy = _get_proxy(request.proxy) results = { "success_count": 0, @@ -618,9 +632,7 @@ async def batch_refresh_tokens(request: BatchRefreshRequest, background_tasks: B @router.post("/{account_id}/refresh") async def refresh_account_token(account_id: int, request: Optional[TokenRefreshRequest] = Body(default=None)): """刷新单个账号的 Token""" - - # 使用传入的代理或全局代理配置 - proxy = request.proxy if request and request.proxy else get_settings().proxy_url + proxy = _get_proxy(request.proxy if request else None) result = do_refresh(account_id, proxy) if result.success: @@ -639,9 +651,7 @@ async def refresh_account_token(account_id: int, request: Optional[TokenRefreshR @router.post("/batch-validate") async def batch_validate_tokens(request: BatchValidateRequest): """批量验证账号 Token 有效性""" - - # 使用传入的代理或全局代理配置 - proxy = request.proxy if request.proxy else get_settings().proxy_url + proxy = _get_proxy(request.proxy) results = { "valid_count": 0, @@ -681,9 +691,7 @@ async def batch_validate_tokens(request: BatchValidateRequest): @router.post("/{account_id}/validate") async def validate_account_token(account_id: int, request: Optional[TokenValidateRequest] = Body(default=None)): """验证单个账号的 Token 有效性""" - - # 使用传入的代理或全局代理配置 - proxy = request.proxy if request and request.proxy else get_settings().proxy_url + proxy = _get_proxy(request.proxy if request else None) is_valid, error = do_validate(account_id, proxy) return {