SysRegisterService.java 3.6 KB

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