UserServiceImpl.java 4.7 KB

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