SysRegisterService.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.ruoyi.web.service;
  2. import cn.dev33.satoken.secure.BCrypt;
  3. import com.ruoyi.common.core.constant.Constants;
  4. import com.ruoyi.common.core.constant.GlobalConstants;
  5. import com.ruoyi.common.core.domain.model.RegisterBody;
  6. import com.ruoyi.common.core.enums.UserType;
  7. import com.ruoyi.common.core.exception.user.CaptchaException;
  8. import com.ruoyi.common.core.exception.user.CaptchaExpireException;
  9. import com.ruoyi.common.core.exception.user.UserException;
  10. import com.ruoyi.common.core.utils.MessageUtils;
  11. import com.ruoyi.common.core.utils.ServletUtils;
  12. import com.ruoyi.common.core.utils.SpringUtils;
  13. import com.ruoyi.common.core.utils.StringUtils;
  14. import com.ruoyi.common.log.event.LogininforEvent;
  15. import com.ruoyi.common.redis.utils.RedisUtils;
  16. import com.ruoyi.common.web.config.properties.CaptchaProperties;
  17. import com.ruoyi.system.domain.bo.SysUserBo;
  18. import com.ruoyi.system.service.ISysUserService;
  19. import jakarta.servlet.http.HttpServletRequest;
  20. import lombok.RequiredArgsConstructor;
  21. import org.springframework.stereotype.Service;
  22. /**
  23. * 注册校验方法
  24. *
  25. * @author Lion Li
  26. */
  27. @RequiredArgsConstructor
  28. @Service
  29. public class SysRegisterService {
  30. private final ISysUserService userService;
  31. private final CaptchaProperties captchaProperties;
  32. /**
  33. * 注册
  34. */
  35. public void register(RegisterBody registerBody) {
  36. HttpServletRequest request = ServletUtils.getRequest();
  37. String tenantId = registerBody.getTenantId();
  38. String username = registerBody.getUsername();
  39. String password = registerBody.getPassword();
  40. // 校验用户类型是否存在
  41. String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
  42. boolean captchaEnabled = captchaProperties.getEnable();
  43. // 验证码开关
  44. if (captchaEnabled) {
  45. validateCaptcha(tenantId, username, registerBody.getCode(), registerBody.getUuid(), request);
  46. }
  47. SysUserBo sysUser = new SysUserBo();
  48. sysUser.setUserName(username);
  49. sysUser.setNickName(username);
  50. sysUser.setPassword(BCrypt.hashpw(password));
  51. sysUser.setUserType(userType);
  52. if (!userService.checkUserNameUnique(sysUser)) {
  53. throw new UserException("user.register.save.error", username);
  54. }
  55. boolean regFlag = userService.registerUser(sysUser, tenantId);
  56. if (!regFlag) {
  57. throw new UserException("user.register.error");
  58. }
  59. recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.register.success"));
  60. }
  61. /**
  62. * 校验验证码
  63. *
  64. * @param username 用户名
  65. * @param code 验证码
  66. * @param uuid 唯一标识
  67. * @return 结果
  68. */
  69. public void validateCaptcha(String tenantId, String username, String code, String uuid, HttpServletRequest request) {
  70. String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
  71. String captcha = RedisUtils.getCacheObject(verifyKey);
  72. RedisUtils.deleteObject(verifyKey);
  73. if (captcha == null) {
  74. recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire"));
  75. throw new CaptchaExpireException();
  76. }
  77. if (!code.equalsIgnoreCase(captcha)) {
  78. recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error"));
  79. throw new CaptchaException();
  80. }
  81. }
  82. /**
  83. * 记录登录信息
  84. *
  85. * @param tenantId 租户ID
  86. * @param username 用户名
  87. * @param status 状态
  88. * @param message 消息内容
  89. * @return
  90. */
  91. private void recordLogininfor(String tenantId, String username, String status, String message) {
  92. LogininforEvent logininforEvent = new LogininforEvent();
  93. logininforEvent.setTenantId(tenantId);
  94. logininforEvent.setUsername(username);
  95. logininforEvent.setStatus(status);
  96. logininforEvent.setMessage(message);
  97. logininforEvent.setRequest(ServletUtils.getRequest());
  98. SpringUtils.context().publishEvent(logininforEvent);
  99. }
  100. }