SysRegisterService.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package com.ruoyi.web.service;
  2. import com.ruoyi.common.core.domain.AjaxResult;
  3. import com.ruoyi.common.core.domain.model.LoginCard;
  4. import com.ruoyi.common.core.domain.model.LoginUser;
  5. import com.ruoyi.common.enums.UserRegStatus;
  6. import com.ruoyi.common.exception.base.BaseException;
  7. import com.ruoyi.common.utils.bean.BeanUtils;
  8. import com.ruoyi.dz.domain.DzCards;
  9. import com.ruoyi.dz.service.IDzCardsService;
  10. import com.ruoyi.enums.CardStatus;
  11. import com.ruoyi.enums.UserTypeEnum;
  12. import com.ruoyi.framework.web.service.TokenService;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Component;
  15. import com.ruoyi.common.constant.CacheConstants;
  16. import com.ruoyi.common.constant.UserConstants;
  17. import com.ruoyi.common.core.domain.entity.SysUser;
  18. import com.ruoyi.common.core.domain.model.RegisterBody;
  19. import com.ruoyi.common.core.redis.RedisCache;
  20. import com.ruoyi.common.exception.user.CaptchaException;
  21. import com.ruoyi.common.exception.user.CaptchaExpireException;
  22. import com.ruoyi.common.utils.DateUtils;
  23. import com.ruoyi.common.utils.SecurityUtils;
  24. import com.ruoyi.common.utils.StringUtils;
  25. import com.ruoyi.system.service.ISysConfigService;
  26. import com.ruoyi.system.service.ISysUserService;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import java.text.ParseException;
  29. /**
  30. * 注册校验方法
  31. *
  32. * @author ruoyi
  33. */
  34. @Component
  35. public class SysRegisterService
  36. {
  37. @Autowired
  38. private ISysUserService userService;
  39. @Autowired
  40. private ISysConfigService configService;
  41. @Autowired
  42. private RedisCache redisCache;
  43. @Autowired
  44. private IDzCardsService cardsService;
  45. @Autowired
  46. private TokenService tokenService;
  47. /**
  48. * 注册
  49. */
  50. @Transactional(rollbackFor = Exception.class)
  51. public String register(RegisterBody registerBody, LoginUser loginUser)
  52. {
  53. String username = registerBody.getUsername(), mobile = registerBody.getMobile(), password = registerBody.getPassword();
  54. DzCards upCard = null;
  55. SysUser upUser = new SysUser();
  56. upUser.setPhonenumber(mobile);
  57. if(null == loginUser) { // 手机注册或卡注册
  58. if (StringUtils.isBlank(mobile)) {
  59. return "手机号不能为空";
  60. }
  61. if(StringUtils.isNotBlank(username)) { // 卡注册
  62. DzCards exist = cardsService.selectDzCardsByCardNo(username);
  63. if (StringUtils.isNull(exist)) {
  64. return "卡号不正确";
  65. }
  66. upCard = new DzCards();
  67. upCard.setCardId(exist.getCardId());
  68. } else {
  69. username = mobile;
  70. password = "123456";
  71. registerBody.setUsername(username); // TODO 带回去重登陆
  72. registerBody.setPassword(password); // TODO 带回去重登陆
  73. }
  74. upUser.setPassword2(SecurityUtils.encryptPassword2(password));
  75. upUser.setPassword(SecurityUtils.encryptPassword(password));
  76. upUser.setPwdUpdateDate(DateUtils.getNowDate());
  77. } else {// 注册用户完善 不能改手机, 不能改邀请码
  78. DzCards exist = cardsService.selectDzCardsByCardNo(username);
  79. if(null == exist) {
  80. throw new BaseException("卡号不存在: " + username);
  81. }
  82. username = exist.getCardNo();
  83. upCard = new DzCards();
  84. upCard.setCardId(exist.getCardId());
  85. upUser.setUserId(loginUser.getUserId());
  86. }
  87. upUser.setUserName(username);
  88. if (StringUtils.isEmpty(username)) {
  89. return "用户名不能为空";
  90. }
  91. else if (StringUtils.isEmpty(password))
  92. {
  93. return "用户密码不能为空";
  94. }
  95. else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  96. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  97. {
  98. return "账户长度必须在2到20个字符之间";
  99. }
  100. else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  101. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  102. {
  103. return "密码长度必须在5到20个字符之间";
  104. }
  105. else if (!userService.checkUserNameUnique(upUser))
  106. {
  107. return "保存用户'" + username + "'失败,注册卡号已存在";
  108. }
  109. else if (!userService.checkPhoneUnique(upUser))
  110. {
  111. return "保存用户'" + username + "'失败,注册手机已存在" + mobile;
  112. }
  113. saveInfo(upUser, upCard, registerBody); // 注册或完善后刷新登陆信息
  114. if(null != loginUser) {
  115. BeanUtils.copyProperties(userService.selectUserById(loginUser.getUserId()), loginUser.getUser(), "password");
  116. if(null != upCard.getCardId()) {
  117. BeanUtils.copyProperties(cardsService.selectDzCardsByCardId(upCard.getCardId()), loginUser.getCard());
  118. }
  119. tokenService.refreshToken(loginUser);
  120. }
  121. return "";
  122. }
  123. private void saveInfo(SysUser user, DzCards card, RegisterBody register) {
  124. user.setNickName(register.getNickName());
  125. user.setLocation(register.getLocation());
  126. user.setExamType(register.getExamType());
  127. user.setEndYear(register.getEndYear());
  128. user.setScores(register.getScores());
  129. if(null == card) { // 手机注册时 无卡
  130. user.setInviteCode(register.getInviteCode()); //
  131. user.setRegStatus(UserRegStatus.User);
  132. } else {
  133. card.setEndYear(register.getEndYear());
  134. try {
  135. card.setOutDate(DateUtils.parseDate(register.getEndYear() + "-08-30", "yyyy-MM-dd"));
  136. } catch (ParseException e) {
  137. throw new RuntimeException("saveInfo 日期错误", e);
  138. }
  139. card.setYear(register.getEndYear() - 3);
  140. card.setSchoolId(register.getSchoolId());
  141. card.setClassId(register.getClassId());
  142. if(null == user.getCardId() || !user.getCardId().equals(card.getCardId())) { // 未绑定或换绑时激活卡
  143. if(!CardStatus.Paid.getVal().equals(card.getStatus())) {
  144. throw new RuntimeException("无效卡");
  145. }
  146. user.setCardId(card.getCardId());
  147. user.setRegStatus(UserRegStatus.Student);
  148. card.setStatus(CardStatus.Active.getVal());
  149. card.setActiveTime(DateUtils.getNowDate());
  150. }
  151. }
  152. if(null == user.getUserId()) {
  153. user.setUserType(UserTypeEnum.Card.getVal());
  154. userService.insertUser(user);
  155. } else {
  156. userService.updateUser(user);
  157. }
  158. if(null != card) {
  159. cardsService.updateDzCards(card);
  160. }
  161. }
  162. /**
  163. * 校验验证码
  164. *
  165. * @param username 用户名
  166. * @param code 验证码
  167. * @param uuid 唯一标识
  168. * @return 结果
  169. */
  170. public void validateCaptcha(String username, String code, String uuid)
  171. {
  172. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
  173. String captcha = redisCache.getCacheObject(verifyKey);
  174. redisCache.deleteObject(verifyKey);
  175. if (captcha == null)
  176. {
  177. throw new CaptchaExpireException();
  178. }
  179. if (!code.equalsIgnoreCase(captcha))
  180. {
  181. throw new CaptchaException();
  182. }
  183. }
  184. public void validateSmsCaptcha(String mobile, String code, String uuid) {
  185. }
  186. }