SysRegisterService.java 3.4 KB

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