¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.web.service; |
| | | |
| | | import com.ruoyi.common.constant.Constants; |
| | | 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.MessageUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.ServletUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * æ³¨åæ ¡éªæ¹æ³ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Component |
| | | public class SysRegisterService |
| | | { |
| | | @Autowired |
| | | private ISysUserService userService; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | |
| | | @Autowired |
| | | private AsyncService asyncService; |
| | | |
| | | /** |
| | | * 注å |
| | | */ |
| | | public String register(RegisterBody registerBody) |
| | | { |
| | | String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword(); |
| | | |
| | | boolean captchaOnOff = configService.selectCaptchaOnOff(); |
| | | // éªè¯ç å¼å
³ |
| | | if (captchaOnOff) |
| | | { |
| | | validateCaptcha(username, registerBody.getCode(), registerBody.getUuid()); |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(username)) |
| | | { |
| | | msg = "ç¨æ·åä¸è½ä¸ºç©º"; |
| | | } |
| | | else if (StringUtils.isEmpty(password)) |
| | | { |
| | | msg = "ç¨æ·å¯ç ä¸è½ä¸ºç©º"; |
| | | } |
| | | else if (username.length() < UserConstants.USERNAME_MIN_LENGTH |
| | | || username.length() > UserConstants.USERNAME_MAX_LENGTH) |
| | | { |
| | | msg = "è´¦æ·é¿åº¦å¿
é¡»å¨2å°20个å符ä¹é´"; |
| | | } |
| | | else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH |
| | | || password.length() > UserConstants.PASSWORD_MAX_LENGTH) |
| | | { |
| | | msg = "å¯ç é¿åº¦å¿
é¡»å¨5å°20个å符ä¹é´"; |
| | | } |
| | | else if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) |
| | | { |
| | | msg = "ä¿åç¨æ·'" + username + "'å¤±è´¥ï¼æ³¨åè´¦å·å·²åå¨"; |
| | | } |
| | | else |
| | | { |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setUserName(username); |
| | | sysUser.setNickName(username); |
| | | sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword())); |
| | | boolean regFlag = userService.registerUser(sysUser); |
| | | if (!regFlag) |
| | | { |
| | | msg = "注å失败,请è系系ç»ç®¡ç人å"; |
| | | } |
| | | else |
| | | { |
| | | asyncService.recordLogininfor(username, Constants.REGISTER, |
| | | MessageUtils.message("user.register.success"), ServletUtils.getRequest()); |
| | | } |
| | | } |
| | | return msg; |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªéªè¯ç |
| | | * |
| | | * @param username ç¨æ·å |
| | | * @param code éªè¯ç |
| | | * @param uuid å¯ä¸æ è¯ |
| | | * @return ç»æ |
| | | */ |
| | | public void validateCaptcha(String username, String code, String uuid) |
| | | { |
| | | String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; |
| | | String captcha = redisCache.getCacheObject(verifyKey); |
| | | redisCache.deleteObject(verifyKey); |
| | | if (captcha == null) |
| | | { |
| | | throw new CaptchaExpireException(); |
| | | } |
| | | if (!code.equalsIgnoreCase(captcha)) |
| | | { |
| | | throw new CaptchaException(); |
| | | } |
| | | } |
| | | } |