修改密码
This commit is contained in:
@@ -2,6 +2,7 @@ package com.tailbet.controller;
|
||||
|
||||
import com.tailbet.model.dto.LoginDTO;
|
||||
import com.tailbet.model.dto.RegisterDTO;
|
||||
import com.tailbet.model.dto.UpdatePasswordDTO;
|
||||
import com.tailbet.model.dto.UpdateUserDTO;
|
||||
import com.tailbet.model.vo.LoginUserVo;
|
||||
import com.tailbet.model.vo.LoginVo;
|
||||
@@ -72,6 +73,17 @@ public class UserController {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*/
|
||||
@PutMapping("/password")
|
||||
public R<Void> changePassword(@RequestBody @Validated UpdatePasswordDTO dto) {
|
||||
Long userId = UserContext.getUserId();
|
||||
userService.changePassword(userId, dto.getOldPassword(), dto.getNewPassword());
|
||||
return R.ok();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
|
||||
@@ -17,8 +17,4 @@ public class UpdateUserDTO {
|
||||
*/
|
||||
private String avatarUrl;
|
||||
|
||||
/**
|
||||
* 新密码(可选)
|
||||
*/
|
||||
private String password;
|
||||
}
|
||||
|
||||
@@ -33,4 +33,9 @@ public interface IUserService extends IService<User> {
|
||||
* 更新用户信息
|
||||
*/
|
||||
void updateUserInfo(Long userId, UpdateUserDTO dto);
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*/
|
||||
void changePassword(Long userId, String oldPassword, String newPassword);
|
||||
}
|
||||
|
||||
@@ -105,9 +105,22 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
||||
if (dto.getAvatarUrl() != null) {
|
||||
user.setAvatarUrl(dto.getAvatarUrl());
|
||||
}
|
||||
if (dto.getPassword() != null && !dto.getPassword().isEmpty()) {
|
||||
user.setPassword(passwordUtil.encode(dto.getPassword()));
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*/
|
||||
@Override
|
||||
public void changePassword(Long userId, String oldPassword, String newPassword) {
|
||||
User user = userMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
if (!passwordUtil.matches(oldPassword, user.getPassword())) {
|
||||
throw new RuntimeException("原密码错误");
|
||||
}
|
||||
user.setPassword(passwordUtil.encode(newPassword));
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user