| | |
| | | package com.ruoyi.framework.web.service; |
| | | |
| | | import cn.hutool.core.util.PhoneUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.common.constant.CacheConstants; |
| | | 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.entity.TAppUserResp; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.core.domain.model.LoginUserApplet; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import com.ruoyi.system.service.TAppUserService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.security.authentication.AuthenticationManager; |
| | | import org.springframework.security.authentication.BadCredentialsException; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | |
| | | private SysPermissionService permissionService; |
| | | @Autowired |
| | | private TAppUserService tAppUserService; |
| | | |
| | | @Value("${spring.profiles.active}") |
| | | private String env; |
| | | |
| | | /** |
| | | * 登录验证 |
| | |
| | | throw new UserNotExistsException(); |
| | | } |
| | | // 用户验证 |
| | | TAppUser user = tAppUserService.getOne(Wrappers.<TAppUser>lambdaQuery().eq(TAppUser::getPhone,username)); |
| | | TAppUser user = tAppUserService.getOne(Wrappers.<TAppUser>lambdaQuery().eq(TAppUser::getPhone,username).ne(TAppUser::getStatus,3)); |
| | | if (StringUtils.isNull(user)){ |
| | | log.info("登录用户:{} 不存在.", username); |
| | | throw new ServiceException(MessageUtils.message("user.not.exists")); |
| | | } else if (user.getStatus().equals(3)) { |
| | | log.info("登录用户:{} 已被删除.", username); |
| | | throw new ServiceException(MessageUtils.message("user.password.delete")); |
| | | log.info("登录用户:{} 不存在.创建一个新用户", username); |
| | | user = new TAppUser(); |
| | | user.setPhone(username); |
| | | user.setStatus(1); |
| | | user.setName(String.format("用户%s", PhoneUtil.subAfter(username))); |
| | | user.setProfilePhoto(""); |
| | | tAppUserService.save(user); |
| | | // throw new ServiceException(MessageUtils.message("user.not.exists")); |
| | | } else { |
| | | if (user.getStatus().equals(2)) { |
| | | log.info("登录用户:{} 已被停用.", username); |
| | | throw new ServiceException(MessageUtils.message("user.blocked")); |
| | | } |
| | | } |
| | | // 校验验证码 |
| | | Object cacheObject = redisCache.getCacheObject(user.getPhone()); |
| | | if(!code.equals(String.valueOf(cacheObject))){ |
| | | log.info("登录用户:{} 短信验证码错误{}", username,code); |
| | | throw new ServiceException("短信验证码错误"); |
| | | if (("test".equals(env) || "dev".equals(env)) && code.equals("111111")) { |
| | | //直接放行 |
| | | } else { |
| | | // 校验验证码 |
| | | Object cacheObject = redisCache.getCacheObject(user.getPhone()); |
| | | if(!code.equals(String.valueOf(cacheObject))){ |
| | | log.info("登录用户:{} 短信验证码错误{}", username,code); |
| | | throw new ServiceException("短信验证码错误"); |
| | | } |
| | | } |
| | | |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); |
| | | LoginUserApplet loginUser = new LoginUserApplet(user.getId(), null, null, null); |
| | | recordAppLoginInfo(loginUser.getUserId().toString()); |
| | | TAppUserResp sysUserResp = new TAppUserResp(); |
| | | BeanUtils.copyProperties(user, sysUserResp); |
| | | loginUser.setUser(sysUserResp); |
| | | // 生成token |
| | | return loginUser; |
| | | } |