|
|
@@ -1,96 +1,91 @@
|
|
|
package nju.seec.helper.service.impl;
|
|
|
|
|
|
-import cn.hutool.crypto.SecureUtil;
|
|
|
-import nju.seec.helper.aspect.auth.LoginUser;
|
|
|
import nju.seec.helper.dao.UserDAO;
|
|
|
-import nju.seec.helper.dto.user.ResetPasswordDTO;
|
|
|
-import nju.seec.helper.dto.user.UserDTO;
|
|
|
import nju.seec.helper.entity.User;
|
|
|
-import nju.seec.helper.enums.ExceptionType;
|
|
|
-import nju.seec.helper.enums.UserState;
|
|
|
-import nju.seec.helper.enums.UserType;
|
|
|
-import nju.seec.helper.exception.HelperException;
|
|
|
import nju.seec.helper.service.UserService;
|
|
|
-import nju.seec.helper.util.Consts;
|
|
|
-import nju.seec.helper.util.cache.RedisCacheUtils;
|
|
|
import nju.seec.helper.vo.UserVO;
|
|
|
+import org.springframework.data.util.Optionals;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
/**
|
|
|
* @author cst
|
|
|
*/
|
|
|
@Service
|
|
|
public class UserServiceImpl implements UserService {
|
|
|
- private final RedisCacheUtils cacheUtils;
|
|
|
+ // private final RedisCacheUtils cacheUtils;
|
|
|
private final UserDAO userDAO;
|
|
|
|
|
|
- public UserServiceImpl(UserDAO userDAO, RedisCacheUtils cacheUtils) {
|
|
|
+ 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 = userDAO.save(new User()
|
|
|
- .setName(userDTO.getName())
|
|
|
- .setEmail(userDTO.getEmail())
|
|
|
- .setPhone(userDTO.getPhone())
|
|
|
- .setPassword(SecureUtil.sha256(userDTO.getPassword()))
|
|
|
- .setType(userDTO.getType())
|
|
|
- .setState(userDTO.getType() == UserType.TEACHER ? UserState.CHECKING : UserState.NORMAL));
|
|
|
-
|
|
|
- cacheUtils.remove(Consts.EMAIL_CACHE_NAME, email);
|
|
|
- cacheUtils.remove(Consts.PHONE_CACHE_NAME, phone);
|
|
|
- return new UserVO(user);
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- @Override
|
|
|
- public UserVO modifyUser(LoginUser loginUser, UserDTO userDTO) {
|
|
|
- User user = userDAO.findUserById(loginUser.getId());
|
|
|
- user.setName(userDTO.getName());
|
|
|
- user = userDAO.save(user);
|
|
|
- return new UserVO(user);
|
|
|
+// this.cacheUtils = cacheUtils;
|
|
|
}
|
|
|
|
|
|
- @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(SecureUtil.sha256(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, SecureUtil.sha256(password))
|
|
|
- .map(UserVO::new)
|
|
|
- .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "邮箱或密码错误"));
|
|
|
- }
|
|
|
+// @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 = userDAO.save(new User()
|
|
|
+// .setName(userDTO.getName())
|
|
|
+// .setEmail(userDTO.getEmail())
|
|
|
+// .setPhone(userDTO.getPhone())
|
|
|
+// .setPassword(SecureUtil.sha256(userDTO.getPassword()))
|
|
|
+// .setType(userDTO.getType())
|
|
|
+// .setState(userDTO.getType() == UserRole.TEACHER ? UserState.CHECKING : UserState.NORMAL));
|
|
|
+//
|
|
|
+// cacheUtils.remove(Consts.EMAIL_CACHE_NAME, email);
|
|
|
+// cacheUtils.remove(Consts.PHONE_CACHE_NAME, phone);
|
|
|
+// return new UserVO(user);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Transactional(rollbackFor = Exception.class)
|
|
|
+// @Override
|
|
|
+// public UserVO modifyUser(LoginUser loginUser, UserDTO userDTO) {
|
|
|
+// User user = userDAO.findUserById(loginUser.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(SecureUtil.sha256(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, SecureUtil.sha256(password))
|
|
|
+// .map(UserVO::new)
|
|
|
+// .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "邮箱或密码错误"));
|
|
|
+// }
|
|
|
|
|
|
@Transactional(readOnly = true)
|
|
|
@Override
|
|
|
@@ -98,15 +93,26 @@ public class UserServiceImpl implements UserService {
|
|
|
return new UserVO(userDAO.findUserById(userId));
|
|
|
}
|
|
|
|
|
|
- private void checkSameEmail(String email) {
|
|
|
- if (userDAO.existsByEmail(email)) {
|
|
|
- throw HelperException.of(ExceptionType.CONFLICT, "该邮箱已注册");
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public Optional<User> getUserByPhoneOrPid(String phone, Long pid) {
|
|
|
+ return Optionals.firstNonEmpty(() -> userDAO.findByPhone(phone), () -> userDAO.findByPid(pid));
|
|
|
}
|
|
|
|
|
|
- private void checkSamePhone(String phone) {
|
|
|
- if (userDAO.existsByPhone(phone)) {
|
|
|
- throw HelperException.of(ExceptionType.CONFLICT, "该手机号已注册");
|
|
|
- }
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public User createOrUpdateUser(User user) {
|
|
|
+ return userDAO.save(user);
|
|
|
}
|
|
|
+
|
|
|
+ // 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, "该手机号已注册");
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|