v1.0.0提交
This commit is contained in:
57
backend/routers/v1/user.py
Normal file
57
backend/routers/v1/user.py
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
PerToolBox Server - 用户路由
|
||||
Copyright (C) 2024 Sea Network Technology Studio
|
||||
Author: Canglan <admin@sea-studio.top>
|
||||
License: AGPL v3
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from ...dependencies import CurrentUserDependency, DbDependency
|
||||
from ...models import User
|
||||
from ...schemas import UserResponse, UserUpdateRequest
|
||||
from ...utils.security import hash_password, verify_password
|
||||
from ...utils.logger import logger
|
||||
|
||||
router = APIRouter(prefix="/api/v1/user", tags=["user"])
|
||||
|
||||
@router.get("/profile", response_model=UserResponse)
|
||||
async def get_profile(current_user: CurrentUserDependency):
|
||||
"""获取当前用户信息"""
|
||||
return current_user
|
||||
|
||||
@router.put("/profile", response_model=UserResponse)
|
||||
async def update_profile(
|
||||
request: UserUpdateRequest,
|
||||
current_user: CurrentUserDependency,
|
||||
db: DbDependency
|
||||
):
|
||||
"""更新用户信息"""
|
||||
if request.username:
|
||||
current_user.username = request.username
|
||||
if request.avatar:
|
||||
current_user.avatar = request.avatar
|
||||
|
||||
db.commit()
|
||||
db.refresh(current_user)
|
||||
|
||||
logger.info(f"用户信息更新: {current_user.id}")
|
||||
return current_user
|
||||
|
||||
@router.post("/change-password")
|
||||
async def change_password(
|
||||
old_password: str,
|
||||
new_password: str,
|
||||
current_user: CurrentUserDependency,
|
||||
db: DbDependency
|
||||
):
|
||||
"""修改密码"""
|
||||
if not verify_password(old_password, current_user.password_hash):
|
||||
raise HTTPException(status_code=400, detail="原密码错误")
|
||||
|
||||
current_user.password_hash = hash_password(new_password)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"密码修改: {current_user.id}")
|
||||
return {"success": True, "message": "密码已修改"}
|
||||
Reference in New Issue
Block a user