SysRegisterService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package com.ruoyi.web.service;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.google.common.collect.Lists;
  5. import com.ruoyi.common.core.domain.model.LoginUser;
  6. import com.ruoyi.common.enums.UserRegStatus;
  7. import com.ruoyi.common.exception.base.BaseException;
  8. import com.ruoyi.common.utils.bean.BeanUtils;
  9. import com.ruoyi.dz.domain.DzCards;
  10. import com.ruoyi.dz.service.IDzCardsService;
  11. import com.ruoyi.enums.CardDistributeStatus;
  12. import com.ruoyi.enums.CardStatus;
  13. import com.ruoyi.enums.UserTypeEnum;
  14. import com.ruoyi.framework.web.service.TokenService;
  15. import com.ruoyi.learn.domain.LearnStudent;
  16. import com.ruoyi.learn.mapper.LearnStudentMapper;
  17. import com.ruoyi.system.mapper.SysUserMapper;
  18. import com.ruoyi.web.domain.CardUserBody;
  19. import org.apache.commons.collections4.CollectionUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Component;
  22. import com.ruoyi.common.constant.CacheConstants;
  23. import com.ruoyi.common.constant.UserConstants;
  24. import com.ruoyi.common.core.domain.entity.SysUser;
  25. import com.ruoyi.common.core.domain.model.RegisterBody;
  26. import com.ruoyi.common.core.redis.RedisCache;
  27. import com.ruoyi.common.exception.user.CaptchaException;
  28. import com.ruoyi.common.exception.user.CaptchaExpireException;
  29. import com.ruoyi.common.utils.DateUtils;
  30. import com.ruoyi.common.utils.SecurityUtils;
  31. import com.ruoyi.common.utils.StringUtils;
  32. import com.ruoyi.system.service.ISysConfigService;
  33. import com.ruoyi.system.service.ISysUserService;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import javax.validation.ValidationException;
  36. import java.text.ParseException;
  37. import java.util.List;
  38. /**
  39. * 注册校验方法
  40. *
  41. * @author ruoyi
  42. */
  43. @Component
  44. public class SysRegisterService
  45. {
  46. @Autowired
  47. private ISysUserService userService;
  48. @Autowired
  49. private ISysConfigService configService;
  50. @Autowired
  51. private RedisCache redisCache;
  52. @Autowired
  53. private IDzCardsService cardsService;
  54. @Autowired
  55. private TokenService tokenService;
  56. @Autowired
  57. private LearnStudentMapper learnStudentMapper;
  58. @Autowired
  59. private SysUserMapper sysUserMapper;
  60. @Autowired
  61. private LearnTeacherService learnTeacherService;
  62. /**
  63. * 注册
  64. */
  65. @Transactional(rollbackFor = Exception.class)
  66. public String register(RegisterBody registerBody, LoginUser loginUser)
  67. {
  68. String username = registerBody.getUsername(), mobile = registerBody.getMobile(), password = registerBody.getPassword();
  69. DzCards upCard = null;
  70. SysUser upUser = new SysUser();
  71. upUser.setPhonenumber(mobile);
  72. if(null == loginUser) { // 手机注册或卡注册
  73. if (StringUtils.isBlank(mobile)) {
  74. return "手机号不能为空";
  75. }
  76. if(StringUtils.isNotBlank(username)) { // 卡注册
  77. DzCards exist = cardsService.selectDzCardsByCardNo(username);
  78. if (StringUtils.isNull(exist)) {
  79. return "卡号密码不正确";
  80. }
  81. if(StringUtils.isBlank(password) || !password.equals(exist.getPassword())) {
  82. return "卡号密码不正确";
  83. }
  84. upCard = new DzCards();
  85. upCard.setCardId(exist.getCardId());
  86. upCard.setStatus(exist.getStatus());
  87. upCard.setDistributeStatus(exist.getDistributeStatus());
  88. upCard.setDays(exist.getDays());
  89. } else {
  90. username = mobile;
  91. password = "123456";
  92. registerBody.setUsername(username); // TODO 带回去重登陆
  93. registerBody.setPassword(password); // TODO 带回去重登陆
  94. }
  95. upUser.setPassword2(SecurityUtils.encryptPassword2(password));
  96. upUser.setPassword(SecurityUtils.encryptPassword(password));
  97. upUser.setPwdUpdateDate(DateUtils.getNowDate());
  98. } else {// 注册用户完善 不能改手机, 不能改邀请码
  99. DzCards exist = cardsService.selectDzCardsByCardNo(username);
  100. if(null == exist) {
  101. throw new BaseException("卡号不存在: " + username);
  102. }
  103. username = exist.getCardNo();
  104. upCard = new DzCards();
  105. upCard.setCardId(exist.getCardId());
  106. upCard.setStatus(exist.getStatus());
  107. upCard.setDistributeStatus(exist.getDistributeStatus());
  108. upCard.setDays(exist.getDays());
  109. upUser.setUserId(loginUser.getUserId());
  110. }
  111. upUser.setUserName(username);
  112. if (StringUtils.isEmpty(username)) {
  113. return "用户名不能为空";
  114. }
  115. else if (StringUtils.isEmpty(password))
  116. {
  117. return "用户密码不能为空";
  118. }
  119. else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  120. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  121. {
  122. return "账户长度必须在2到20个字符之间";
  123. }
  124. else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  125. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  126. {
  127. return "密码长度必须在5到20个字符之间";
  128. }
  129. else if (!userService.checkUserNameUnique(upUser))
  130. {
  131. return "保存用户'" + username + "'失败,注册卡号已存在";
  132. }
  133. else if (!userService.checkPhoneUnique(upUser))
  134. {
  135. return "保存用户'" + username + "'失败,注册手机已存在" + mobile;
  136. }
  137. saveInfo(upUser, upCard, registerBody); // 注册或完善后刷新登陆信息
  138. if(null != loginUser) {
  139. BeanUtils.copyProperties(userService.selectUserById(loginUser.getUserId()), loginUser.getUser(), "password");
  140. if(null != upCard.getCardId()) {
  141. BeanUtils.copyProperties(cardsService.selectDzCardsByCardId(upCard.getCardId()), loginUser.getCard());
  142. }
  143. tokenService.refreshToken(loginUser);
  144. }
  145. return "";
  146. }
  147. @Transactional(rollbackFor = Exception.class)
  148. public void improve(CardUserBody cardUserBody) {
  149. DzCards card = cardsService.selectDzCardsByCardId(cardUserBody.getCardId());
  150. if(null == card) {
  151. throw new ValidationException("卡用户不存在");
  152. }
  153. List<SysUser> sysUserList = sysUserMapper.selectUserByCardIds(Lists.newArrayList(card.getCardId()));
  154. if(CollectionUtils.isEmpty(sysUserList)) {
  155. throw new ValidationException("卡用户不存在");
  156. }
  157. Long userId = sysUserList.get(0).getUserId();
  158. SysUser upUser = new SysUser();
  159. upUser.setUserId(userId);
  160. upUser.setNickName(cardUserBody.getNickName());
  161. upUser.setLocation(cardUserBody.getLocation());
  162. upUser.setExamType(cardUserBody.getExamType());
  163. upUser.setEndYear(cardUserBody.getEndYear());
  164. upUser.setScores(cardUserBody.getScores());
  165. upUser.setSchoolId(cardUserBody.getSchoolId());
  166. upUser.setClassId(cardUserBody.getClassId());
  167. upUser.setPhonenumber(cardUserBody.getMobile());
  168. if (!userService.checkPhoneUnique(upUser)) {
  169. throw new ValidationException("保存用户'" + upUser.getNickName() + "'失败,注册手机已存在" + cardUserBody.getMobile());
  170. }
  171. JSONArray directList = cardUserBody.getDirectionStudy();
  172. if (null != directList && !directList.isEmpty()) {
  173. JSONObject directed = directList.getJSONObject(0);
  174. Long planId = directed.getLongValue("majorId");
  175. Long universityId = directed.getLongValue("universityId");
  176. directed.put("notice", learnTeacherService.updateDirected(userId, universityId, planId));
  177. cardUserBody.getDirectionStudy().set(0, directed);
  178. upUser.setDirectedStudy(cardUserBody.getDirectionStudy().toJSONString());
  179. }
  180. DzCards upCard = new DzCards();
  181. upCard.setCardId(card.getCardId());
  182. upCard.setSchoolId(cardUserBody.getSchoolId());
  183. upCard.setClassId(cardUserBody.getClassId());
  184. upCard.setCampusId(cardUserBody.getCampusSchoolId());
  185. upCard.setCampusClassId(cardUserBody.getCampusClassId());
  186. upCard.setOutDate(cardUserBody.getOutDate());
  187. userService.updateUserInfo(upUser);
  188. cardsService.updateDzCards(upCard);
  189. }
  190. private void saveInfo(SysUser user, DzCards card, RegisterBody register) {
  191. user.setNickName(register.getNickName());
  192. user.setLocation(register.getLocation());
  193. user.setExamType(register.getExamType());
  194. user.setEndYear(register.getEndYear());
  195. user.setScores(register.getScores());
  196. user.setSchoolId(register.getSchoolId());
  197. user.setClassId(register.getClassId());
  198. if(null == card) { // 手机注册时 无卡
  199. user.setInviteCode(register.getInviteCode()); //
  200. user.setRegStatus(UserRegStatus.User);
  201. } else {
  202. card.setEndYear(register.getEndYear());
  203. card.setYear(register.getEndYear() - 3);
  204. card.setSchoolId(register.getSchoolId());
  205. card.setClassId(register.getClassId());
  206. if(null == user.getCardId() || !user.getCardId().equals(card.getCardId())) { // 未绑定或换绑时激活卡
  207. if(!CardDistributeStatus.Assign.getVal().equals(card.getDistributeStatus()) || !CardStatus.Free.getVal().equals(card.getStatus())) {
  208. throw new RuntimeException("无效卡");
  209. }
  210. user.setEvalCounts("{\"1\":1,\"2\":1,\"3\":1,\"11\":1,\"0\":0}");
  211. user.setCardId(card.getCardId());
  212. user.setRegStatus(UserRegStatus.Student);
  213. card.setStatus(CardStatus.Active.getVal());
  214. card.setActiveTime(DateUtils.getNowDate());
  215. if(null != card.getDays()) {
  216. card.setOutDate(DateUtils.addDays(card.getActiveTime(), card.getDays()));
  217. }
  218. }
  219. try {
  220. if(null == card.getDays()) {
  221. card.setOutDate(DateUtils.parseDate(register.getEndYear() + "-08-30", "yyyy-MM-dd"));
  222. }
  223. card.setDays(null);
  224. } catch (ParseException e) {
  225. throw new RuntimeException("saveInfo 日期错误", e);
  226. }
  227. }
  228. if(null == user.getUserId()) {
  229. user.setUserType(UserTypeEnum.Card.getVal());
  230. userService.insertUser(user);
  231. LearnStudent upStudent = new LearnStudent();
  232. upStudent.setStudentId(user.getUserId());
  233. upStudent.setClassId(user.getClassId());
  234. learnStudentMapper.insertLearnStudent(upStudent);
  235. } else {
  236. LearnStudent upStudent = new LearnStudent();
  237. upStudent.setStudentId(user.getUserId());
  238. upStudent.setClassId(user.getClassId());
  239. learnStudentMapper.updateLearnStudent(upStudent);
  240. userService.updateUser(user);
  241. }
  242. if(null != card) {
  243. cardsService.updateDzCards(card);
  244. }
  245. }
  246. /**
  247. * 校验验证码
  248. *
  249. * @param username 用户名
  250. * @param code 验证码
  251. * @param uuid 唯一标识
  252. * @return 结果
  253. */
  254. public void validateCaptcha(String username, String code, String uuid)
  255. {
  256. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
  257. String captcha = redisCache.getCacheObject(verifyKey);
  258. redisCache.deleteObject(verifyKey);
  259. if (captcha == null)
  260. {
  261. throw new CaptchaExpireException();
  262. }
  263. if (!code.equalsIgnoreCase(captcha))
  264. {
  265. throw new CaptchaException();
  266. }
  267. }
  268. public void validateSmsCaptcha(String mobile, String code, String uuid) {
  269. }
  270. }