//userController
@PatchMapping("/updatePwd")
public Result updatePwd(@RequestBody Map<String,String> params){
//1.校验参数
String oldPwd = params.get("old_pwd");
String newPwd = params.get("new_pwd");
String rePwd = params.get("re_pwd");
if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
return Result.error("缺少必要的参数");
}
//判断原密码是否正确
//调用userService根据用户名拿到密码,再和old_pwd对比
Map<Object,String> map = ThreadLocalUtil.get();
String username =(String) map.get("username");
User loginUser = userService.findByUserName(username);
if(!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))){
return Result.error("原密码填写错误");
}
//newPwd和rePwd是否一样
if(!rePwd.equals(newPwd)){
return Result.error("两次填写的新密码不一样");
}
//2.调用service完成密码更新
userService.updatePwd(newPwd);
return Result.success();
//userservice
void updatePwd(String newPwd);
//userServiceImpl
@Override
public void updatePwd(String newPwd) {
Map<String,Object> map = ThreadLocalUtil.get();
Integer id = (Integer) map.get("id");
userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
}
//userMapper
@Update("update user set password=#{md5String},update_time=now() where id=#{id}")
void updatePwd(String md5String, Integer id);
标签:updatePwd,String,get,19,2024,密码,Result,id,newPwd
From: https://www.cnblogs.com/muzhaodi/p/18083919