mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-05-15 20:38:28 +08:00
feat(settings): 添加验证码配置页面和数据库存储支持
This commit is contained in:
@@ -110,10 +110,6 @@ class Settings(BaseSettings):
|
||||
tempmail_timeout: int = Field(default=30)
|
||||
tempmail_max_retries: int = Field(default=3)
|
||||
|
||||
# 验证码等待配置
|
||||
email_code_timeout: int = Field(default=120) # 验证码等待超时(秒)
|
||||
email_code_poll_interval: int = Field(default=3) # 验证码轮询间隔(秒)
|
||||
|
||||
# 自定义域名邮箱配置
|
||||
custom_domain_base_url: str = Field(default="")
|
||||
custom_domain_api_key: Optional[SecretStr] = Field(default=None)
|
||||
|
||||
@@ -27,8 +27,35 @@ from ..config.constants import (
|
||||
OTP_CODE_SEMANTIC_PATTERN,
|
||||
OPENAI_EMAIL_SENDERS,
|
||||
OPENAI_VERIFICATION_KEYWORDS,
|
||||
OTP_WAIT_TIMEOUT,
|
||||
OTP_POLL_INTERVAL,
|
||||
)
|
||||
from ..config import get_settings
|
||||
from ..database import crud
|
||||
from ..database.session import get_db
|
||||
|
||||
|
||||
def get_email_code_settings() -> dict:
|
||||
"""
|
||||
从数据库获取验证码等待配置
|
||||
|
||||
Returns:
|
||||
dict: 包含 timeout 和 poll_interval 的字典
|
||||
"""
|
||||
try:
|
||||
with get_db() as db:
|
||||
timeout_setting = crud.get_setting(db, "email_code.timeout")
|
||||
poll_interval_setting = crud.get_setting(db, "email_code.poll_interval")
|
||||
|
||||
return {
|
||||
"timeout": int(timeout_setting.value) if timeout_setting else OTP_WAIT_TIMEOUT,
|
||||
"poll_interval": int(poll_interval_setting.value) if poll_interval_setting else OTP_POLL_INTERVAL,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning(f"获取验证码配置失败,使用默认值: {e}")
|
||||
return {
|
||||
"timeout": OTP_WAIT_TIMEOUT,
|
||||
"poll_interval": OTP_POLL_INTERVAL,
|
||||
}
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -474,10 +501,10 @@ class OutlookService(BaseEmailService):
|
||||
self.update_status(False, EmailServiceError(f"未找到邮箱对应的账户: {email}"))
|
||||
return None
|
||||
|
||||
# 使用配置的超时时间
|
||||
settings = get_settings()
|
||||
actual_timeout = timeout or settings.email_code_timeout
|
||||
poll_interval = settings.email_code_poll_interval
|
||||
# 从数据库获取验证码等待配置
|
||||
code_settings = get_email_code_settings()
|
||||
actual_timeout = timeout or code_settings["timeout"]
|
||||
poll_interval = code_settings["poll_interval"]
|
||||
|
||||
logger.info(f"[{email}] 开始获取验证码,超时 {actual_timeout}s,OTP发送时间: {otp_sent_at}")
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ from pydantic import BaseModel
|
||||
from ...database import crud
|
||||
from ...database.session import get_db
|
||||
from ...config.settings import get_settings, update_settings
|
||||
from ...config.constants import OTP_WAIT_TIMEOUT, OTP_POLL_INTERVAL
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
@@ -71,6 +72,11 @@ async def get_all_settings():
|
||||
"""获取所有设置"""
|
||||
settings = get_settings()
|
||||
|
||||
# 从数据库获取验证码设置
|
||||
with get_db() as db:
|
||||
timeout_setting = crud.get_setting(db, "email_code.timeout")
|
||||
poll_interval_setting = crud.get_setting(db, "email_code.poll_interval")
|
||||
|
||||
return {
|
||||
"proxy": {
|
||||
"enabled": settings.proxy_enabled,
|
||||
@@ -97,6 +103,10 @@ async def get_all_settings():
|
||||
"timeout": settings.tempmail_timeout,
|
||||
"max_retries": settings.tempmail_max_retries,
|
||||
},
|
||||
"email_code": {
|
||||
"timeout": int(timeout_setting.value) if timeout_setting else OTP_WAIT_TIMEOUT,
|
||||
"poll_interval": int(poll_interval_setting.value) if poll_interval_setting else OTP_POLL_INTERVAL,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -362,6 +372,12 @@ class TempmailSettings(BaseModel):
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class EmailCodeSettings(BaseModel):
|
||||
"""验证码等待设置"""
|
||||
timeout: int = 120 # 验证码等待超时(秒)
|
||||
poll_interval: int = 3 # 验证码轮询间隔(秒)
|
||||
|
||||
|
||||
@router.get("/tempmail")
|
||||
async def get_tempmail_settings():
|
||||
"""获取临时邮箱设置"""
|
||||
@@ -388,6 +404,49 @@ async def update_tempmail_settings(request: TempmailSettings):
|
||||
return {"success": True, "message": "临时邮箱设置已更新"}
|
||||
|
||||
|
||||
# ============== 验证码等待设置 ==============
|
||||
|
||||
@router.get("/email-code")
|
||||
async def get_email_code_settings():
|
||||
"""获取验证码等待设置"""
|
||||
with get_db() as db:
|
||||
timeout_setting = crud.get_setting(db, "email_code.timeout")
|
||||
poll_interval_setting = crud.get_setting(db, "email_code.poll_interval")
|
||||
|
||||
return {
|
||||
"timeout": int(timeout_setting.value) if timeout_setting else OTP_WAIT_TIMEOUT,
|
||||
"poll_interval": int(poll_interval_setting.value) if poll_interval_setting else OTP_POLL_INTERVAL,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/email-code")
|
||||
async def update_email_code_settings(request: EmailCodeSettings):
|
||||
"""更新验证码等待设置"""
|
||||
with get_db() as db:
|
||||
# 验证参数范围
|
||||
if request.timeout < 30 or request.timeout > 600:
|
||||
raise HTTPException(status_code=400, detail="超时时间必须在 30-600 秒之间")
|
||||
if request.poll_interval < 1 or request.poll_interval > 30:
|
||||
raise HTTPException(status_code=400, detail="轮询间隔必须在 1-30 秒之间")
|
||||
|
||||
crud.set_setting(
|
||||
db,
|
||||
"email_code.timeout",
|
||||
str(request.timeout),
|
||||
description="验证码等待超时(秒)",
|
||||
category="email"
|
||||
)
|
||||
crud.set_setting(
|
||||
db,
|
||||
"email_code.poll_interval",
|
||||
str(request.poll_interval),
|
||||
description="验证码轮询间隔(秒)",
|
||||
category="email"
|
||||
)
|
||||
|
||||
return {"success": True, "message": "验证码等待设置已更新"}
|
||||
|
||||
|
||||
# ============== 代理列表 CRUD ==============
|
||||
|
||||
class ProxyCreateRequest(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user