mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-05-14 16:27:35 +08:00
feat(config): 合并上传配置并修复debug模式下数据库初始化提示错误
This commit is contained in:
@@ -514,7 +514,8 @@ def init_default_settings() -> None:
|
||||
)
|
||||
print(f"[Settings] 初始化默认设置: {defn.db_key} = {default_value if not defn.is_secret else '***'}")
|
||||
except Exception as e:
|
||||
print(f"[Settings] 初始化默认设置失败: {e}")
|
||||
if "未初始化" not in str(e):
|
||||
print(f"[Settings] 初始化默认设置失败: {e}")
|
||||
|
||||
|
||||
def _load_settings_from_db() -> Dict[str, Any]:
|
||||
@@ -549,7 +550,8 @@ def _load_settings_from_db() -> Dict[str, Any]:
|
||||
settings_dict["webui_access_password"] = env_password
|
||||
return settings_dict
|
||||
except Exception as e:
|
||||
print(f"[Settings] 从数据库加载设置失败: {e},使用默认值")
|
||||
if "未初始化" not in str(e):
|
||||
print(f"[Settings] 从数据库加载设置失败: {e},使用默认值")
|
||||
return {name: defn.default_value for name, defn in SETTING_DEFINITIONS.items()}
|
||||
|
||||
|
||||
@@ -572,7 +574,8 @@ def _save_settings_to_db(**kwargs) -> None:
|
||||
description=defn.description
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"[Settings] 保存设置到数据库失败: {e}")
|
||||
if "未初始化" not in str(e):
|
||||
print(f"[Settings] 保存设置到数据库失败: {e}")
|
||||
|
||||
|
||||
class Settings(BaseModel):
|
||||
|
||||
@@ -647,4 +647,68 @@ def delete_sub2api_service(db: Session, service_id: int) -> bool:
|
||||
return False
|
||||
db.delete(svc)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Team Manager 服务 CRUD
|
||||
# ============================================================================
|
||||
|
||||
def create_tm_service(
|
||||
db: Session,
|
||||
name: str,
|
||||
api_url: str,
|
||||
api_key: str,
|
||||
enabled: bool = True,
|
||||
priority: int = 0,
|
||||
):
|
||||
"""创建 Team Manager 服务配置"""
|
||||
from .models import TeamManagerService
|
||||
svc = TeamManagerService(
|
||||
name=name,
|
||||
api_url=api_url,
|
||||
api_key=api_key,
|
||||
enabled=enabled,
|
||||
priority=priority,
|
||||
)
|
||||
db.add(svc)
|
||||
db.commit()
|
||||
db.refresh(svc)
|
||||
return svc
|
||||
|
||||
|
||||
def get_tm_service_by_id(db: Session, service_id: int):
|
||||
"""按 ID 获取 Team Manager 服务"""
|
||||
from .models import TeamManagerService
|
||||
return db.query(TeamManagerService).filter(TeamManagerService.id == service_id).first()
|
||||
|
||||
|
||||
def get_tm_services(db: Session, enabled=None):
|
||||
"""获取 Team Manager 服务列表"""
|
||||
from .models import TeamManagerService
|
||||
q = db.query(TeamManagerService)
|
||||
if enabled is not None:
|
||||
q = q.filter(TeamManagerService.enabled == enabled)
|
||||
return q.order_by(TeamManagerService.priority.asc(), TeamManagerService.id.asc()).all()
|
||||
|
||||
|
||||
def update_tm_service(db: Session, service_id: int, **kwargs):
|
||||
"""更新 Team Manager 服务配置"""
|
||||
svc = get_tm_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
return None
|
||||
for k, v in kwargs.items():
|
||||
setattr(svc, k, v)
|
||||
db.commit()
|
||||
db.refresh(svc)
|
||||
return svc
|
||||
|
||||
|
||||
def delete_tm_service(db: Session, service_id: int) -> bool:
|
||||
"""删除 Team Manager 服务配置"""
|
||||
svc = get_tm_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
return False
|
||||
db.delete(svc)
|
||||
db.commit()
|
||||
return True
|
||||
@@ -158,6 +158,20 @@ class Sub2ApiService(Base):
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
|
||||
class TeamManagerService(Base):
|
||||
"""Team Manager 服务配置表"""
|
||||
__tablename__ = 'tm_services'
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
name = Column(String(100), nullable=False) # 服务名称
|
||||
api_url = Column(String(500), nullable=False) # API URL
|
||||
api_key = Column(Text, nullable=False) # X-API-Key
|
||||
enabled = Column(Boolean, default=True)
|
||||
priority = Column(Integer, default=0) # 优先级
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
|
||||
class Proxy(Base):
|
||||
"""代理列表表"""
|
||||
__tablename__ = 'proxies'
|
||||
|
||||
@@ -11,6 +11,7 @@ from .email_services import router as email_services_router
|
||||
from .payment import router as payment_router
|
||||
from .cpa_services import router as cpa_services_router
|
||||
from .sub2api_services import router as sub2api_services_router
|
||||
from .tm_services import router as tm_services_router
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
@@ -22,3 +23,4 @@ api_router.include_router(email_services_router, prefix="/email-services", tags=
|
||||
api_router.include_router(payment_router, prefix="/payment", tags=["payment"])
|
||||
api_router.include_router(cpa_services_router, prefix="/cpa-services", tags=["cpa-services"])
|
||||
api_router.include_router(sub2api_services_router, prefix="/sub2api-services", tags=["sub2api-services"])
|
||||
api_router.include_router(tm_services_router, prefix="/tm-services", tags=["tm-services"])
|
||||
|
||||
@@ -11,6 +11,7 @@ from pydantic import BaseModel
|
||||
|
||||
from ...database.session import get_db
|
||||
from ...database.models import Account
|
||||
from ...database import crud
|
||||
from ...config.settings import get_settings
|
||||
from .accounts import resolve_account_ids
|
||||
from ...core.payment import (
|
||||
@@ -61,12 +62,14 @@ class BatchCheckSubscriptionRequest(BaseModel):
|
||||
|
||||
class UploadTMRequest(BaseModel):
|
||||
proxy: Optional[str] = None # 保留,TM 上传不走代理
|
||||
service_id: Optional[int] = None # 指定 TM 服务 ID,不传则使用第一个启用的
|
||||
|
||||
|
||||
class BatchUploadTMRequest(BaseModel):
|
||||
ids: List[int] = []
|
||||
select_all: bool = False
|
||||
status_filter: Optional[str] = None
|
||||
service_id: Optional[int] = None # 指定 TM 服务 ID,不传则使用第一个启用的
|
||||
email_service_filter: Optional[str] = None
|
||||
search_filter: Optional[str] = None
|
||||
|
||||
@@ -200,14 +203,21 @@ def batch_check_subscription(request: BatchCheckSubscriptionRequest):
|
||||
@router.post("/accounts/{account_id}/upload-tm")
|
||||
def upload_account_tm(account_id: int, request: UploadTMRequest = None):
|
||||
"""上传单账号到 Team Manager"""
|
||||
settings = get_settings()
|
||||
if not settings.tm_enabled:
|
||||
raise HTTPException(status_code=400, detail="Team Manager 上传未启用")
|
||||
|
||||
api_url = settings.tm_api_url
|
||||
api_key = settings.tm_api_key.get_secret_value() if settings.tm_api_key else ""
|
||||
service_id = request.service_id if request and hasattr(request, 'service_id') else None
|
||||
|
||||
with get_db() as db:
|
||||
if service_id:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
else:
|
||||
svcs = crud.get_tm_services(db, enabled=True)
|
||||
svc = svcs[0] if svcs else None
|
||||
|
||||
if not svc:
|
||||
raise HTTPException(status_code=400, detail="未找到可用的 Team Manager 服务,请先在设置中配置")
|
||||
|
||||
api_url = svc.api_url
|
||||
api_key = svc.api_key
|
||||
|
||||
account = db.query(Account).filter(Account.id == account_id).first()
|
||||
if not account:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
@@ -219,14 +229,21 @@ def upload_account_tm(account_id: int, request: UploadTMRequest = None):
|
||||
@router.post("/accounts/batch-upload-tm")
|
||||
def batch_upload_tm(request: BatchUploadTMRequest):
|
||||
"""批量上传账号到 Team Manager"""
|
||||
settings = get_settings()
|
||||
if not settings.tm_enabled:
|
||||
raise HTTPException(status_code=400, detail="Team Manager 上传未启用")
|
||||
|
||||
api_url = settings.tm_api_url
|
||||
api_key = settings.tm_api_key.get_secret_value() if settings.tm_api_key else ""
|
||||
service_id = request.service_id if hasattr(request, 'service_id') else None
|
||||
|
||||
with get_db() as db:
|
||||
if service_id:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
else:
|
||||
svcs = crud.get_tm_services(db, enabled=True)
|
||||
svc = svcs[0] if svcs else None
|
||||
|
||||
if not svc:
|
||||
raise HTTPException(status_code=400, detail="未找到可用的 Team Manager 服务,请先在设置中配置")
|
||||
|
||||
api_url = svc.api_url
|
||||
api_key = svc.api_key
|
||||
|
||||
ids = resolve_account_ids(
|
||||
db, request.ids, request.select_all,
|
||||
request.status_filter, request.email_service_filter, request.search_filter
|
||||
|
||||
Reference in New Issue
Block a user