|
@@ -1,6 +1,7 @@
|
|
package com.ruoyi.framework.web.service;
|
|
package com.ruoyi.framework.web.service;
|
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
+import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import com.ruoyi.common.constant.CacheConstants;
|
|
import com.ruoyi.common.constant.CacheConstants;
|
|
@@ -19,16 +20,16 @@ import com.ruoyi.framework.manager.factory.AsyncFactory;
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
|
|
-import static com.ruoyi.common.constant.CommonConstants.ONE;
|
|
|
|
|
|
+import static com.ruoyi.common.constant.CommonConstants.*;
|
|
|
|
+import static com.ruoyi.common.constant.CommonConstants.LOGIN_USER_SMS;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 注册校验方法
|
|
* 注册校验方法
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @author ruoyi
|
|
* @author ruoyi
|
|
*/
|
|
*/
|
|
@Component
|
|
@Component
|
|
-public class SysRegisterService
|
|
|
|
-{
|
|
|
|
|
|
+public class SysRegisterService {
|
|
@Autowired
|
|
@Autowired
|
|
private ISysUserService userService;
|
|
private ISysUserService userService;
|
|
|
|
|
|
@@ -41,77 +42,85 @@ public class SysRegisterService
|
|
/**
|
|
/**
|
|
* 注册
|
|
* 注册
|
|
*/
|
|
*/
|
|
- public String register(RegisterBody registerBody)
|
|
|
|
- {
|
|
|
|
|
|
+ public String register(RegisterBody registerBody) {
|
|
String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword();
|
|
String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword();
|
|
SysUser sysUser = new SysUser();
|
|
SysUser sysUser = new SysUser();
|
|
sysUser.setUserName(username);
|
|
sysUser.setUserName(username);
|
|
|
|
|
|
// 验证码开关
|
|
// 验证码开关
|
|
boolean captchaEnabled = configService.selectCaptchaEnabled();
|
|
boolean captchaEnabled = configService.selectCaptchaEnabled();
|
|
- if (captchaEnabled)
|
|
|
|
- {
|
|
|
|
|
|
+ if (captchaEnabled) {
|
|
validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
|
|
validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
|
|
}
|
|
}
|
|
|
|
|
|
- if (StringUtils.isEmpty(username))
|
|
|
|
- {
|
|
|
|
|
|
+ if (StringUtils.isEmpty(username)) {
|
|
msg = "用户名不能为空";
|
|
msg = "用户名不能为空";
|
|
- }
|
|
|
|
- else if (StringUtils.isEmpty(password))
|
|
|
|
- {
|
|
|
|
|
|
+ } else if (StringUtils.isEmpty(password)) {
|
|
msg = "用户密码不能为空";
|
|
msg = "用户密码不能为空";
|
|
}
|
|
}
|
|
sysUser.setPassword(password);
|
|
sysUser.setPassword(password);
|
|
if (ONE.equals(userService.checkStrongPwd(sysUser))) {
|
|
if (ONE.equals(userService.checkStrongPwd(sysUser))) {
|
|
msg = "密码必须包含数字、大小写字母、特殊符号且大于8位";
|
|
msg = "密码必须包含数字、大小写字母、特殊符号且大于8位";
|
|
- }
|
|
|
|
- else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
|
|
|
- || username.length() > UserConstants.USERNAME_MAX_LENGTH)
|
|
|
|
- {
|
|
|
|
|
|
+ } else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
|
|
|
+ || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
|
msg = "账户长度必须在2到20个字符之间";
|
|
msg = "账户长度必须在2到20个字符之间";
|
|
- }
|
|
|
|
- else if (!userService.checkUserNameUnique(sysUser))
|
|
|
|
- {
|
|
|
|
|
|
+ } else if (!userService.checkUserNameUnique(sysUser)) {
|
|
msg = "保存用户'" + username + "'失败,注册账号已存在";
|
|
msg = "保存用户'" + username + "'失败,注册账号已存在";
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- sysUser.setNickName(username);
|
|
|
|
- sysUser.setPassword(SecurityUtils.encryptPassword(password));
|
|
|
|
- boolean regFlag = userService.registerUser(sysUser);
|
|
|
|
- if (!regFlag)
|
|
|
|
- {
|
|
|
|
- msg = "注册失败,请联系系统管理人员";
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
|
|
|
|
|
|
+ } else {
|
|
|
|
+ if(validateType(username,registerBody.getCode())){
|
|
|
|
+ sysUser.setNickName(username);
|
|
|
|
+ sysUser.setPassword(SecurityUtils.encryptPassword(password));
|
|
|
|
+ boolean regFlag = userService.registerUser(sysUser);
|
|
|
|
+ if (!regFlag) {
|
|
|
|
+ msg = "注册失败,请联系系统管理人员";
|
|
|
|
+ } else {
|
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ msg = "注册失败,验证码错误";
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
return msg;
|
|
return msg;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 校验验证码
|
|
* 校验验证码
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param username 用户名
|
|
* @param username 用户名
|
|
- * @param code 验证码
|
|
|
|
- * @param uuid 唯一标识
|
|
|
|
|
|
+ * @param code 验证码
|
|
|
|
+ * @param uuid 唯一标识
|
|
* @return 结果
|
|
* @return 结果
|
|
*/
|
|
*/
|
|
- public void validateCaptcha(String username, String code, String uuid)
|
|
|
|
- {
|
|
|
|
|
|
+ public void validateCaptcha(String username, String code, String uuid) {
|
|
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
|
|
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
|
|
String captcha = redisCache.getCacheObject(verifyKey);
|
|
String captcha = redisCache.getCacheObject(verifyKey);
|
|
redisCache.deleteObject(verifyKey);
|
|
redisCache.deleteObject(verifyKey);
|
|
- if (captcha == null)
|
|
|
|
- {
|
|
|
|
|
|
+ if (captcha == null) {
|
|
throw new CaptchaExpireException();
|
|
throw new CaptchaExpireException();
|
|
}
|
|
}
|
|
- if (!code.equalsIgnoreCase(captcha))
|
|
|
|
- {
|
|
|
|
|
|
+ if (!code.equalsIgnoreCase(captcha)) {
|
|
throw new CaptchaException();
|
|
throw new CaptchaException();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 注册短信验证码验证
|
|
|
|
+ *
|
|
|
|
+ * @param username 用户名
|
|
|
|
+ * @param code 短信验证码
|
|
|
|
+ */
|
|
|
|
+ public Boolean validateType(String username, String code) {
|
|
|
|
+ boolean bl = false;
|
|
|
|
+ //校验短信验证码
|
|
|
|
+ Object cacheObject = redisCache.getCacheObject(LOGIN_USER_SMS + username);
|
|
|
|
+ if (code.equals(cacheObject)) {
|
|
|
|
+ bl = true;
|
|
|
|
+ redisCache.deleteObject(LOGIN_USER_SMS + username);
|
|
|
|
+ } else {
|
|
|
|
+ throw new CaptchaExpireException();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return bl;
|
|
|
|
+ }
|
|
}
|
|
}
|