diff --git a/backend/routers/users.py b/backend/routers/users.py index 9df766d..e2c78fb 100644 --- a/backend/routers/users.py +++ b/backend/routers/users.py @@ -4,8 +4,8 @@ from sqlalchemy.orm import Session from typing import List from database import get_db from models import User, Role, PhaseGroup -from schemas import UserCreate, UserUpdate, UserOut -from auth import get_current_user, hash_password, require_permission +from schemas import UserCreate, UserUpdate, UserOut, ChangePasswordRequest +from auth import get_current_user, hash_password, verify_password, require_permission router = APIRouter(prefix="/api/users", tags=["用户管理"]) @@ -86,11 +86,28 @@ def update_user( user.social_insurance = req.social_insurance if req.is_active is not None: user.is_active = req.is_active + if req.password: + user.password_hash = hash_password(req.password) db.commit() db.refresh(user) return user_to_out(user) +@router.post("/change-password") +def change_password( + req: ChangePasswordRequest, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + if not verify_password(req.old_password, current_user.password_hash): + raise HTTPException(status_code=400, detail="原密码错误") + if len(req.new_password) < 4: + raise HTTPException(status_code=400, detail="新密码至少4位") + current_user.password_hash = hash_password(req.new_password) + db.commit() + return {"message": "密码修改成功"} + + @router.get("/{user_id}", response_model=UserOut) def get_user( user_id: int, diff --git a/backend/schemas.py b/backend/schemas.py index e357828..ea87f61 100644 --- a/backend/schemas.py +++ b/backend/schemas.py @@ -37,6 +37,12 @@ class UserUpdate(BaseModel): bonus: Optional[float] = None social_insurance: Optional[float] = None is_active: Optional[int] = None + password: Optional[str] = None # 管理员重置密码 + + +class ChangePasswordRequest(BaseModel): + old_password: str + new_password: str class UserOut(BaseModel): diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js index f114818..2764c9a 100644 --- a/frontend/src/api/index.js +++ b/frontend/src/api/index.js @@ -51,6 +51,7 @@ export const userApi = { create: (data) => api.post('/users', data), update: (id, data) => api.put(`/users/${id}`, data), get: (id) => api.get(`/users/${id}`), + changePassword: (data) => api.post('/users/change-password', data), } // ── 项目 ── diff --git a/frontend/src/components/Layout.vue b/frontend/src/components/Layout.vue index 4f73c9a..9d2a38c 100644 --- a/frontend/src/components/Layout.vue +++ b/frontend/src/components/Layout.vue @@ -35,6 +35,11 @@
{{ authStore.user?.role_name }}
+
+ + 修改密码 + +
@@ -55,13 +60,34 @@ + + + + + + + + + + + + + + + +