SysRegisterService.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 lombok.RequiredArgsConstructor;
  20. import org.springframework.stereotype.Service;
  21. /**
  22. * 注册校验方法
  23. *
  24. * @author Lion Li
  25. */
  26. @RequiredArgsConstructor
  27. @Service
  28. public class SysRegisterService {
  29. private final ISysUserService userService;
  30. private final CaptchaProperties captchaProperties;
  31. /**
  32. * 注册
  33. */
  34. public void register(RegisterBody registerBody) {
  35. String tenantId = registerBody.getTenantId();
  36. String username = registerBody.getUsername();
  37. String password = registerBody.getPassword();
  38. // 校验用户类型是否存在
  39. String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
  40. boolean captchaEnabled = captchaProperties.getEnable();
  41. // 验证码开关
  42. if (captchaEnabled) {
  43. validateCaptcha(tenantId, username, registerBody.getCode(), registerBody.getUuid());
  44. }
  45. SysUserBo sysUser = new SysUserBo();
  46. sysUser.setUserName(username);
  47. sysUser.setNickName(username);
  48. sysUser.setPassword(BCrypt.hashpw(password));
  49. sysUser.setUserType(userType);
  50. if (!userService.checkUserNameUnique(sysUser)) {
  51. throw new UserException("user.register.save.error", username);
  52. }
  53. boolean regFlag = userService.registerUser(sysUser, tenantId);
  54. if (!regFlag) {
  55. throw new UserException("user.register.error");
  56. }
  57. recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.register.success"));
  58. }
  59. /**
  60. * 校验验证码
  61. *
  62. * @param username 用户名
  63. * @param code 验证码
  64. * @param uuid 唯一标识
  65. */
  66. public void validateCaptcha(String tenantId, String username, String code, String uuid) {
  67. String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
  68. String captcha = RedisUtils.getCacheObject(verifyKey);
  69. RedisUtils.deleteObject(verifyKey);
  70. if (captcha == null) {
  71. recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire"));
  72. throw new CaptchaExpireException();
  73. }
  74. if (!code.equalsIgnoreCase(captcha)) {
  75. recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error"));
  76. throw new CaptchaException();
  77. }
  78. }
  79. /**
  80. * 记录登录信息
  81. *
  82. * @param tenantId 租户ID
  83. * @param username 用户名
  84. * @param status 状态
  85. * @param message 消息内容
  86. * @return
  87. */
  88. private void recordLogininfor(String tenantId, String username, String status, String message) {
  89. LogininforEvent logininforEvent = new LogininforEvent();
  90. logininforEvent.setTenantId(tenantId);
  91. logininforEvent.setUsername(username);
  92. logininforEvent.setStatus(status);
  93. logininforEvent.setMessage(message);
  94. logininforEvent.setRequest(ServletUtils.getRequest());
  95. SpringUtils.context().publishEvent(logininforEvent);
  96. }
  97. }