jiachenlong/backend/app/routers/auth.py

490 lines
11 KiB
Python
Raw Normal View History

# auth - 认证路由
# Version: 1.2.85
# 更新:
from fastapi import APIRouter
# Version: 1.2.x
# 更新:
2026-03-23 11:08:52 +08:00
from fastapi import APIRouter, Depends, HTTPException, status, Body
# 更新:
2026-03-23 11:08:52 +08:00
from fastapi.security import OAuth2PasswordRequestForm
# 更新:
2026-03-23 11:08:52 +08:00
from sqlalchemy.orm import Session
# 更新:
2026-03-23 11:08:52 +08:00
from app.core.database import get_db
# 更新:
2026-03-23 11:08:52 +08:00
from app.core.auth import verify_password, create_access_token, get_password_hash, get_current_user
# 更新:
2026-03-23 11:08:52 +08:00
from app.models.models import User
# 更新:
2026-03-23 11:08:52 +08:00
from app.schemas.schemas import Token, UserCreate, UserResponse
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
router = APIRouter(prefix="/api/auth", tags=["认证"])
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
def generate_user_code(db):
# 更新:
"""生成用户编码从201开始按自然数顺序递增跳过已存在的"""
# 更新:
2026-03-23 11:08:52 +08:00
# 查找最大的user_code
# 更新:
2026-03-23 11:08:52 +08:00
max_code = db.query(User.user_code).filter(User.user_code != None).order_by(User.user_code.desc()).first()
# 更新:
2026-03-23 11:08:52 +08:00
if max_code and max_code[0]:
# 更新:
2026-03-23 11:08:52 +08:00
try:
# 更新:
2026-03-23 11:08:52 +08:00
num = int(max_code[0]) + 1
# 更新:
2026-04-09 00:41:21 +08:00
if num < 201:
# 更新:
2026-04-09 00:41:21 +08:00
num = 201
# 更新:
# 检查是否已存在,如果存在则继续递增
# 更新:
while db.query(User).filter(User.user_code == str(num)).first():
# 更新:
num += 1
# 更新:
2026-04-09 00:41:21 +08:00
return str(num)
# 更新:
2026-03-23 11:08:52 +08:00
except:
# 更新:
2026-03-23 11:08:52 +08:00
pass
# 更新:
2026-04-09 00:41:21 +08:00
return "201"
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.post("/register", response_model=UserResponse)
# 更新:
2026-03-23 11:08:52 +08:00
def register(user_data: UserCreate, db: Session = Depends(get_db)):
# 更新:
2026-03-23 11:08:52 +08:00
"""用户注册"""
# 更新:
2026-03-23 11:08:52 +08:00
# 检查用户名是否已存在
# 更新:
2026-03-23 11:08:52 +08:00
existing_user = db.query(User).filter(User.f01_01_name == user_data.f01_01_name).first()
# 更新:
2026-03-23 11:08:52 +08:00
if existing_user:
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(
# 更新:
2026-03-23 11:08:52 +08:00
status_code=status.HTTP_400_BAD_REQUEST,
# 更新:
2026-03-23 11:08:52 +08:00
detail="f01_01_name: 用户名已存在"
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 检查邮箱是否已存在
# 更新:
# 更新:
# 检查手机号是否已存在
# 更新:
if user_data.phone:
# 更新:
existing_phone = db.query(User).filter(User.phone == user_data.phone).first()
# 更新:
if existing_phone:
# 更新:
raise HTTPException(
# 更新:
status_code=status.HTTP_400_BAD_REQUEST,
# 更新:
detail="E00040:该手机号已被注册,请更换手机号"
# 更新:
)
# 更新:
2026-03-23 11:08:52 +08:00
if user_data.email:
# 更新:
2026-03-23 11:08:52 +08:00
existing_email = db.query(User).filter(User.email == user_data.email).first()
# 更新:
2026-03-23 11:08:52 +08:00
if existing_email:
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(
# 更新:
2026-03-23 11:08:52 +08:00
status_code=status.HTTP_400_BAD_REQUEST,
# 更新:
detail="E00041:该邮箱已被注册,请更换邮箱"
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
# 处理邀请码
# 更新:
invited_by_user = None
# 更新:
if user_data.invite_code:
# 更新:
# 查找邀请人
# 更新:
invited_by_user = db.query(User).filter(User.user_code == user_data.invite_code).first()
# 更新:
if not invited_by_user:
# 更新:
raise HTTPException(
# 更新:
status_code=status.HTTP_400_BAD_REQUEST,
# 更新:
detail="E00042:邀请码无效"
# 更新:
)
# 更新:
# 更新:
2026-03-23 11:08:52 +08:00
# 创建用户
# 更新:
2026-03-23 11:08:52 +08:00
import uuid
# 更新:
2026-03-23 11:08:52 +08:00
hashed_password = get_password_hash(user_data.password)
# 更新:
generated_code = generate_user_code(db)
# 更新:
2026-03-23 11:08:52 +08:00
user = User(
# 更新:
2026-03-23 11:08:52 +08:00
f99_90_id=str(uuid.uuid4()),
# 更新:
2026-03-23 11:08:52 +08:00
f99_91_user_id=str(uuid.uuid4()),
# 更新:
user_code=generated_code,
# 更新:
2026-03-23 11:08:52 +08:00
f01_01_name=user_data.f01_01_name,
# 更新:
2026-03-23 11:08:52 +08:00
email=user_data.email,
# 更新:
2026-03-23 11:08:52 +08:00
phone=user_data.phone,
# 更新:
2026-03-23 11:08:52 +08:00
avatar=user_data.avatar,
# 更新:
2026-03-23 11:08:52 +08:00
address=user_data.address,
# 更新:
2026-03-23 11:08:52 +08:00
bio=user_data.bio,
# 更新:
2026-03-23 11:08:52 +08:00
password=hashed_password,
# 更新:
2026-03-23 11:08:52 +08:00
role="user"
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
db.add(user)
# 更新:
db.flush() # 确保获取user ID
# 更新:
# 更新:
# 更新邀请人、被邀请人的关联关系
# 更新:
if invited_by_user:
# 更新:
# 记录是被谁邀请的
# 更新:
user.f01_13_invite_code = invited_by_user.user_code
# 更新:
# 增加邀请人的邀请计数
# 更新:
invited_by_user.f99_101_invited_count = (invited_by_user.f99_101_invited_count or 0) + 1
# 更新:
# 更新:
# 生成自己的邀请码用自己的user_code
# 更新:
user.f01_13_invite_code = generated_code
# 更新:
# 更新:
2026-03-23 11:08:52 +08:00
db.commit()
# 更新:
2026-03-23 11:08:52 +08:00
db.refresh(user)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
# 返回用户信息避免Pydantic序列化问题
# 更新:
return {
# 更新:
"id": user.f99_90_id,
# 更新:
"username": user.f01_01_name,
# 更新:
"user_code": user.user_code,
# 更新:
"email": user.email,
# 更新:
"phone": user.phone,
# 更新:
"avatar": user.avatar,
# 更新:
"role": user.role,
# 更新:
"level": user.f99_94_level,
# 更新:
"aiCount": user.f99_95_ai_count or 0,
# 更新:
"searchCount": user.f99_96_search_count or 0,
# 更新:
"collectionCount": user.f99_97_collection_count or 0
# 更新:
}
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.post("/login", response_model=Token)
# 更新:
2026-03-23 11:08:52 +08:00
def login(
# 更新:
2026-03-23 11:08:52 +08:00
form_data: OAuth2PasswordRequestForm = Depends(),
# 更新:
2026-03-23 11:08:52 +08:00
db: Session = Depends(get_db)
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-04-09 00:45:19 +08:00
"""用户登录 - 支持用户名或用户编码登录"""
# 更新:
2026-04-09 00:45:19 +08:00
# 先尝试用户名登录
# 更新:
2026-03-23 11:08:52 +08:00
user = db.query(User).filter(User.f01_01_name == form_data.username).first()
# 更新:
2026-04-09 00:45:19 +08:00
# 如果用户名不存在,尝试用户编码登录
# 更新:
2026-04-09 00:45:19 +08:00
if not user:
# 更新:
2026-04-09 00:45:19 +08:00
user = db.query(User).filter(User.user_code == form_data.username).first()
# 更新:
2026-03-23 11:08:52 +08:00
if not user:
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(
# 更新:
2026-03-23 11:08:52 +08:00
status_code=status.HTTP_401_UNAUTHORIZED,
# 更新:
2026-03-23 11:08:52 +08:00
detail="E00011: 用户名或密码错误",
# 更新:
2026-03-23 11:08:52 +08:00
headers={"WWW-Authenticate": "Bearer"},
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 验证密码
# 更新:
2026-03-23 11:08:52 +08:00
if not verify_password(form_data.password, user.password):
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(
# 更新:
2026-03-23 11:08:52 +08:00
status_code=status.HTTP_401_UNAUTHORIZED,
# 更新:
2026-03-23 11:08:52 +08:00
detail="E00011: 用户名或密码错误",
# 更新:
2026-03-23 11:08:52 +08:00
headers={"WWW-Authenticate": "Bearer"},
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
# 更新登录次数和最后登录时间
# 更新:
from datetime import datetime
# 更新:
user.f99_98_login_count = (user.f99_98_login_count or 0) + 1
# 更新:
user.f99_99_last_login = datetime.now()
# 更新:
db.commit()
# 更新:
# 更新:
2026-03-23 11:08:52 +08:00
# 生成 token
# 更新:
2026-03-23 11:08:52 +08:00
access_token = create_access_token(data={"sub": user.f99_90_id})
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
return {
# 更新:
2026-03-23 11:08:52 +08:00
"access_token": access_token,
# 更新:
2026-03-23 11:08:52 +08:00
"token_type": "bearer"
# 更新:
2026-03-23 11:08:52 +08:00
}
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.get("/me", response_model=UserResponse)
# 更新:
2026-03-23 11:08:52 +08:00
def get_current_user_info(
# 更新:
2026-03-23 11:08:52 +08:00
current_user: User = Depends(lambda: None)
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-03-23 11:08:52 +08:00
"""获取当前用户信息"""
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(
# 更新:
2026-03-23 11:08:52 +08:00
status_code=status.HTTP_501_NOT_IMPLEMENTED,
# 更新:
2026-03-23 11:08:52 +08:00
detail="请使用正确的依赖注入"
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.post("/change-password")
# 更新:
2026-03-23 11:08:52 +08:00
def change_password(
# 更新:
2026-03-23 11:08:52 +08:00
old_password: str = Body(...),
# 更新:
2026-03-23 11:08:52 +08:00
new_password: str = Body(...),
# 更新:
2026-03-23 11:08:52 +08:00
current_user: User = Depends(get_current_user),
# 更新:
2026-03-23 11:08:52 +08:00
db: Session = Depends(get_db)
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-03-23 11:08:52 +08:00
"""修改当前用户密码"""
# 更新:
2026-03-23 11:08:52 +08:00
from app.core.auth import verify_password, get_password_hash
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 在当前session中重新查询用户
# 更新:
2026-03-23 11:08:52 +08:00
user = db.query(User).filter(User.f99_90_id == current_user.f99_90_id).first()
# 更新:
2026-03-23 11:08:52 +08:00
if not user:
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(status_code=404, detail="用户不存在")
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 验证旧密码
# 更新:
2026-03-23 11:08:52 +08:00
if not verify_password(old_password, user.password):
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(
# 更新:
2026-03-23 11:08:52 +08:00
status_code=status.HTTP_400_BAD_REQUEST,
# 更新:
2026-03-23 11:08:52 +08:00
detail="当前密码错误"
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新密码
# 更新:
2026-03-23 11:08:52 +08:00
user.password = get_password_hash(new_password)
# 更新:
2026-03-23 11:08:52 +08:00
db.commit()
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
return {"message": "密码修改成功"}
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# ============ 短信验证码接口 ============
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.post("/send-verification-code")
# 更新:
2026-03-23 11:08:52 +08:00
def send_verification_code(
# 更新:
2026-03-23 11:08:52 +08:00
phone: str = Body(..., min_length=11, max_length=11),
# 更新:
2026-03-23 11:08:52 +08:00
purpose: str = Body("register") # register | login | reset_password
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-03-23 11:08:52 +08:00
"""发送短信验证码"""
# 更新:
2026-03-23 11:08:52 +08:00
from app.services.sms import send_verification_code as send_sms
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 验证手机号格式
# 更新:
2026-03-23 11:08:52 +08:00
if not phone.startswith("1") or len(phone) != 11:
# 更新:
2026-03-23 11:08:52 +08:00
return {"success": False, "message": "手机号格式不正确"}
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
result = send_sms(phone)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
if result["success"]:
# 更新:
2026-03-23 11:08:52 +08:00
return {
# 更新:
2026-03-23 11:08:52 +08:00
"success": True,
# 更新:
2026-03-23 11:08:52 +08:00
"message": f"验证码已发送到 {phone[:3]}****{phone[7:]}",
# 更新:
2026-03-23 11:08:52 +08:00
"expire": result.get("expire", 300)
# 更新:
2026-03-23 11:08:52 +08:00
}
# 更新:
2026-03-23 11:08:52 +08:00
else:
# 更新:
2026-03-23 11:08:52 +08:00
return result
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.post("/verify-code")
# 更新:
2026-03-23 11:08:52 +08:00
def verify_code(
# 更新:
2026-03-23 11:08:52 +08:00
phone: str = Body(...),
# 更新:
2026-03-23 11:08:52 +08:00
code: str = Body(..., min_length=6, max_length=6)
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-03-23 11:08:52 +08:00
"""验证短信验证码(仅验证,不执行后续操作)"""
# 更新:
2026-03-23 11:08:52 +08:00
from app.services.sms import verify_code as check_code
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
is_valid = check_code(phone, code)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
if is_valid:
# 更新:
2026-03-23 11:08:52 +08:00
return {"success": True, "message": "验证成功"}
# 更新:
2026-03-23 11:08:52 +08:00
else:
# 更新:
2026-03-23 11:08:52 +08:00
return {"success": False, "message": "验证码错误或已过期"}
# 更新: