SysRegisterService.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.ruoyi.system.service;
  2. import com.ruoyi.common.constant.Constants;
  3. import com.ruoyi.common.constant.UserConstants;
  4. import com.ruoyi.common.core.domain.entity.SysUser;
  5. import com.ruoyi.common.core.domain.model.RegisterBody;
  6. import com.ruoyi.common.core.service.LogininforService;
  7. import com.ruoyi.common.enums.UserType;
  8. import com.ruoyi.common.exception.user.CaptchaException;
  9. import com.ruoyi.common.exception.user.CaptchaExpireException;
  10. import com.ruoyi.common.utils.MessageUtils;
  11. import com.ruoyi.common.utils.SecurityUtils;
  12. import com.ruoyi.common.utils.ServletUtils;
  13. import com.ruoyi.common.utils.StringUtils;
  14. import com.ruoyi.common.utils.redis.RedisUtils;
  15. import lombok.RequiredArgsConstructor;
  16. import org.springframework.stereotype.Service;
  17. /**
  18. * 注册校验方法
  19. *
  20. * @author Lion Li
  21. */
  22. @RequiredArgsConstructor
  23. @Service
  24. public class SysRegisterService {
  25. private final ISysUserService userService;
  26. private final ISysConfigService configService;
  27. private final LogininforService asyncService;
  28. /**
  29. * 注册
  30. */
  31. public String register(RegisterBody registerBody) {
  32. String msg = "";
  33. String username = registerBody.getUsername();
  34. String password = registerBody.getPassword();
  35. // 校验用户类型是否存在
  36. String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
  37. boolean captchaOnOff = configService.selectCaptchaOnOff();
  38. // 验证码开关
  39. if (captchaOnOff) {
  40. validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
  41. }
  42. if (StringUtils.isEmpty(username)) {
  43. msg = "用户名不能为空";
  44. } else if (StringUtils.isEmpty(password)) {
  45. msg = "用户密码不能为空";
  46. } else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  47. || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
  48. msg = "账户长度必须在2到20个字符之间";
  49. } else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  50. || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
  51. msg = "密码长度必须在5到20个字符之间";
  52. } else if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) {
  53. msg = "保存用户'" + username + "'失败,注册账号已存在";
  54. } else {
  55. SysUser sysUser = new SysUser();
  56. sysUser.setUserName(username);
  57. sysUser.setNickName(username);
  58. sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword()));
  59. sysUser.setUserType(userType);
  60. boolean regFlag = userService.registerUser(sysUser);
  61. if (!regFlag) {
  62. msg = "注册失败,请联系系统管理人员";
  63. } else {
  64. asyncService.recordLogininfor(username, Constants.REGISTER,
  65. MessageUtils.message("user.register.success"), ServletUtils.getRequest());
  66. }
  67. }
  68. return msg;
  69. }
  70. /**
  71. * 校验验证码
  72. *
  73. * @param username 用户名
  74. * @param code 验证码
  75. * @param uuid 唯一标识
  76. * @return 结果
  77. */
  78. public void validateCaptcha(String username, String code, String uuid) {
  79. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  80. String captcha = RedisUtils.getCacheObject(verifyKey);
  81. RedisUtils.deleteObject(verifyKey);
  82. if (captcha == null) {
  83. throw new CaptchaExpireException();
  84. }
  85. if (!code.equalsIgnoreCase(captcha)) {
  86. throw new CaptchaException();
  87. }
  88. }
  89. }