| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package com.ruoyi.web.service;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.core.domain.model.LoginCard;
- import com.ruoyi.common.core.domain.model.LoginUser;
- import com.ruoyi.common.enums.UserRegStatus;
- import com.ruoyi.common.exception.base.BaseException;
- import com.ruoyi.common.utils.bean.BeanUtils;
- import com.ruoyi.dz.domain.DzCards;
- import com.ruoyi.dz.service.IDzCardsService;
- import com.ruoyi.enums.CardStatus;
- import com.ruoyi.enums.UserTypeEnum;
- import com.ruoyi.framework.web.service.TokenService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import com.ruoyi.common.constant.CacheConstants;
- import com.ruoyi.common.constant.UserConstants;
- import com.ruoyi.common.core.domain.entity.SysUser;
- import com.ruoyi.common.core.domain.model.RegisterBody;
- import com.ruoyi.common.core.redis.RedisCache;
- import com.ruoyi.common.exception.user.CaptchaException;
- import com.ruoyi.common.exception.user.CaptchaExpireException;
- import com.ruoyi.common.utils.DateUtils;
- import com.ruoyi.common.utils.SecurityUtils;
- import com.ruoyi.common.utils.StringUtils;
- import com.ruoyi.system.service.ISysConfigService;
- import com.ruoyi.system.service.ISysUserService;
- import org.springframework.transaction.annotation.Transactional;
- import java.text.ParseException;
- /**
- * 注册校验方法
- *
- * @author ruoyi
- */
- @Component
- public class SysRegisterService
- {
- @Autowired
- private ISysUserService userService;
- @Autowired
- private ISysConfigService configService;
- @Autowired
- private RedisCache redisCache;
- @Autowired
- private IDzCardsService cardsService;
- @Autowired
- private TokenService tokenService;
- /**
- * 注册
- */
- @Transactional(rollbackFor = Exception.class)
- public String register(RegisterBody registerBody, LoginUser loginUser)
- {
- String username = registerBody.getUsername(), mobile = registerBody.getMobile(), password = registerBody.getPassword();
- DzCards upCard = null;
- SysUser upUser = new SysUser();
- upUser.setPhonenumber(mobile);
- if(null == loginUser) { // 手机注册或卡注册
- if (StringUtils.isBlank(mobile)) {
- return "手机号不能为空";
- }
- if(StringUtils.isNotBlank(username)) { // 卡注册
- DzCards exist = cardsService.selectDzCardsByCardNo(username);
- if (StringUtils.isNull(exist)) {
- return "卡号不正确";
- }
- upCard = new DzCards();
- upCard.setCardId(exist.getCardId());
- } else {
- username = mobile;
- password = "123456";
- registerBody.setUsername(username); // TODO 带回去重登陆
- registerBody.setPassword(password); // TODO 带回去重登陆
- }
- upUser.setPassword2(SecurityUtils.encryptPassword2(password));
- upUser.setPassword(SecurityUtils.encryptPassword(password));
- upUser.setPwdUpdateDate(DateUtils.getNowDate());
- } else {// 注册用户完善 不能改手机, 不能改邀请码
- DzCards exist = cardsService.selectDzCardsByCardNo(username);
- if(null == exist) {
- throw new BaseException("卡号不存在: " + username);
- }
- username = exist.getCardNo();
- upCard = new DzCards();
- upCard.setCardId(exist.getCardId());
- upUser.setUserId(loginUser.getUserId());
- }
- upUser.setUserName(username);
- if (StringUtils.isEmpty(username)) {
- return "用户名不能为空";
- }
- else if (StringUtils.isEmpty(password))
- {
- return "用户密码不能为空";
- }
- else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
- || username.length() > UserConstants.USERNAME_MAX_LENGTH)
- {
- return "账户长度必须在2到20个字符之间";
- }
- else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
- || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
- {
- return "密码长度必须在5到20个字符之间";
- }
- else if (!userService.checkUserNameUnique(upUser))
- {
- return "保存用户'" + username + "'失败,注册卡号已存在";
- }
- else if (!userService.checkPhoneUnique(upUser))
- {
- return "保存用户'" + username + "'失败,注册手机已存在" + mobile;
- }
- saveInfo(upUser, upCard, registerBody); // 注册或完善后刷新登陆信息
- if(null != loginUser) {
- BeanUtils.copyProperties(userService.selectUserById(loginUser.getUserId()), loginUser.getUser(), "password");
- if(null != upCard.getCardId()) {
- BeanUtils.copyProperties(cardsService.selectDzCardsByCardId(upCard.getCardId()), loginUser.getCard());
- }
- tokenService.refreshToken(loginUser);
- }
- return "";
- }
- private void saveInfo(SysUser user, DzCards card, RegisterBody register) {
- user.setNickName(register.getNickName());
- user.setLocation(register.getLocation());
- user.setExamType(register.getExamType());
- user.setEndYear(register.getEndYear());
- user.setScores(register.getScores());
- if(null == card) { // 手机注册时 无卡
- user.setInviteCode(register.getInviteCode()); //
- user.setRegStatus(UserRegStatus.User);
- } else {
- card.setEndYear(register.getEndYear());
- try {
- card.setOutDate(DateUtils.parseDate(register.getEndYear() + "-08-30", "yyyy-MM-dd"));
- } catch (ParseException e) {
- throw new RuntimeException("saveInfo 日期错误", e);
- }
- card.setYear(register.getEndYear() - 3);
- card.setSchoolId(register.getSchoolId());
- card.setClassId(register.getClassId());
- if(null == user.getCardId() || !user.getCardId().equals(card.getCardId())) { // 未绑定或换绑时激活卡
- if(!CardStatus.Paid.getVal().equals(card.getStatus())) {
- throw new RuntimeException("无效卡");
- }
- user.setCardId(card.getCardId());
- user.setRegStatus(UserRegStatus.Student);
- card.setStatus(CardStatus.Active.getVal());
- card.setActiveTime(DateUtils.getNowDate());
- }
- }
- if(null == user.getUserId()) {
- user.setUserType(UserTypeEnum.Card.getVal());
- userService.insertUser(user);
- } else {
- userService.updateUser(user);
- }
- if(null != card) {
- cardsService.updateDzCards(card);
- }
- }
- /**
- * 校验验证码
- *
- * @param username 用户名
- * @param code 验证码
- * @param uuid 唯一标识
- * @return 结果
- */
- public void validateCaptcha(String username, String code, String uuid)
- {
- String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
- String captcha = redisCache.getCacheObject(verifyKey);
- redisCache.deleteObject(verifyKey);
- if (captcha == null)
- {
- throw new CaptchaExpireException();
- }
- if (!code.equalsIgnoreCase(captcha))
- {
- throw new CaptchaException();
- }
- }
- public void validateSmsCaptcha(String mobile, String code, String uuid) {
- }
- }
|