agent init
This commit is contained in:
@@ -138,30 +138,48 @@ async def register(
|
||||
|
||||
@router.post("/login", response_model=TokenResponse)
|
||||
async def login(
|
||||
request: LoginRequest,
|
||||
request: LoginRequest = None,
|
||||
form_data: OAuth2PasswordRequestForm = Depends(),
|
||||
db: AsyncSession = Depends(get_async_db)
|
||||
):
|
||||
"""
|
||||
用户登录
|
||||
|
||||
验证手机号和密码,返回访问令牌和刷新令牌
|
||||
验证手机号和密码,返回访问令牌和刷新令牌。
|
||||
|
||||
支持两种格式:
|
||||
- JSON: {"phone": "13800138000", "password": "xxx"}
|
||||
- 表单 (Swagger UI): username=13800138000&password=xxx
|
||||
"""
|
||||
# 优先使用表单数据(Swagger UI),否则使用 JSON
|
||||
if form_data and form_data.username:
|
||||
phone = form_data.username
|
||||
password = form_data.password
|
||||
elif request:
|
||||
phone = request.phone
|
||||
password = request.password
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请提供手机号和密码"
|
||||
)
|
||||
|
||||
# 验证手机号格式(简单验证)
|
||||
if not request.phone or len(request.phone) != 11 or not request.phone.isdigit():
|
||||
if not phone or len(phone) != 11 or not phone.isdigit():
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="手机号格式不正确,应为11位数字"
|
||||
)
|
||||
|
||||
# 验证密码不为空
|
||||
if not request.password:
|
||||
if not password:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="密码不能为空"
|
||||
)
|
||||
|
||||
# 查找用户
|
||||
stmt = select(User).where(User.phone == request.phone)
|
||||
stmt = select(User).where(User.phone == phone)
|
||||
result = await db.execute(stmt)
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
@@ -172,7 +190,7 @@ async def login(
|
||||
)
|
||||
|
||||
# 验证密码
|
||||
if not verify_password(request.password, user.password_hash):
|
||||
if not verify_password(password, user.password_hash):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="手机号或密码错误"
|
||||
|
||||
Reference in New Issue
Block a user