| | |
| | | package com.doumee.service.system.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.doumee.config.jwt.JwtTokenUtil; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.model.LoginUserInfo; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.utils.Constants; |
| | | import com.doumee.core.utils.Utils; |
| | | import com.doumee.dao.business.dao.CompanyMapper; |
| | | import com.doumee.dao.business.dao.SmsEmailMapper; |
| | | import com.doumee.dao.business.model.Company; |
| | | import com.doumee.dao.business.model.SmsEmail; |
| | | import com.doumee.dao.system.SystemUserMapper; |
| | | import com.doumee.dao.system.dto.LoginByOpenidDTO; |
| | | import com.doumee.dao.system.dto.LoginDTO; |
| | | import com.doumee.dao.system.model.SystemLoginLog; |
| | | import com.doumee.dao.system.model.SystemPermission; |
| | | import com.doumee.dao.system.model.SystemRole; |
| | | import com.doumee.dao.system.model.SystemUser; |
| | | import com.doumee.dao.system.dto.LoginPhoneDTO; |
| | | import com.doumee.dao.system.model.*; |
| | | import com.doumee.service.common.CaptchaService; |
| | | import com.doumee.service.system.*; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | |
| | | import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | @Slf4j |
| | | @Service |
| | |
| | | @Lazy |
| | | @Autowired |
| | | private SystemUserService systemUserService; |
| | | @Autowired |
| | | private SystemUserMapper systemUserMapper; |
| | | |
| | | @Autowired |
| | | private SmsEmailMapper smsEmailMapper; |
| | | |
| | | @Lazy |
| | | @Autowired |
| | | private SystemDataPermissionService systemDataPermissionService; |
| | | @Lazy |
| | | @Autowired |
| | | private SystemRoleService systemRoleService; |
| | |
| | | @Autowired |
| | | private SystemLoginLogService systemLoginLogService; |
| | | |
| | | @Autowired |
| | | private CompanyMapper companyMapper; |
| | | |
| | | @Resource |
| | | private JwtTokenUtil jwtTokenUtil; |
| | | |
| | | |
| | | |
| | | @Override |
| | | public String loginByPassword(LoginDTO dto, HttpServletRequest request) { |
| | | SystemLoginLog loginLog = new SystemLoginLog(); |
| | |
| | | loginLog.setLoginTime(new Date()); |
| | | loginLog.setSystemVersion(systemVersion); |
| | | loginLog.setIp(Utils.User_Client.getIP(request)); |
| | | loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); |
| | | // 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(isDebug == null || !isDebug){ |
| | | if((isDebug == null || !isDebug) && (Objects.isNull(dto.getCheckCode()) || dto.getCheckCode()) ){ |
| | | // 校验验证码 |
| | | try { |
| | | captchaService.check(dto.getUuid(), dto.getCode()); |
| | |
| | | loginLog.setLoginUsername(dto.getUsername()); |
| | | loginLog.setLoginTime(new Date()); |
| | | loginLog.setSystemVersion(systemVersion); |
| | | loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); |
| | | // loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); |
| | | if(request!=null&&request.getHeaders()!=null && request.getHeaders().size()>0){ |
| | | loginLog.setIp(Utils.User_Client.getIP(request)); |
| | | loginLog.setPlatform(Utils.User_Client.getPlatform(request)); |
| | |
| | | loginLog.setOsInfo(Utils.User_Client.getOS(request)); |
| | | } |
| | | loginLog.setServerIp(Utils.Server.getIP()); |
| | | if(isDebug == null || !isDebug){ |
| | | // if(isDebug == null || !isDebug && dto.getCheckCode()){ |
| | | // 校验验证码 |
| | | try { |
| | | captchaService.check(dto.getUuid(), dto.getCode()); |
| | |
| | | systemLoginLogService.create(loginLog); |
| | | throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(),"对不起,验证码不正确!"); |
| | | } |
| | | // } |
| | | // 根据用户名查询用户对象 |
| | | SystemUser queryDto = new SystemUser(); |
| | | queryDto.setUsername(dto.getUsername()); |
| | | queryDto.setDeleted(Boolean.FALSE); |
| | | SystemUser user = systemUserService.findOne(queryDto); |
| | | if (user == null) { |
| | | throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT); |
| | | } |
| | | if(!Constants.equalsInteger(user.getSource(),Constants.ZERO)){ |
| | | throw new BusinessException(ResponseStatus.NO_ALLOW_LOGIN); |
| | | } |
| | | String pwd = Utils.Secure.encryptPassword(new String(dto.getPassword()), user.getSalt()); |
| | | // 比较密码 |
| | | if( !StringUtils.equals(pwd, user.getPassword())){ |
| | | throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT); |
| | | } |
| | | dealOpenIdBiz(user,dto.getOpenid()); |
| | | Company company = new Company(); |
| | | if(Objects.nonNull(user.getCompanyId())){ |
| | | company = companyMapper.selectById(user.getCompanyId()); |
| | | } |
| | | // 获取登录用户信息 |
| | | List<SystemRole> roles = systemRoleService.findByUserId(user.getId()); |
| | | List<SystemPermission> permissions = systemPermissionService.findByUserId(user.getId()); |
| | | |
| | | SystemRole rt = new SystemRole(); |
| | | rt.setDeleted(Boolean.FALSE); |
| | | //数据部门权限集合 |
| | | user.setCompanyIdList(systemDataPermissionService.selectHighRole(new SystemDataPermission(),rt,user)); |
| | | |
| | | LoginUserInfo userInfo = LoginUserInfo.from(user, roles, permissions,company,null); |
| | | return userInfo; |
| | | } |
| | | |
| | | private void dealOpenIdBiz(SystemUser user, String openid) { |
| | | if(StringUtils.isNotBlank(openid)){ |
| | | //如果openId不为空,绑定该用户openid |
| | | systemUserMapper.update(null,new UpdateWrapper<SystemUser>().lambda() |
| | | .set(SystemUser::getOpenid,null) |
| | | .eq(SystemUser::getType,user.getType())); |
| | | systemUserMapper.update(null,new UpdateWrapper<SystemUser>().lambda() |
| | | .set(SystemUser::getOpenid,openid) |
| | | .eq(SystemUser::getId,user.getId())); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public LoginUserInfo loginByPhone(LoginPhoneDTO dto, ServerHttpRequest request) { |
| | | SystemLoginLog loginLog = new SystemLoginLog(); |
| | | loginLog.setLoginUsername(dto.getPhone()); |
| | | loginLog.setLoginTime(new Date()); |
| | | loginLog.setSystemVersion(systemVersion); |
| | | // loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); |
| | | if(request!=null&&request.getHeaders()!=null && request.getHeaders().size()>0){ |
| | | loginLog.setIp(Utils.User_Client.getIP(request)); |
| | | 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()); |
| | | |
| | | |
| | | // 根据用户名查询用户对象 |
| | | SystemUser queryDto = new SystemUser(); |
| | | queryDto.setMobile(dto.getPhone()); |
| | | queryDto.setDeleted(Boolean.FALSE); |
| | | SystemUser user = systemUserService.findOne(queryDto); |
| | | if (user == null) { |
| | | throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT); |
| | | } |
| | | if(!Constants.equalsInteger(user.getSource(),Constants.ZERO)){ |
| | | throw new BusinessException(ResponseStatus.NO_ALLOW_LOGIN); |
| | | } |
| | | |
| | | if(isDebug == null || !isDebug){ |
| | | //验证 短信码 |
| | | SmsEmail model = smsEmailMapper.selectOne(new QueryWrapper<SmsEmail>().lambda() |
| | | .eq(SmsEmail::getType, Constants.ZERO) |
| | | .eq(SmsEmail::getPhone, dto.getPhone()) |
| | | .eq(SmsEmail::getRemark, dto.getCode()) |
| | | .eq(SmsEmail::getIsdeleted, Constants.ZERO) |
| | | ); |
| | | if(model == null){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,验证码不正确,请重新发送再试!"); |
| | | } |
| | | if(!Constants.equalsInteger(model.getStatus(),Constants.ZERO)){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,验证码已失效,请重新发送再试!"); |
| | | } |
| | | if(model.getCreateDate() !=null && |
| | | System.currentTimeMillis() - model.getCreateDate().getTime() > 3*60*1000){ |
| | | model.setStatus(Constants.ONE); |
| | | model.setEditDate(new Date()); |
| | | smsEmailMapper.updateById(model); |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,验证码已失效,请重新发送再试~"); |
| | | } |
| | | model.setStatus(Constants.ONE); |
| | | model.setEditDate(new Date()); |
| | | smsEmailMapper.updateById(model); |
| | | } |
| | | |
| | | Company company = new Company(); |
| | | if(Objects.nonNull(user.getCompanyId())){ |
| | | company = companyMapper.selectById(user.getCompanyId()); |
| | | } |
| | | // 获取登录用户信息 |
| | | List<SystemRole> roles = systemRoleService.findByUserId(user.getId()); |
| | | List<SystemPermission> permissions = systemPermissionService.findByUserId(user.getId()); |
| | | |
| | | SystemRole rt = new SystemRole(); |
| | | rt.setDeleted(Boolean.FALSE); |
| | | //数据部门权限集合 |
| | | user.setCompanyIdList(systemDataPermissionService.selectHighRole(new SystemDataPermission(),rt,user)); |
| | | |
| | | LoginUserInfo userInfo = LoginUserInfo.from(user, roles, permissions,company,null); |
| | | return userInfo; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public LoginUserInfo driverLogin(LoginDTO dto, ServerHttpRequest request) { |
| | | SystemLoginLog loginLog = new SystemLoginLog(); |
| | | loginLog.setLoginUsername(dto.getUsername()); |
| | | loginLog.setLoginTime(new Date()); |
| | | loginLog.setReason("【公众号】司机端登陆:openid="+dto.getOpenid()); |
| | | loginLog.setSystemVersion(systemVersion); |
| | | // loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); |
| | | if(request!=null&&request.getHeaders()!=null && request.getHeaders().size()>0){ |
| | | loginLog.setIp(Utils.User_Client.getIP(request)); |
| | | 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()); |
| | | // 根据用户名查询用户对象 |
| | | SystemUser queryDto = new SystemUser(); |
| | | queryDto.setUsername(dto.getUsername()); |
| | |
| | | } |
| | | String pwd = Utils.Secure.encryptPassword(new String(dto.getPassword()), user.getSalt()); |
| | | // 比较密码 |
| | | if( StringUtils.equals(pwd, user.getPassword())){ |
| | | if( !StringUtils.equals(pwd, user.getPassword())){ |
| | | throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT); |
| | | } |
| | | dealOpenIdBiz(user,dto.getOpenid()); |
| | | Company company = new Company(); |
| | | if(Objects.nonNull(user.getCompanyId())){ |
| | | company = companyMapper.selectById(user.getCompanyId()); |
| | | } |
| | | LoginUserInfo userInfo = LoginUserInfo.from(user, null, null,company,null); |
| | | return userInfo; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 内部人员 与 司机 根据code查询openId后进行登录接口 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String loginByUserId(Integer userId) { |
| | | SystemLoginLog loginLog = new SystemLoginLog(); |
| | | loginLog.setLoginTime(new Date()); |
| | | loginLog.setSystemVersion(systemVersion); |
| | | // loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp())); |
| | | loginLog.setServerIp(Utils.Server.getIP()); |
| | | |
| | | // 根据用户名查询用户对象 |
| | | SystemUser user = systemUserService.findById(userId); |
| | | if (user == null) { |
| | | throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT); |
| | | } |
| | | Company company = new Company(); |
| | | if(Objects.nonNull(user.getCompanyId())){ |
| | | company = companyMapper.selectById(user.getCompanyId()); |
| | | } |
| | | // 获取登录用户信息 |
| | | List<SystemRole> roles = systemRoleService.findByUserId(user.getId()); |
| | | List<SystemPermission> permissions = systemPermissionService.findByUserId(user.getId()); |
| | | LoginUserInfo userInfo = LoginUserInfo.from(user, roles, permissions,null); |
| | | return userInfo; |
| | | |
| | | SystemRole rt = new SystemRole(); |
| | | rt.setDeleted(Boolean.FALSE); |
| | | //数据部门权限集合 |
| | | user.setCompanyIdList(systemDataPermissionService.selectHighRole(new SystemDataPermission(),rt,user)); |
| | | |
| | | LoginUserInfo userInfo = LoginUserInfo.from(user, roles, permissions,company,null); |
| | | String token = jwtTokenUtil.generateToken(userInfo); |
| | | |
| | | return token; |
| | | } |
| | | |
| | | } |