|
|
@ -2,38 +2,36 @@ package com.epmet.service.impl; |
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
|
|
|
import com.epmet.common.token.constant.LoginConstant; |
|
|
|
import com.epmet.commons.tools.constant.ServiceConstant; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.security.dto.GovTokenDto; |
|
|
|
import com.epmet.commons.tools.security.dto.TokenDto; |
|
|
|
import com.epmet.commons.tools.security.password.PasswordUtils; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.utils.CpUserDetailRedis; |
|
|
|
import com.epmet.commons.tools.utils.DateUtils; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.dto.*; |
|
|
|
import com.epmet.dto.form.*; |
|
|
|
import com.epmet.dto.result.DepartmentListResultDTO; |
|
|
|
import com.epmet.dto.result.GridByStaffResultDTO; |
|
|
|
import com.epmet.dto.result.StaffLatestAgencyResultDTO; |
|
|
|
import com.epmet.dto.result.UserTokenResultDTO; |
|
|
|
import com.epmet.dto.result.*; |
|
|
|
import com.epmet.feign.EpmetThirdFeignClient; |
|
|
|
import com.epmet.feign.EpmetUserOpenFeignClient; |
|
|
|
import com.epmet.feign.GovOrgOpenFeignClient; |
|
|
|
import com.epmet.jwt.JwtTokenProperties; |
|
|
|
import com.epmet.jwt.JwtTokenUtils; |
|
|
|
import com.epmet.redis.CaptchaRedis; |
|
|
|
import com.epmet.service.ThirdLoginService; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
@ -48,6 +46,8 @@ public class ThirdLoginServiceImpl implements ThirdLoginService { |
|
|
|
@Autowired |
|
|
|
private JwtTokenUtils jwtTokenUtils; |
|
|
|
@Autowired |
|
|
|
private CaptchaRedis captchaRedis; |
|
|
|
@Autowired |
|
|
|
private JwtTokenProperties jwtTokenProperties; |
|
|
|
@Autowired |
|
|
|
private CpUserDetailRedis cpUserDetailRedis; |
|
|
@ -142,6 +142,7 @@ public class ThirdLoginServiceImpl implements ThirdLoginService { |
|
|
|
**/ |
|
|
|
@Override |
|
|
|
public UserTokenResultDTO workLogin(LoginFormDTO formDTO) { |
|
|
|
|
|
|
|
//1.调用epmet_third服务,校验appId是否有效以及是否授权,校验通过的调用微信API获取用户基本信息
|
|
|
|
WxLoginFormDTO resiLoginFormDTO = new WxLoginFormDTO(); |
|
|
|
resiLoginFormDTO.setAppId(formDTO.getAppId()); |
|
|
@ -379,66 +380,150 @@ public class ThirdLoginServiceImpl implements ThirdLoginService { |
|
|
|
wxMaJscode2SessionResult.setSessionKey(userWechatDTO.getSessionKey()); |
|
|
|
wxMaJscode2SessionResult.setUnionid(""); |
|
|
|
// end
|
|
|
|
|
|
|
|
//3、记录staff_wechat,并记录用户激活状态,激活时间
|
|
|
|
this.savestaffwechat(customerStaff.getUserId(), wxMaJscode2SessionResult.getOpenid()); |
|
|
|
this.savestaffwechat(customerStaff.getUserId(), userWechatDTO.getWxOpenId()); |
|
|
|
//4、记录登录日志
|
|
|
|
this.saveGovStaffLoginRecord(formDTO, customerStaff.getUserId(), wxMaJscode2SessionResult.getOpenid()); |
|
|
|
StaffLatestAgencyResultDTO staffLatestAgencyResultDTO = new StaffLatestAgencyResultDTO(); |
|
|
|
staffLatestAgencyResultDTO.setCustomerId(formDTO.getCustomerId()); |
|
|
|
staffLatestAgencyResultDTO.setStaffId(customerStaff.getUserId()); |
|
|
|
staffLatestAgencyResultDTO.setWxOpenId(userWechatDTO.getWxOpenId()); |
|
|
|
staffLatestAgencyResultDTO.setMobile(formDTO.getMobile()); |
|
|
|
staffLatestAgencyResultDTO.setAgencyId(formDTO.getRootAgencyId()); |
|
|
|
this.saveStaffLoginRecord(staffLatestAgencyResultDTO); |
|
|
|
//5.1、获取用户token
|
|
|
|
String token = this.generateGovWxmpToken(customerStaff.getUserId()); |
|
|
|
//5.2、保存到redis
|
|
|
|
this.saveGovTokenDto(formDTO.getRootAgencyId(), formDTO.getCustomerId(), customerStaff.getUserId(), wxMaJscode2SessionResult, token); |
|
|
|
StaffLatestAgencyResultDTO staffLatestAgency = new StaffLatestAgencyResultDTO(); |
|
|
|
staffLatestAgency.setAgencyId(formDTO.getRootAgencyId()); |
|
|
|
staffLatestAgency.setCustomerId(formDTO.getCustomerId()); |
|
|
|
staffLatestAgency.setStaffId(customerStaff.getUserId()); |
|
|
|
this.saveLatestGovTokenDto(staffLatestAgency, userWechatDTO, token); |
|
|
|
|
|
|
|
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO(); |
|
|
|
userTokenResultDTO.setToken(token); |
|
|
|
return userTokenResultDTO; |
|
|
|
} |
|
|
|
|
|
|
|
//保存登录日志
|
|
|
|
private Result saveGovStaffLoginRecord(ThirdWxmpEnteOrgFormDTO formDTO, String staffId, String openId) { |
|
|
|
StaffLoginAgencyRecordFormDTO staffLoginAgencyRecordFormDTO = new StaffLoginAgencyRecordFormDTO(); |
|
|
|
staffLoginAgencyRecordFormDTO.setCustomerId(formDTO.getCustomerId()); |
|
|
|
staffLoginAgencyRecordFormDTO.setStaffId(staffId); |
|
|
|
staffLoginAgencyRecordFormDTO.setWxOpenId(openId); |
|
|
|
staffLoginAgencyRecordFormDTO.setMobile(formDTO.getMobile()); |
|
|
|
staffLoginAgencyRecordFormDTO.setAgencyId(formDTO.getRootAgencyId()); |
|
|
|
Result staffLoginRecordResult = epmetUserOpenFeignClient.saveStaffLoginRecord(staffLoginAgencyRecordFormDTO); |
|
|
|
return staffLoginRecordResult; |
|
|
|
/** |
|
|
|
* @param formDTO |
|
|
|
* @return |
|
|
|
* @Author sun |
|
|
|
* @Description 单客户-手机验证码获取组织 |
|
|
|
**/ |
|
|
|
@Override |
|
|
|
public List<StaffOrgsResultDTO> getMyOrg(ThirdStaffOrgsFormDTO formDTO) { |
|
|
|
//0、验证码是否正确
|
|
|
|
String rightSmsCode = captchaRedis.getSmsCode(formDTO.getMobile()); |
|
|
|
if (!formDTO.getSmsCode().equals(rightSmsCode)) { |
|
|
|
logger.error(String.format("验证码错误code[%s],msg[%s]",EpmetErrorCode.MOBILE_CODE_ERROR.getCode(),EpmetErrorCode.MOBILE_CODE_ERROR.getMsg())); |
|
|
|
throw new RenException(EpmetErrorCode.MOBILE_CODE_ERROR.getCode()); |
|
|
|
} |
|
|
|
//1.根据appId查询对应客户Id
|
|
|
|
Result<PublicCustomerResultDTO> resultDTO = epmetThirdFeignClient.getCustomerMsg(formDTO.getAppId()); |
|
|
|
if (!resultDTO.success()) { |
|
|
|
logger.error(String.format("根据appId查询客户Id失败,对应appId->" + formDTO.getAppId())); |
|
|
|
throw new RenException(resultDTO.getMsg()); |
|
|
|
} |
|
|
|
PaCustomerDTO customer = resultDTO.getData().getCustomer(); |
|
|
|
|
|
|
|
//7.28 根据appId只能存在一个客户Id,后边的批量操作逻辑
|
|
|
|
//2.根据手机号查询到用户信息
|
|
|
|
ThirdCustomerStaffFormDTO dto = new ThirdCustomerStaffFormDTO(); |
|
|
|
dto.setCustomerId(customer.getId()); |
|
|
|
dto.setMobile(formDTO.getMobile()); |
|
|
|
Result<List<CustomerStaffDTO>> customerStaffResult = epmetUserOpenFeignClient.getCustsomerStaffByIdAndPhone(dto); |
|
|
|
if (!customerStaffResult.success()) { |
|
|
|
logger.error(String.format("手机验证码登录异常,手机号[%s],code[%s],msg[%s]", formDTO.getMobile(), customerStaffResult.getCode(), customerStaffResult.getMsg())); |
|
|
|
throw new RenException(customerStaffResult.getCode()); |
|
|
|
} |
|
|
|
|
|
|
|
//3、查询用户所有的组织信息
|
|
|
|
List<String> customerIdList = new ArrayList<>(); |
|
|
|
for (CustomerStaffDTO customerStaffDTO : customerStaffResult.getData()) { |
|
|
|
customerIdList.add(customerStaffDTO.getCustomerId()); |
|
|
|
} |
|
|
|
StaffOrgFormDTO staffOrgFormDTO = new StaffOrgFormDTO(); |
|
|
|
staffOrgFormDTO.setCustomerIdList(customerIdList); |
|
|
|
Result<List<StaffOrgsResultDTO>> result = govOrgOpenFeignClient.getStaffOrgList(staffOrgFormDTO); |
|
|
|
if(result.success()&&null!=result.getData()){ |
|
|
|
return result.getData(); |
|
|
|
} |
|
|
|
logger.error(String .format("手机验证码获取组织,调用%s服务失败,入参手机号%s,验证码%s,返回错误码%s,错误提示信息%s", ServiceConstant.GOV_ORG_SERVER,formDTO.getMobile(),formDTO.getSmsCode(),result.getCode(),result.getMsg())); |
|
|
|
return new ArrayList<>(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 生成token |
|
|
|
* @Date 2020/4/18 23:04 |
|
|
|
* @param formDTO |
|
|
|
* @return |
|
|
|
* @author sun |
|
|
|
* @description 单客户-手机号密码获取组织 |
|
|
|
**/ |
|
|
|
private void saveGovTokenDto(String orgId, |
|
|
|
String customerId, |
|
|
|
String staffId, |
|
|
|
WxMaJscode2SessionResult wxMaJscode2SessionResult, |
|
|
|
String token) { |
|
|
|
int expire = jwtTokenProperties.getExpire(); |
|
|
|
GovTokenDto govTokenDto = new GovTokenDto(); |
|
|
|
govTokenDto.setApp(LoginConstant.APP_GOV); |
|
|
|
govTokenDto.setClient(LoginConstant.CLIENT_WXMP); |
|
|
|
govTokenDto.setUserId(staffId); |
|
|
|
govTokenDto.setOpenId(wxMaJscode2SessionResult.getOpenid()); |
|
|
|
govTokenDto.setSessionKey(wxMaJscode2SessionResult.getSessionKey()); |
|
|
|
govTokenDto.setUnionId(null == wxMaJscode2SessionResult.getUnionid() ? "" : wxMaJscode2SessionResult.getUnionid()); |
|
|
|
govTokenDto.setToken(token); |
|
|
|
govTokenDto.setUpdateTime(System.currentTimeMillis()); |
|
|
|
govTokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); |
|
|
|
govTokenDto.setRootAgencyId(orgId); |
|
|
|
govTokenDto.setCustomerId(customerId); |
|
|
|
@Override |
|
|
|
public List<StaffOrgsResultDTO> getMyOrgByPassword(ThirdStaffOrgsFormDTO formDTO) { |
|
|
|
//0.根据appId查询对应客户Id
|
|
|
|
Result<PublicCustomerResultDTO> resultDTO = epmetThirdFeignClient.getCustomerMsg(formDTO.getAppId()); |
|
|
|
if (!resultDTO.success()) { |
|
|
|
logger.error(String.format("根据appId查询客户Id失败,对应appId->" + formDTO.getAppId())); |
|
|
|
throw new RenException(resultDTO.getMsg()); |
|
|
|
} |
|
|
|
PaCustomerDTO customer = resultDTO.getData().getCustomer(); |
|
|
|
//7.28 上边根据appId只能锁定一条客户id,后边的批量循环操作暂不做调整,还是使用之前的代码 sun
|
|
|
|
//1、根据手机号查询到用户信息
|
|
|
|
ThirdCustomerStaffFormDTO dto = new ThirdCustomerStaffFormDTO(); |
|
|
|
dto.setCustomerId(customer.getId()); |
|
|
|
dto.setMobile(formDTO.getMobile()); |
|
|
|
Result<List<CustomerStaffDTO>> customerStaffResult = epmetUserOpenFeignClient.getCustsomerStaffByIdAndPhone(dto); |
|
|
|
if (!customerStaffResult.success()) { |
|
|
|
logger.error(String.format("手机密码登录异常,手机号[%s],code[%s],msg[%s]", formDTO.getMobile(), customerStaffResult.getCode(), customerStaffResult.getMsg())); |
|
|
|
throw new RenException(customerStaffResult.getCode()); |
|
|
|
} |
|
|
|
//2、密码是否正确
|
|
|
|
List<CustomerStaffDTO> customerStaffList=customerStaffResult.getData(); |
|
|
|
//3、查询用户所有的组织信息
|
|
|
|
List<String> customerIdList = new ArrayList<>(); |
|
|
|
//是否设置过密码
|
|
|
|
boolean havePasswordFlag=false; |
|
|
|
//密码是否正确
|
|
|
|
boolean passwordRightFlag=false; |
|
|
|
for (CustomerStaffDTO customerStaffDTO : customerStaffList) { |
|
|
|
if(StringUtils.isNotBlank(customerStaffDTO.getPassword())){ |
|
|
|
havePasswordFlag=true; |
|
|
|
}else{ |
|
|
|
logger.warn(String.format("当前用户:手机号%s,客户Id%s下未设置密码.",formDTO.getMobile(),customerStaffDTO.getCustomerId())); |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (!PasswordUtils.matches(formDTO.getPassword(), customerStaffDTO.getPassword())) { |
|
|
|
logger.warn(String.format("当前用户:手机号%s,客户Id%s密码匹配错误.",formDTO.getMobile(),customerStaffDTO.getCustomerId())); |
|
|
|
|
|
|
|
//设置部门,网格,角色列表
|
|
|
|
govTokenDto.setDeptIdList(getDeptartmentIdList(staffId)); |
|
|
|
govTokenDto.setGridIdList(getGridIdList(staffId)); |
|
|
|
CustomerAgencyDTO agency = getAgencyByStaffId(staffId); |
|
|
|
if (agency != null) { |
|
|
|
govTokenDto.setAgencyId(agency.getId()); |
|
|
|
govTokenDto.setRoleList(queryGovStaffRoles(staffId, agency.getId())); |
|
|
|
}else{ |
|
|
|
logger.warn(String.format("当前用户:手机号%s,客户Id%s密码匹配正确.",formDTO.getMobile(),customerStaffDTO.getCustomerId())); |
|
|
|
passwordRightFlag=true; |
|
|
|
customerIdList.add(customerStaffDTO.getCustomerId()); |
|
|
|
} |
|
|
|
} |
|
|
|
govTokenDto.setOrgIdPath(getOrgIdPath(staffId)); |
|
|
|
cpUserDetailRedis.set(govTokenDto, expire); |
|
|
|
logger.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss")); |
|
|
|
//根据手机号查出来所有用户,密码都为空,表明用户未激活账户,未设置密码
|
|
|
|
if(!havePasswordFlag){ |
|
|
|
logger.error(String.format("当前手机号(%s)下所有账户都未设置密码,请先使用验证码登录激活账户",formDTO.getMobile())); |
|
|
|
throw new RenException(EpmetErrorCode.PASSWORD_ERROR.getCode()); |
|
|
|
} |
|
|
|
//密码错误
|
|
|
|
if(!passwordRightFlag){ |
|
|
|
logger.error(String.format("根据当前手机号(%s)密码未找到所属组织,密码错误",formDTO.getMobile())); |
|
|
|
throw new RenException(EpmetErrorCode.PASSWORD_ERROR.getCode()); |
|
|
|
} |
|
|
|
StaffOrgFormDTO staffOrgFormDTO = new StaffOrgFormDTO(); |
|
|
|
staffOrgFormDTO.setCustomerIdList(customerIdList); |
|
|
|
Result<List<StaffOrgsResultDTO>> result = govOrgOpenFeignClient.getStaffOrgList(staffOrgFormDTO); |
|
|
|
if(result.success()&&null!=result.getData()){ |
|
|
|
return result.getData(); |
|
|
|
} |
|
|
|
logger.error(String .format("手机验证码获取组织,调用%s服务失败,入参手机号%s,密码%s,返回错误码%s,错误提示信息%s", |
|
|
|
ServiceConstant.GOV_ORG_SERVER, |
|
|
|
formDTO.getMobile(), |
|
|
|
formDTO.getPassword(), |
|
|
|
result.getCode(), |
|
|
|
result.getMsg())); |
|
|
|
return new ArrayList<>(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|