| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package nju.seec.helper.service.impl;
- import com.google.common.collect.ImmutableMap;
- import nju.seec.helper.dao.UserDAO;
- import nju.seec.helper.dto.user.LoginUser;
- import nju.seec.helper.dto.user.ResetPasswordDTO;
- import nju.seec.helper.dto.user.UserDTO;
- import nju.seec.helper.entity.User;
- import nju.seec.helper.service.UserService;
- import nju.seec.helper.service.util.AuthUtils;
- import nju.seec.helper.util.Consts;
- import nju.seec.helper.util.EncryptUtils;
- import nju.seec.helper.util.RedisCacheUtils;
- import nju.seec.helper.util.enums.ExceptionType;
- import nju.seec.helper.util.enums.UserType;
- import nju.seec.helper.util.exception.HelperException;
- import nju.seec.helper.vo.UserVO;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.Map;
- /**
- * @author cst
- */
- @Service
- public class UserServiceImpl implements UserService {
- private final RedisCacheUtils cacheUtils;
- private final UserDAO userDAO;
- private final static Map<String, UserType> EMAIL_SUFFIX_AND_USER_TYPE_MAP =
- ImmutableMap.of(
- "smail.nju.edu.cn", UserType.STUDENT,
- "nju.edu.cn", UserType.TEACHER
- );
- public UserServiceImpl(UserDAO userDAO, RedisCacheUtils cacheUtils) {
- this.userDAO = userDAO;
- this.cacheUtils = cacheUtils;
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public UserVO createUser(UserDTO userDTO) {
- checkSameEmail(userDTO.getEmail());
- checkSamePhone(userDTO.getPhone());
- String email = userDTO.getEmail();
- if (!userDTO.getEmailCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, email))) {
- throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误");
- }
- String phone = userDTO.getPhone();
- if (!userDTO.getPhoneCode().equals(cacheUtils.get(Consts.PHONE_CACHE_NAME, phone))) {
- throw HelperException.of(ExceptionType.NOT_FOUND, "手机号或验证码错误");
- }
- User user = new User()
- .setName(userDTO.getName())
- .setEmail(userDTO.getEmail())
- .setPhone(userDTO.getPhone())
- .setPassword(EncryptUtils.encode(userDTO.getPassword()))
- .setType(getUserType(email));
- user = userDAO.save(user);
- cacheUtils.remove(Consts.EMAIL_CACHE_NAME, email);
- cacheUtils.remove(Consts.PHONE_CACHE_NAME, phone);
- return new UserVO(user);
- }
- private UserType getUserType(String email) {
- return EMAIL_SUFFIX_AND_USER_TYPE_MAP.get(email.substring(email.indexOf("@") + 1));
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public UserVO modifyUser(LoginUser loginUser, UserDTO userDTO) {
- User user = userDAO.findUserById(loginUser.getId());
- AuthUtils.checkDataAuth(loginUser.getId(), user.getId(), "您无权修改该用户的个人信息");
- user.setName(userDTO.getName());
- user = userDAO.save(user);
- return new UserVO(user);
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void resetPasswordByEmail(ResetPasswordDTO resetPasswordDTO) {
- User user = userDAO.findByEmail(resetPasswordDTO.getUsername())
- .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "该邮箱未在系统中注册"));
- if (!resetPasswordDTO.getCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername()))) {
- throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误");
- }
- user.setPassword(EncryptUtils.encode(resetPasswordDTO.getPassword()));
- userDAO.save(user);
- cacheUtils.remove(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername());
- }
- @Transactional(readOnly = true)
- @Override
- public UserVO getUserByEmailAndPassword(String email, String password) {
- return userDAO
- .findByEmailAndPassword(email, EncryptUtils.encode(password))
- .map(UserVO::new)
- .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "邮箱或密码错误"));
- }
- @Transactional(readOnly = true)
- @Override
- public UserVO getUserById(Long userId) {
- return new UserVO(userDAO.findUserById(userId));
- }
- private void checkSameEmail(String email) {
- if (userDAO.existsByEmail(email)) {
- throw HelperException.of(ExceptionType.CONFLICT, "该邮箱已注册");
- }
- }
- private void checkSamePhone(String phone) {
- if (userDAO.existsByPhone(phone)) {
- throw HelperException.of(ExceptionType.CONFLICT, "该手机号已注册");
- }
- }
- }
|