新增修改密码的接口
This commit is contained in:
parent
60011e3d9a
commit
2bd1b3dafc
@ -1,6 +1,6 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Response, Body
|
from fastapi import APIRouter, HTTPException, Depends, Response, Body
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from app.models.user import UserLogin ,UserInfo, ResetPasswordRequest,PhoneLoginRequest,VerifyCodeRequest, UserDB, UserUpdate, UserRole, UserPasswordLogin, ReferralUserInfo, generate_user_code
|
from app.models.user import UserLogin ,UserInfo, ResetPasswordRequest,PhoneLoginRequest,VerifyCodeRequest, UserDB, UserUpdate, UserRole, UserPasswordLogin, ReferralUserInfo, generate_user_code, ChangePasswordRequest
|
||||||
from app.models.coupon import CouponDB, UserCouponDB, CouponStatus
|
from app.models.coupon import CouponDB, UserCouponDB, CouponStatus
|
||||||
from app.api.deps import get_current_user, get_admin_user
|
from app.api.deps import get_current_user, get_admin_user
|
||||||
from app.models.database import get_db
|
from app.models.database import get_db
|
||||||
@ -16,7 +16,7 @@ from app.core.response import success_response, error_response, ResponseModel
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from app.models.community import CommunityDB
|
from app.models.community import CommunityDB
|
||||||
|
|
||||||
@ -465,4 +465,32 @@ async def update_user_community(
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return error_response(code=500, message=f"更新失败: {str(e)}")
|
return error_response(code=500, message=f"更新失败: {str(e)}")
|
||||||
|
|
||||||
|
@router.post("/change-password", response_model=ResponseModel)
|
||||||
|
async def change_password(
|
||||||
|
request: ChangePasswordRequest,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserDB = Depends(get_current_user) # 获取当前登录用户
|
||||||
|
):
|
||||||
|
"""通过短信验证码修改密码"""
|
||||||
|
# 验证短信验证码
|
||||||
|
redis_code = redis_client.get(f"verify_code:{current_user.phone}")
|
||||||
|
if not redis_code:
|
||||||
|
return error_response(message="验证码已过期")
|
||||||
|
|
||||||
|
if redis_code.decode() != request.verify_code:
|
||||||
|
return error_response(message="验证码错误")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 更新密码
|
||||||
|
current_user.password = get_password_hash(request.new_password)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# 删除验证码
|
||||||
|
redis_client.delete(f"verify_code:{current_user.phone}")
|
||||||
|
|
||||||
|
return success_response(message="密码修改成功")
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
return error_response(code=500, message=f"修改密码失败: {str(e)}")
|
||||||
@ -87,6 +87,10 @@ class UserPasswordLogin(BaseModel):
|
|||||||
phone: str = Field(..., pattern="^1[3-9]\d{9}$")
|
phone: str = Field(..., pattern="^1[3-9]\d{9}$")
|
||||||
password: str = Field(..., min_length=6, max_length=20)
|
password: str = Field(..., min_length=6, max_length=20)
|
||||||
|
|
||||||
|
class ChangePasswordRequest(BaseModel):
|
||||||
|
verify_code: str = Field(..., min_length=6, max_length=6)
|
||||||
|
new_password: str = Field(..., min_length=6, max_length=20)
|
||||||
|
|
||||||
def generate_user_code(db=None) -> str:
|
def generate_user_code(db=None) -> str:
|
||||||
"""生成6位大写字母+数字的用户编码"""
|
"""生成6位大写字母+数字的用户编码"""
|
||||||
chars = string.ascii_uppercase + string.digits
|
chars = string.ascii_uppercase + string.digits
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user