UserServiceImpl.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package nju.seec.helper.service.impl;
  2. import nju.seec.helper.aspect.auth.LoginUser;
  3. import nju.seec.helper.dao.UserDAO;
  4. import nju.seec.helper.dto.user.ResetPasswordDTO;
  5. import nju.seec.helper.dto.user.UserDTO;
  6. import nju.seec.helper.entity.User;
  7. import nju.seec.helper.enums.ExceptionType;
  8. import nju.seec.helper.enums.UserState;
  9. import nju.seec.helper.enums.UserType;
  10. import nju.seec.helper.exception.HelperException;
  11. import nju.seec.helper.service.UserService;
  12. import nju.seec.helper.util.Consts;
  13. import nju.seec.helper.util.EncryptUtils;
  14. import nju.seec.helper.util.cache.RedisCacheUtils;
  15. import nju.seec.helper.vo.UserVO;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. /**
  19. * @author cst
  20. */
  21. @Service
  22. public class UserServiceImpl implements UserService {
  23. private final RedisCacheUtils cacheUtils;
  24. private final UserDAO userDAO;
  25. public UserServiceImpl(UserDAO userDAO, RedisCacheUtils cacheUtils) {
  26. this.userDAO = userDAO;
  27. this.cacheUtils = cacheUtils;
  28. }
  29. @Transactional(rollbackFor = Exception.class)
  30. @Override
  31. public UserVO createUser(UserDTO userDTO) {
  32. checkSameEmail(userDTO.getEmail());
  33. checkSamePhone(userDTO.getPhone());
  34. String email = userDTO.getEmail();
  35. if (!userDTO.getEmailCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, email))) {
  36. throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误");
  37. }
  38. String phone = userDTO.getPhone();
  39. if (!userDTO.getPhoneCode().equals(cacheUtils.get(Consts.PHONE_CACHE_NAME, phone))) {
  40. throw HelperException.of(ExceptionType.NOT_FOUND, "手机号或验证码错误");
  41. }
  42. User user = userDAO.save(new User()
  43. .setName(userDTO.getName())
  44. .setEmail(userDTO.getEmail())
  45. .setPhone(userDTO.getPhone())
  46. .setPassword(EncryptUtils.encode(userDTO.getPassword()))
  47. .setType(userDTO.getType())
  48. .setState(userDTO.getType() == UserType.TEACHER ? UserState.CHECKING : UserState.NORMAL));
  49. cacheUtils.remove(Consts.EMAIL_CACHE_NAME, email);
  50. cacheUtils.remove(Consts.PHONE_CACHE_NAME, phone);
  51. return new UserVO(user);
  52. }
  53. @Transactional(rollbackFor = Exception.class)
  54. @Override
  55. public UserVO modifyUser(LoginUser loginUser, UserDTO userDTO) {
  56. User user = userDAO.findUserById(loginUser.getId());
  57. user.setName(userDTO.getName());
  58. user = userDAO.save(user);
  59. return new UserVO(user);
  60. }
  61. @Transactional(rollbackFor = Exception.class)
  62. @Override
  63. public void resetPasswordByEmail(ResetPasswordDTO resetPasswordDTO) {
  64. User user = userDAO.findByEmail(resetPasswordDTO.getUsername())
  65. .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "该邮箱未在系统中注册"));
  66. if (!resetPasswordDTO.getCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername()))) {
  67. throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误");
  68. }
  69. user.setPassword(EncryptUtils.encode(resetPasswordDTO.getPassword()));
  70. userDAO.save(user);
  71. cacheUtils.remove(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername());
  72. }
  73. @Transactional(readOnly = true)
  74. @Override
  75. public UserVO getUserByEmailAndPassword(String email, String password) {
  76. return userDAO
  77. .findByEmailAndPassword(email, EncryptUtils.encode(password))
  78. .map(UserVO::new)
  79. .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "邮箱或密码错误"));
  80. }
  81. @Transactional(readOnly = true)
  82. @Override
  83. public UserVO getUserById(Long userId) {
  84. return new UserVO(userDAO.findUserById(userId));
  85. }
  86. private void checkSameEmail(String email) {
  87. if (userDAO.existsByEmail(email)) {
  88. throw HelperException.of(ExceptionType.CONFLICT, "该邮箱已注册");
  89. }
  90. }
  91. private void checkSamePhone(String phone) {
  92. if (userDAO.existsByPhone(phone)) {
  93. throw HelperException.of(ExceptionType.CONFLICT, "该手机号已注册");
  94. }
  95. }
  96. }