|  |  | 
 |  |  | package com.doumee.service.system.impl; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.extension.api.R; | 
 |  |  | import com.doumee.config.shiro.ShiroToken; | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.model.LoginUserInfo; | 
 |  |  | import com.doumee.core.utils.Constants; | 
 |  |  | import com.doumee.core.utils.Utils; | 
 |  |  | import com.doumee.dao.business.SmsEmailMapper; | 
 |  |  | import com.doumee.dao.system.dto.LoginDTO; | 
 |  |  | import com.doumee.dao.system.dto.LoginPhoneDTO; | 
 |  |  | import com.doumee.dao.system.model.SystemLoginLog; | 
 |  |  | import com.doumee.service.business.impl.SmsEmailServiceImpl; | 
 |  |  | import com.doumee.service.common.CaptchaService; | 
 |  |  | import com.doumee.service.system.SystemLoginLogService; | 
 |  |  | import com.doumee.service.system.SystemLoginService; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.apache.shiro.SecurityUtils; | 
 |  |  | import org.apache.shiro.authc.AuthenticationException; | 
 |  |  | import org.apache.shiro.authc.UsernamePasswordToken; | 
 |  |  | import org.apache.shiro.subject.Subject; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Value("${project.version}") | 
 |  |  |     private String systemVersion; | 
 |  |  |     @Value("${captcha_check}") | 
 |  |  |     private boolean captchaCheck; | 
 |  |  |  | 
 |  |  |     @Value("${debug_model}") | 
 |  |  |     private Boolean debugModel; | 
 |  |  |     @Autowired | 
 |  |  |     private SmsEmailMapper smsEmailMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private CaptchaService captchaService; | 
 |  |  |  | 
 |  |  | 
 |  |  |         loginLog.setOsInfo(Utils.User_Client.getOS(request)); | 
 |  |  |         loginLog.setServerIp(Utils.Server.getIP()); | 
 |  |  |         // 校验验证码 | 
 |  |  |         try { | 
 |  |  |             if(!debugModel){ | 
 |  |  |                 captchaService.check(dto.getUuid(), dto.getCode()); | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error(e.getMessage(), e); | 
 |  |  |             loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage()); | 
 |  |  |             loginLog.setSuccess(Boolean.FALSE); | 
 |  |  |             systemLoginLogService.create(loginLog); | 
 |  |  |             throw e; | 
 |  |  |         } | 
 |  |  |         // 校验用户名和密码 | 
 |  |  |         Subject subject = SecurityUtils.getSubject(); | 
 |  |  |         ShiroToken token = new ShiroToken(dto.getUsername(), dto.getPassword(),true,type); | 
 |  |  |         try { | 
 |  |  |             subject.login(token); | 
 |  |  |             loginLog.setUserId(((LoginUserInfo)subject.getPrincipal()).getId()); | 
 |  |  |             loginLog.setSuccess(Boolean.TRUE); | 
 |  |  |             systemLoginLogService.create(loginLog); | 
 |  |  |             return (String)subject.getSession().getId(); | 
 |  |  |         } catch (AuthenticationException e) { | 
 |  |  |             log.error(ResponseStatus.ACCOUNT_INCORRECT.getMessage(), e); | 
 |  |  |             loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage()); | 
 |  |  |             loginLog.setSuccess(Boolean.FALSE); | 
 |  |  |             systemLoginLogService.create(loginLog); | 
 |  |  |             throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), Objects.isNull(e.getCause())?ResponseStatus.ACCOUNT_INCORRECT.getMessage():e.getCause().getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     @Override | 
 |  |  |     public String loginByPhone (LoginPhoneDTO dto, int type, HttpServletRequest request) { | 
 |  |  |         SystemLoginLog loginLog = new SystemLoginLog(); | 
 |  |  |         loginLog.setLoginUsername(dto.getPhone()); | 
 |  |  |         loginLog.setLoginTime(new Date()); | 
 |  |  |         loginLog.setSystemVersion(systemVersion); | 
 |  |  |         loginLog.setIp(Utils.User_Client.getIP(request)); | 
 |  |  |         loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); | 
 |  |  |         loginLog.setPlatform(Utils.User_Client.getPlatform(request)); | 
 |  |  |         loginLog.setClientInfo(Utils.User_Client.getBrowser(request)); | 
 |  |  |         loginLog.setOsInfo(Utils.User_Client.getOS(request)); | 
 |  |  |         loginLog.setServerIp(Utils.Server.getIP()); | 
 |  |  |         // 校验验证码 | 
 |  |  |         if(type!= Constants.TWO){ | 
 |  |  |             try { | 
 |  |  |                 if(captchaCheck){ | 
 |  |  |                     captchaService.check(dto.getUuid(), dto.getCode()); | 
 |  |  |                 if(!debugModel){ | 
 |  |  |                     SmsEmailServiceImpl.isCaptcheValide(smsEmailMapper,dto.getPhone(), dto.getCode()); | 
 |  |  |                 } | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 log.error(e.getMessage(), e); | 
 |  |  | 
 |  |  |         } | 
 |  |  |         // 校验用户名和密码 | 
 |  |  |         Subject subject = SecurityUtils.getSubject(); | 
 |  |  |         UsernamePasswordToken token = new UsernamePasswordToken(dto.getUsername(), dto.getPassword()); | 
 |  |  |         ShiroToken token = new ShiroToken(dto.getPhone(), null,false,type); | 
 |  |  |         try { | 
 |  |  |             subject.login(token); | 
 |  |  |             loginLog.setUserId(((LoginUserInfo)subject.getPrincipal()).getId()); |