|
@@ -15,15 +15,19 @@ import cn.iselab.mooctest.user.service.VerifyCodeService;
|
|
|
import cn.iselab.mooctest.user.util.EncryptionUtil;
|
|
|
import cn.iselab.mooctest.user.util.MSUtil;
|
|
|
import cn.iselab.mooctest.user.web.data.UserVO;
|
|
|
+import cn.iselab.mooctest.user.web.data.UserVOWrapper;
|
|
|
import cn.iselab.mooctest.user.web.exception.HttpBadRequestException;
|
|
|
import cn.iselab.mooctest.user.web.logic.BaseLogic;
|
|
|
import cn.iselab.mooctest.user.web.logic.UserLogic;
|
|
|
import cn.iselab.mooctest.user.web.wrapper.UserWrapper;
|
|
|
+import com.alibaba.dubbo.rpc.filter.EchoFilter;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.commons.validator.routines.EmailValidator;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.mail.MessagingException;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.sql.Timestamp;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -52,6 +56,9 @@ public class UserLogicImpl extends BaseLogic implements UserLogic {
|
|
|
@Autowired
|
|
|
private MailProperties mailProperties;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserVOWrapper userVOWrapper;
|
|
|
+
|
|
|
@Override
|
|
|
public UserDTO getUserByEmail(String s) {
|
|
|
User user = userService.findByEmail(s);
|
|
@@ -117,6 +124,7 @@ public class UserLogicImpl extends BaseLogic implements UserLogic {
|
|
|
LOG.error("账号和密码不可为空");
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
User user;
|
|
|
try {
|
|
|
user = (!userDTO.getEmail().isEmpty())?
|
|
@@ -301,13 +309,13 @@ public class UserLogicImpl extends BaseLogic implements UserLogic {
|
|
|
public ResponseResult<String> sendVerifyEmail(UserDTO userDTO) {
|
|
|
String receiver = userDTO.getEmail();
|
|
|
ResponseResult<String> result = new ResponseResult<>();
|
|
|
- if(userService.findByEmail(userDTO.getEmail()) != null){
|
|
|
- result.init(ResponseStatus.FAILED, "该邮箱已被注册", receiver);
|
|
|
- LOG.error(result.getMsg());
|
|
|
- return result;
|
|
|
- }
|
|
|
+// if(userService.findByEmail(userDTO.getEmail()) != null){
|
|
|
+// result.init(ResponseStatus.FAILED, "该邮箱已被注册", receiver);
|
|
|
+// LOG.error(result.getMsg());
|
|
|
+// return result;
|
|
|
+// }
|
|
|
if (!EmailValidator.getInstance().isValid(userDTO.getEmail())){
|
|
|
- result.init(ResponseStatus.FAILED, "注册邮箱非法", receiver);
|
|
|
+ result.init(ResponseStatus.FAILED, "邮箱输入非法", receiver);
|
|
|
LOG.error(result.getMsg());
|
|
|
return result;
|
|
|
}
|
|
@@ -369,7 +377,7 @@ public class UserLogicImpl extends BaseLogic implements UserLogic {
|
|
|
+ VerifyConstants.TIME_OUT));
|
|
|
verifyCodeService.saveVerifyCode(verifyCode);
|
|
|
result.init(ResponseStatus.SUCCESS, "短信发送成功", receiver);
|
|
|
- LOG.info(result.getMsg() + ", mobile: " + receiver);
|
|
|
+ LOG.info(result.getMsg() + ", mobile: " + receiver + " code: " +code);
|
|
|
}else{
|
|
|
if (sendResult.equals(arrearsCode)){
|
|
|
mailService.sendArrearsEmail();
|
|
@@ -409,5 +417,201 @@ public class UserLogicImpl extends BaseLogic implements UserLogic {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public ResponseResult<UserVO> checkVerifyCode(UserVO userVO, String type){
|
|
|
+ ResponseResult<UserVO> result = new ResponseResult<>();
|
|
|
+ if (type.equals("email")){
|
|
|
+ if (!EmailValidator.getInstance().isValid(userVO.getEmail())){
|
|
|
+ result.init(ResponseStatus.FAILED, "邮箱输入非法", userVO);
|
|
|
+ LOG.error(result.getMsg() + " email: " + userVO.getEmail());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if(! verifyCodeService.validateVerifyCode("email", userVO.getEmail(), userVO.getVerifyCode())){
|
|
|
+ result.init(ResponseStatus.FAILED, "请确认邮箱及验证码输入正确", userVO);
|
|
|
+ LOG.error(result.getMsg());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ User user = userService.findByEmail(userVO.getEmail());
|
|
|
+ if (user == null){
|
|
|
+ result.init(ResponseStatus.FAILED, "用户不存在", userVO);
|
|
|
+ LOG.error(result.getMsg());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ userVO = resetCodeStr(user);
|
|
|
+ result.init(ResponseStatus.SUCCESS, "验证码输入正确", userVO);
|
|
|
+ LOG.info(result.getMsg() + " email: " + userVO.getEmail()+ " code: " + userVO.getVerifyCode());
|
|
|
+ return result;
|
|
|
+ }catch (Exception e){
|
|
|
+ LOG.error("重置密码加密口令出错", e);
|
|
|
+ result.init(ResponseStatus.FAILED, "系统繁忙", null);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type.equals("mobile")){
|
|
|
+ if (userVO.getMobile() == null || !MSUtil.isMobileNO(userVO.getMobile())){
|
|
|
+ result.init(ResponseStatus.FAILED, "手机号不正确", userVO);
|
|
|
+ LOG.error(result.getMsg() + userVO.getMobile());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if(!verifyCodeService.validateVerifyCode("mobile", userVO.getMobile(), userVO.getVerifyCode())){
|
|
|
+ result.init(ResponseStatus.FAILED, "手机号或验证码不正确", userVO);
|
|
|
+ LOG.error(result.getMsg());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ User user = userService.findByMobile(userVO.getMobile());
|
|
|
+ if (user == null){
|
|
|
+ result.init(ResponseStatus.FAILED, "该手机号未绑定慕测账号", userVO);
|
|
|
+ LOG.error(result.getMsg() + userVO.getMobile());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ userVO = resetCodeStr(user);
|
|
|
+ result.init(ResponseStatus.SUCCESS, "验证码输入正确", userVO);
|
|
|
+ LOG.info(result.getMsg() + " email: " + userVO.getMobile()+ " code: " + userVO.getVerifyCode());
|
|
|
+ return result;
|
|
|
+ } catch (Exception e){
|
|
|
+ LOG.error("重置密码加密口令出错", e);
|
|
|
+ result.init(ResponseStatus.FAILED, "系统繁忙", null);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ result.init(ResponseStatus.FAILED, "验证出错", userVO);
|
|
|
+ LOG.error(result.getMsg());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseResult<UserVO> resetPassword(UserVO userVO){
|
|
|
+ ResponseResult<UserVO> result = new ResponseResult<>();
|
|
|
+ String encrypt = userVO.getVerifyCode();
|
|
|
+ try{
|
|
|
+ String base64DecodeStr = new String(Base64.decodeBase64(encrypt.getBytes("UTF-8")));
|
|
|
+ String decrypt = EncryptionUtil.decryptDES(base64DecodeStr);
|
|
|
+ String[] items = decrypt.split("_");
|
|
|
+ long userId = Long.valueOf(items[1]);
|
|
|
+ long expireTime = Long.valueOf(items[2]);
|
|
|
+ if (expireTime < System.currentTimeMillis()){
|
|
|
+ throw new Exception("验证已过期");
|
|
|
+ }
|
|
|
+ User user = userService.findById(userId);
|
|
|
+ if (user == null){
|
|
|
+ throw new Exception("用户不存在");
|
|
|
+ }
|
|
|
+ if (userVO.getPassword().length() > 16 || userVO.getPassword().length() < 6){
|
|
|
+ throw new Exception("密码长度应在6-16位");
|
|
|
+ }
|
|
|
+ user.setPassword(EncryptionUtil.encryptMD5(userVO.getPassword()));
|
|
|
+ user = userService.update(user);
|
|
|
+ userVO = userVOWrapper.wrap(user);
|
|
|
+ result.init(ResponseStatus.SUCCESS, "密码重置成功", userVO);
|
|
|
+ LOG.info(result.getMsg() + "userId: " + userVO.getId());
|
|
|
+ } catch (Exception e){
|
|
|
+ result.init(ResponseStatus.FAILED, e.getMessage(), userVO);
|
|
|
+ LOG.error(result.getMsg(), e);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserVO resetCodeStr(User user) throws Exception {
|
|
|
+ UserVO userVO = userVOWrapper.wrap(user);
|
|
|
+ String passStr = "user_" + user.getId() + "_" +
|
|
|
+ String.valueOf(System.currentTimeMillis() +
|
|
|
+ VerifyConstants.TIME_OUT);
|
|
|
+ String encryptStr;
|
|
|
+ encryptStr = EncryptionUtil.encryptDES(passStr);
|
|
|
+ String base64Encrypt = new String(Base64.encodeBase64(encryptStr.getBytes("UTF-8")));
|
|
|
+ String code = base64Encrypt;
|
|
|
+ userVO.setVerifyCode(code);
|
|
|
+ return userVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseResult<UserVO> updateUserPassword(UserVO userVO, HttpServletRequest request){
|
|
|
+ ResponseResult<UserVO> result = new ResponseResult<>();
|
|
|
+ Long userId = (Long) request.getSession().getAttribute("userId");
|
|
|
+ try {
|
|
|
+ if (userId == null)
|
|
|
+ throw new Exception("用户未登录");
|
|
|
+ if (userVO.getPassword().length() > 16 || userVO.getPassword().length() < 6){
|
|
|
+ throw new Exception("密码长度应在6-16位");
|
|
|
+ }
|
|
|
+ User user = userService.findById(userId);
|
|
|
+ if (!EncryptionUtil.encryptMD5(userVO.getOldPassword()).equals(user.getPassword()))
|
|
|
+ throw new Exception("旧密码错误");
|
|
|
+
|
|
|
+ String password = EncryptionUtil.encryptMD5(userVO.getPassword());
|
|
|
+ user.setPassword(password);
|
|
|
+ user = userService.update(user);
|
|
|
+ userVO = userVOWrapper.wrap(user);
|
|
|
+ userVO.setPassword("");
|
|
|
+ userVO.setPassword2("");
|
|
|
+ result.init(ResponseStatus.SUCCESS, "密码修改成功", userVO);
|
|
|
+ LOG.info(result.getMsg()+" userId: "+userVO.getId());
|
|
|
+ request.getSession().removeAttribute("userId");
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.init(ResponseStatus.FAILED, e.getMessage(), userVO);
|
|
|
+ LOG.error(result.getMsg(),e);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseResult<UserVO> updateUserEmail(UserVO userVO, HttpServletRequest request) {
|
|
|
+ ResponseResult<UserVO> result = new ResponseResult<>();
|
|
|
+ Long userId = (Long) request.getSession().getAttribute("userId");
|
|
|
+ try {
|
|
|
+ if (userId == null)
|
|
|
+ throw new Exception("用户未登录");
|
|
|
+ if (!verifyCodeService.validateVerifyCode("email", userVO.getEmail(), userVO.getVerifyCode()))
|
|
|
+ throw new Exception("验证码或邮箱填写错误");
|
|
|
+ User user = userService.findById(userId);
|
|
|
+ if (!userVO.getEmail().equals(user.getEmail()) && userService.findByEmail(userVO.getEmail()) != null)
|
|
|
+ throw new Exception("该邮箱已被注册");
|
|
|
+ user.setEmail(userVO.getEmail());
|
|
|
+ userVO = userVOWrapper.wrap(userService.update(user));
|
|
|
+ result.init(ResponseStatus.SUCCESS, "邮箱修改成功", userVO);
|
|
|
+ LOG.info(result.getMsg() + " userId: "+ userVO.getId() + " email: " + userVO.getEmail());
|
|
|
+ } catch (Exception e){
|
|
|
+ result.init(ResponseStatus.FAILED, e.getMessage(), userVO);
|
|
|
+ LOG.info(result.getMsg(), e);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseResult<UserVO> updateUserMobile(UserVO userVO, HttpServletRequest request) {
|
|
|
+ ResponseResult<UserVO> result = new ResponseResult<>();
|
|
|
+ Long userId = (Long) request.getSession().getAttribute("userId");
|
|
|
+ try {
|
|
|
+ if (userId == null)
|
|
|
+ throw new Exception("用户未登录");
|
|
|
+ if (!verifyCodeService.validateVerifyCode("mobile", userVO.getMobile(), userVO.getVerifyCode()))
|
|
|
+ throw new Exception("验证码或手机号填写错误");
|
|
|
+ User user = userService.findById(userId);
|
|
|
+ if (!userVO.getMobile().equals(user.getMobile()) && userService.findByMobile(userVO.getMobile()) != null)
|
|
|
+ throw new Exception("该手机号码已被绑定");
|
|
|
+ user.setMobile(userVO.getMobile());
|
|
|
+ userVO = userVOWrapper.wrap(userService.update(user));
|
|
|
+ result.init(ResponseStatus.SUCCESS, "手机号修改成功", userVO);
|
|
|
+ LOG.info(result.getMsg() + " userId: "+ userVO.getId() + " mobile: " + userVO.getMobile());
|
|
|
+ } catch (Exception e){
|
|
|
+ result.init(ResponseStatus.FAILED, e.getMessage(), userVO);
|
|
|
+ LOG.info(result.getMsg(), e);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|