增加个人中心修改密码接口

pdf
pan 4 years ago
parent dc68352473
commit 22dc74a257
  1. 54
      src/main/java/com/bupt/note/Controller/UserController.java
  2. 2
      src/main/java/com/bupt/note/Repository/UserRepository.java
  3. 22
      src/main/java/com/bupt/note/dto/UpdatePwd.java

@ -5,12 +5,13 @@ import com.bupt.note.Repository.UserRepository;
import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.ResponseData.ResultEnums; import com.bupt.note.ResponseData.ResultEnums;
import com.bupt.note.dto.UpdatePwd;
import com.bupt.note.dto.UserForm; import com.bupt.note.dto.UserForm;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** /**
* 用户接口 * 用户接口
@ -22,6 +23,8 @@ public class UserController {
@Autowired @Autowired
UserRepository userRepository; UserRepository userRepository;
private Logger logger= LoggerFactory.getLogger(UserController.class);
/** /**
* 注册用户 * 注册用户
* @param user 用户信息 * @param user 用户信息
@ -29,8 +32,7 @@ public class UserController {
*/ */
@PostMapping("/sign_up") @PostMapping("/sign_up")
public ResponseData signUp(@RequestBody UserForm user) { public ResponseData signUp(@RequestBody UserForm user) {
User exist = userRepository.findByUserName(user.getUserName()); if (!userRepository.existsByUserName(user.getUserName())) {
if (exist == null) {
userRepository.save(user.getUser()); userRepository.save(user.getUser());
User u = userRepository.findByUserName(user.getUserName()); User u = userRepository.findByUserName(user.getUserName());
return ResponseDataUtil.buildSuccess(u); return ResponseDataUtil.buildSuccess(u);
@ -45,11 +47,43 @@ public class UserController {
*/ */
@PostMapping("/sign_in") @PostMapping("/sign_in")
public ResponseData signIn(@RequestBody UserForm user){ public ResponseData signIn(@RequestBody UserForm user){
User exist = userRepository.findByUserName(user.getUserName()); if(StringUtils.isNoneEmpty(user.getUserName(),user.getPassword())&&
if(exist==null||!exist.getPassword().equals(user.getPassword())){ userRepository.existsByUserName(user.getUserName())){
return ResponseDataUtil.buildError(ResultEnums.ERROR); User exists= userRepository.findByUserName(user.getUserName());
if(user.getPassword().equals(exists.getPassword())){
return ResponseDataUtil.buildError(ResultEnums.SUCCESS);
}else{
logger.error("账号或者密码错误");
return ResponseDataUtil.buildError(ResultEnums.ERROR);
}
}else { }else {
return ResponseDataUtil.buildSuccess(ResultEnums.SUCCESS); logger.error("登陆校验失败");
return ResponseDataUtil.buildSuccess(ResultEnums.ERROR);
}
}
/**
* 修改密码
* @param user
* @return
*/
@PutMapping("update")
public ResponseData put(@RequestBody UpdatePwd user,@CookieValue("user") String username){
if(StringUtils.isNoneEmpty(username,user.getOldpwd(),user.getNewpwd())&&
userRepository.existsByUserName(username)){
User exists= userRepository.findByUserName(username);
if(user.getOldpwd().equals(exists.getPassword())){
exists.setPassword(user.getNewpwd());
userRepository.save(exists);
return ResponseDataUtil.buildSuccess();
}else{
logger.error("原密码错误");
return ResponseDataUtil.buildError("原密码错误");
}
}else{
logger.error("修改密码表单校验失败");
return ResponseDataUtil.buildError();
} }
} }
} }

@ -4,6 +4,6 @@ import com.bupt.note.Model.User;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> { public interface UserRepository extends JpaRepository<User, Long> {
boolean existsByUserName(String username);
User findByUserName(String userName); User findByUserName(String userName);
void deleteByUserName(String userName);
} }

@ -0,0 +1,22 @@
package com.bupt.note.dto;
public class UpdatePwd {
private String oldpwd;
private String newpwd;
public String getOldpwd() {
return oldpwd;
}
public void setOldpwd(String oldpwd) {
this.oldpwd = oldpwd;
}
public String getNewpwd() {
return newpwd;
}
public void setNewpwd(String newpwd) {
this.newpwd = newpwd;
}
}
Loading…
Cancel
Save