package com.ruoyi.web.service; 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.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; /** * 注册校验方法 * * @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()); card.setYear(register.getEndYear() - 3); card.setSchoolId(register.getSchoolId()); card.setClassId(register.getClassId()); if(null == user.getCardId() || !user.getCardId().equals(card.getCardId())) { // 未绑定或换绑时激活卡 user.setCardId(card.getCardId()); user.setRegStatus(UserRegStatus.Student); card.setActiveTime(DateUtils.getNowDate()); } } if(null == user.getUserId()) { 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) { } }