table_game/backend/app/routers/user_auth.py

146 lines
4.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import JSONResponse # 新增导入
from fastapi.security import OAuth2PasswordRequestForm
from ..schemas.user_auth import (
UserLoginRequest,
CodeLoginRequest,
ResetPasswordRequest,
SendCodeRequest,
RegisterRequest,
CheckUserExistenceRequest,
WechatBindRequest,
UseWxGetPhoneNumberRequest
)
from ..schemas.admin_auth import TokenResponse
from ..services.user_login_service import (
authenticate_user,
send_verification_code,
verify_code_login,
reset_password,
register_user,
check_user_existence,
check_wx_bind_status,
bind_wechat_openid,
use_wx_phoneNumber
)
from ..services.auth_service import generate_login_token
from ..utils.jwt_handler import verify_token
router = APIRouter()
@router.post("/login", response_model=TokenResponse)
async def user_login(request: UserLoginRequest):
"""手机号密码登录"""
try:
user = await authenticate_user(request.phone_number, request.password)
# 检查用户类型是否为普通用户
if user["user_type"] not in ["player", "user"]:
raise HTTPException(403, "非普通用户禁止登录")
# 生成访问令牌默认记住登录状态7天
token, expires_in = generate_login_token(user["phone_number"], remember_me=True)
return {
"access_token": token,
"token_type": "bearer",
"expires_in": expires_in
}
except HTTPException as e:
raise e
except Exception:
raise HTTPException(500, "登录服务暂时不可用")
@router.post("/send_code")
async def send_sms_code(request: SendCodeRequest):
"""发送短信验证码"""
try:
return await send_verification_code(request.phone_number)
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(500, f"短信发送失败: {str(e)}")
@router.post("/login_with_code", response_model=TokenResponse)
async def code_login(request: CodeLoginRequest):
"""验证码登录"""
try:
user = await verify_code_login(request.phone_number, request.code)
# 生成访问令牌默认记住登录状态7天
token, expires_in = generate_login_token(user["phone_number"], remember_me=True)
return {
"access_token": token,
"token_type": "bearer",
"expires_in": expires_in
}
except HTTPException as e:
raise e
except Exception:
raise HTTPException(500, "登录服务暂时不可用")
@router.post("/reset_password")
async def user_reset_password(request: ResetPasswordRequest):
"""重置密码(需验证短信验证码)"""
try:
return await reset_password(request)
except HTTPException as e:
raise e
except Exception:
raise HTTPException(500, "密码重置服务暂时不可用")
@router.post("/register")
async def user_register(request: RegisterRequest):
"""注册新用户"""
try:
return await register_user(request.phone_number, request.code, request.username, request.password)
except HTTPException as e:
raise e
except Exception:
raise HTTPException(500, "注册服务暂时不可用")
@router.post("/check-existence")
async def check_user_existence_route(request: CheckUserExistenceRequest):
return await check_user_existence(request.phone_number)
# 在现有路由之后添加
@router.post("/check_wx_bind")
async def check_wx_bind(token: str):
"""检查微信绑定状态"""
try:
return await check_wx_bind_status(token)
except HTTPException as e:
raise e
except Exception:
raise HTTPException(500, "服务暂时不可用")
@router.post("/bind_wechat")
async def bind_wechat_account(request: WechatBindRequest):
"""绑定微信账号"""
try:
return await bind_wechat_openid(request.token, request.code)
except HTTPException as e:
raise e
except Exception:
raise HTTPException(500, "微信绑定服务暂时不可用")
@router.post("/useWxGetPhoneNumber")
async def use_wx_get_phone_number(request: UseWxGetPhoneNumberRequest):
"""使用微信登录获取手机号"""
try:
result = await use_wx_phoneNumber(request.code)
# 生成JWT令牌记住登录状态7天
token, expires_in = generate_login_token(result["user"]["phone_number"], remember_me=True)
return JSONResponse(
status_code=200,
content={
"access_token": token,
"token_type": "bearer",
"expires_in": expires_in
}
)
except HTTPException as e:
raise e
except Exception:
raise HTTPException(500, "微信绑定服务暂时不可用")