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