UserController
@PatchMapping("/updatePwd")
//@RequestBody注解,mvc框架才能自动的去读取请求体里的json数据,转换成map集合对象
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<String,Object> map = ThreadLocalUtil.get();
String username = (String) map.get("username");
User loginUser = userService.findByName(username);
if (!loginUser.getPassword().equals(MD5Utils.string2MD5(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(MD5Utils.string2MD5(newPwd),id);
}
UserMapper
//更新用户密码
@Update("update user set password=#{string2MD5},update_time=now() where id=#{id}")
void updatePwd(String string2MD5, Integer id);