31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import text
|
|
from app.api.db import get_db
|
|
from pydantic import BaseModel
|
|
from app.settings import WHITELIST_TABLE, WHITELIST_COLUMN
|
|
|
|
router = APIRouter()
|
|
|
|
# 请求模型
|
|
class EmailRequest(BaseModel):
|
|
email: str
|
|
|
|
# 检查邮箱是否在白名单中
|
|
@router.post("/check-email")
|
|
async def check_email_in_whitelist(request: EmailRequest, db: Session = Depends(get_db)):
|
|
try:
|
|
# 构建查询语句
|
|
query = text(f"SELECT COUNT(*) FROM {WHITELIST_TABLE} WHERE {WHITELIST_COLUMN} = :email")
|
|
|
|
# 执行查询
|
|
result = db.execute(query, {"email": request.email})
|
|
count = result.scalar()
|
|
|
|
if count > 0:
|
|
return {"status": "success", "message": "继续操作"}
|
|
else:
|
|
raise HTTPException(status_code=403, detail="邮箱不在白名单中,请联系管理员")
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"服务器错误:{str(e)}") |