public String ChangePassword(UserInfo userInfo){ String msg = ""; if(userInfo.getPassword().equals(userInfo.getNewPassword())){ msg = "新密码不能与旧密码相同"; throw new RuntimeException("新密码不能与旧密码相同"); } User user = selectUserByPhone(User.builder().phoneNumber(userInfo.getUsername()).build()); if (user == null) { msg = "用户不存在"; throw new RuntimeException("用户不存在"); } if (!user.getPassword().equals(userInfo.getPassword())) { msg = "旧密码不正确"; throw new RuntimeException("旧密码不正确"); } if(validatePassword(userInfo.getPassword())){ user.setPassword(userInfo.getNewPassword()); userInfoMapper.updateByPrimaryKeySelective(user); msg = "修改密码成功"; } else { msg = "旧密码不正确"; throw new RuntimeException("旧密码不正确"); } return msg; }简化代码
时间: 2024-03-21 17:41:18 浏览: 46
修改旧密码
这段代码实现了用户修改密码的功能,下面是简化后的代码:
```
public String changePassword(UserInfo userInfo) {
if (userInfo.getPassword().equals(userInfo.getNewPassword())) {
throw new RuntimeException("新密码不能与旧密码相同");
}
User user = selectUserByPhone(User.builder().phoneNumber(userInfo.getUsername()).build());
if (user == null || !user.getPassword().equals(userInfo.getPassword())) {
throw new RuntimeException("旧密码不正确");
}
user.setPassword(userInfo.getNewPassword());
userInfoMapper.updateByPrimaryKeySelective(user);
return "修改密码成功";
}
```
简化后的代码去掉了一些无用的变量和重复的判断语句,让代码更加清晰易懂。
阅读全文