mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-08-08 06:53:41 +08:00
- 新增 /api/keys/all 端点获取所有密钥 - 添加下拉菜单界面,提供复制全部密钥和验证所有密钥选项 - 重构批量重置逻辑,改为逐个处理以提供更好的进度反馈 - 新增批量操作进度模态框,实时显示操作状态和日志 - 在验证模态框中添加批次大小配置选项 - 优化用户体验,提供更直观的批量操作界面
84 lines
2.9 KiB
Python
84 lines
2.9 KiB
Python
from fastapi import APIRouter, Depends, Request
|
|
from app.service.key.key_manager import KeyManager, get_key_manager_instance
|
|
from app.core.security import verify_auth_token
|
|
from fastapi.responses import JSONResponse
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/api/keys")
|
|
async def get_keys_paginated(
|
|
request: Request,
|
|
page: int = 1,
|
|
limit: int = 10,
|
|
search: str = None,
|
|
fail_count_threshold: int = None,
|
|
status: str = "all", # 'valid', 'invalid', 'all'
|
|
key_manager: KeyManager = Depends(get_key_manager_instance),
|
|
):
|
|
"""
|
|
Get paginated, filtered, and searched keys.
|
|
"""
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
return JSONResponse(status_code=401, content={"detail": "Unauthorized"})
|
|
|
|
all_keys_with_status = await key_manager.get_all_keys_with_fail_count()
|
|
|
|
# Filter by status
|
|
if status == "valid":
|
|
keys_to_filter = all_keys_with_status["valid_keys"]
|
|
elif status == "invalid":
|
|
keys_to_filter = all_keys_with_status["invalid_keys"]
|
|
else:
|
|
# Combine both for 'all' status, which might be useful for a unified view if ever needed
|
|
keys_to_filter = {**all_keys_with_status["valid_keys"], **all_keys_with_status["invalid_keys"]}
|
|
|
|
|
|
# Further filtering (search and fail_count_threshold)
|
|
filtered_keys = {}
|
|
for key, fail_count in keys_to_filter.items():
|
|
search_match = True
|
|
if search:
|
|
search_match = search.lower() in key.lower()
|
|
|
|
fail_count_match = True
|
|
if fail_count_threshold is not None:
|
|
fail_count_match = fail_count >= fail_count_threshold
|
|
|
|
if search_match and fail_count_match:
|
|
filtered_keys[key] = fail_count
|
|
|
|
# Pagination
|
|
keys_list = list(filtered_keys.items())
|
|
total_items = len(keys_list)
|
|
start_index = (page - 1) * limit
|
|
end_index = start_index + limit
|
|
paginated_keys = dict(keys_list[start_index:end_index])
|
|
|
|
return {
|
|
"keys": paginated_keys,
|
|
"total_items": total_items,
|
|
"total_pages": (total_items + limit - 1) // limit,
|
|
"current_page": page,
|
|
}
|
|
|
|
@router.get("/api/keys/all")
|
|
async def get_all_keys(
|
|
request: Request,
|
|
key_manager: KeyManager = Depends(get_key_manager_instance),
|
|
):
|
|
"""
|
|
Get all keys (both valid and invalid) for bulk operations.
|
|
"""
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
return JSONResponse(status_code=401, content={"detail": "Unauthorized"})
|
|
|
|
all_keys_with_status = await key_manager.get_all_keys_with_fail_count()
|
|
|
|
return {
|
|
"valid_keys": list(all_keys_with_status["valid_keys"].keys()),
|
|
"invalid_keys": list(all_keys_with_status["invalid_keys"].keys()),
|
|
"total_count": len(all_keys_with_status["valid_keys"]) + len(all_keys_with_status["invalid_keys"])
|
|
}
|