|
|
@ -2,12 +2,18 @@ package com.epmet.service.impl; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.epmet.common.token.constant.LoginConstant; |
|
|
|
import com.epmet.commons.thirdplat.apiservice.AbstractApiService; |
|
|
|
import com.epmet.commons.thirdplat.bean.ThirdPlatUserInfo; |
|
|
|
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.utils.*; |
|
|
|
import com.epmet.dto.*; |
|
|
|
import com.epmet.dto.form.*; |
|
|
|
import com.epmet.dto.result.*; |
|
|
|
import com.epmet.dto.PaCustomerDTO; |
|
|
|
import com.epmet.dto.UserDTO; |
|
|
|
import com.epmet.dto.form.*; |
|
|
@ -18,6 +24,7 @@ import com.epmet.dto.result.UserTokenResultDTO; |
|
|
|
import com.epmet.enums.ThirdPlatformEnum; |
|
|
|
import com.epmet.feign.EpmetUserFeignClient; |
|
|
|
import com.epmet.feign.EpmetUserOpenFeignClient; |
|
|
|
import com.epmet.feign.GovOrgOpenFeignClient; |
|
|
|
import com.epmet.feign.OperCrmOpenFeignClient; |
|
|
|
import com.epmet.jwt.JwtTokenProperties; |
|
|
|
import com.epmet.jwt.JwtTokenUtils; |
|
|
@ -30,10 +37,14 @@ 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.nio.charset.StandardCharsets; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Author zxc |
|
|
@ -60,6 +71,12 @@ public class SsoServiceImpl implements SsoService { |
|
|
|
@Autowired |
|
|
|
private OperCrmOpenFeignClient operCrmOpenFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private GovOrgOpenFeignClient govOrgOpenFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private CpUserDetailRedis cpUserDetailRedis; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private EpmetUserFeignClient epmetUserFeignClient; |
|
|
|
|
|
|
@ -232,6 +249,280 @@ public class SsoServiceImpl implements SsoService { |
|
|
|
return customer.getId(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param formDTO |
|
|
|
* @Author sun |
|
|
|
* @Description 1、ticket自动登录获取内部token |
|
|
|
**/ |
|
|
|
@Override |
|
|
|
public UserTokenResultDTO ssoWorkLogin(SsoLoginFormDTO formDTO) { |
|
|
|
|
|
|
|
//1.根据appId查询客户id
|
|
|
|
String customerId = getCustomerId(formDTO.getAppId()); |
|
|
|
|
|
|
|
//2.客户Id换取第三方apiService,根据ticket换取华为Id
|
|
|
|
Result<ThirdplatApiserviceResultDTO> apiServiceResult = operCrmOpenFeignClient.getApiServiceByCustomerId(new ApiServiceFormDTO(customerId)); |
|
|
|
if (!apiServiceResult.success()) { |
|
|
|
throw new RenException("【SSO登录】调用OperCrm获取ApiService接口失败:", apiServiceResult.getInternalMsg()); |
|
|
|
} |
|
|
|
if (apiServiceResult.getData() == null || StringUtils.isBlank(apiServiceResult.getData().getApiServiceName())) { |
|
|
|
throw new RenException("【SSO登录】调用OperCrm获取ApiService,查询到的结果为空:", apiServiceResult.toString()); |
|
|
|
} |
|
|
|
ThirdPlatUserInfo userInfo; |
|
|
|
try { |
|
|
|
AbstractApiService apiService = (AbstractApiService) SpringContextUtils.getBean(apiServiceResult.getData().getApiServiceName()); |
|
|
|
userInfo = apiService.getUserInfoByTicket(formDTO.getTicket()); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RenException(e.getMessage()); |
|
|
|
} |
|
|
|
if (null == userInfo){ |
|
|
|
throw new RenException(EpmetErrorCode.THIRD_PLAT_REQUEST_ERROR.getCode(), |
|
|
|
"【SSO登录】调用第三方平台查询用户信息失败,用户信息为空"); |
|
|
|
} |
|
|
|
|
|
|
|
//3.根据华为openId查询用户是否存在历史登陆信息
|
|
|
|
Result<StaffLatestAgencyResultDTO> latestStaffWechat = epmetUserOpenFeignClient.getLatestStaffWechatLoginRecord(userInfo.getOpenId()); |
|
|
|
if (!latestStaffWechat.success() || null == latestStaffWechat.getData()) { |
|
|
|
logger.error(String.format("没有获取到用户最近一次登录账户信息,code[%s],msg[%s]", EpmetErrorCode.PLEASE_LOGIN.getCode(), EpmetErrorCode.PLEASE_LOGIN.getMsg())); |
|
|
|
throw new RenException(EpmetErrorCode.PLEASE_LOGIN.getCode()); |
|
|
|
} |
|
|
|
StaffLatestAgencyResultDTO staffLatestAgencyResultDTO = latestStaffWechat.getData(); |
|
|
|
|
|
|
|
//4.记录staff_wechat
|
|
|
|
this.savestaffwechat(staffLatestAgencyResultDTO.getStaffId(), userInfo.getOpenId()); |
|
|
|
|
|
|
|
//5.记录登录日志
|
|
|
|
this.saveStaffLoginRecord(staffLatestAgencyResultDTO); |
|
|
|
|
|
|
|
//6.获取用户token
|
|
|
|
String token = this.generateGovWxmpToken(staffLatestAgencyResultDTO.getStaffId(), formDTO.getApp(), formDTO.getClient()); |
|
|
|
|
|
|
|
//7.保存到redis
|
|
|
|
this.saveLatestGovTokenDto(staffLatestAgencyResultDTO, userInfo, token); |
|
|
|
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO(); |
|
|
|
userTokenResultDTO.setToken(token); |
|
|
|
|
|
|
|
return userTokenResultDTO; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param userId openid |
|
|
|
* @Author sun |
|
|
|
* @Description 保存微信和当前登录用户关系 |
|
|
|
**/ |
|
|
|
private Result savestaffwechat(String userId, String openid) { |
|
|
|
StaffWechatFormDTO staffWechatFormDTO = new StaffWechatFormDTO(); |
|
|
|
staffWechatFormDTO.setUserId(userId); |
|
|
|
staffWechatFormDTO.setWxOpenId(openid); |
|
|
|
return epmetUserOpenFeignClient.saveStaffWechat(staffWechatFormDTO); |
|
|
|
} |
|
|
|
/** |
|
|
|
* @param latestStaffWechatLoginDTO |
|
|
|
* @Author sun |
|
|
|
* @Description 保存登录日志 |
|
|
|
**/ |
|
|
|
private Result saveStaffLoginRecord(StaffLatestAgencyResultDTO latestStaffWechatLoginDTO) { |
|
|
|
StaffLoginAgencyRecordFormDTO staffLoginAgencyRecordFormDTO = new StaffLoginAgencyRecordFormDTO(); |
|
|
|
staffLoginAgencyRecordFormDTO.setCustomerId(latestStaffWechatLoginDTO.getCustomerId()); |
|
|
|
staffLoginAgencyRecordFormDTO.setStaffId(latestStaffWechatLoginDTO.getStaffId()); |
|
|
|
staffLoginAgencyRecordFormDTO.setWxOpenId(latestStaffWechatLoginDTO.getWxOpenId()); |
|
|
|
staffLoginAgencyRecordFormDTO.setMobile(latestStaffWechatLoginDTO.getMobile()); |
|
|
|
staffLoginAgencyRecordFormDTO.setAgencyId(latestStaffWechatLoginDTO.getAgencyId()); |
|
|
|
Result staffLoginRecordResult = epmetUserOpenFeignClient.saveStaffLoginRecord(staffLoginAgencyRecordFormDTO); |
|
|
|
return staffLoginRecordResult; |
|
|
|
} |
|
|
|
/** |
|
|
|
* @Description 生成政府端小程序业务token Key |
|
|
|
* @Author sun |
|
|
|
**/ |
|
|
|
private String generateGovWxmpToken(String staffId, String app, String client) { |
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
map.put("app", app); |
|
|
|
map.put("client", client); |
|
|
|
map.put("userId", staffId); |
|
|
|
String token = jwtTokenUtils.createToken(map); |
|
|
|
logger.info("app:" + app + ";client:" + client + ";userId:" + staffId + ";生成token[" + token + "]"); |
|
|
|
return token; |
|
|
|
} |
|
|
|
/** |
|
|
|
* @Description 保存tokenDto到redis |
|
|
|
* @Author sun |
|
|
|
**/ |
|
|
|
private void saveLatestGovTokenDto(StaffLatestAgencyResultDTO staffLatestAgency, ThirdPlatUserInfo userInfo, String token) { |
|
|
|
int expire = jwtTokenProperties.getExpire(); |
|
|
|
GovTokenDto govTokenDto = new GovTokenDto(); |
|
|
|
govTokenDto.setApp(LoginConstant.APP_GOV); |
|
|
|
govTokenDto.setClient(LoginConstant.CLIENT_WXMP); |
|
|
|
govTokenDto.setUserId(staffLatestAgency.getStaffId()); |
|
|
|
govTokenDto.setOpenId(userInfo.getOpenId()); |
|
|
|
govTokenDto.setSessionKey(""); |
|
|
|
govTokenDto.setUnionId(""); |
|
|
|
govTokenDto.setToken(token); |
|
|
|
govTokenDto.setUpdateTime(System.currentTimeMillis()); |
|
|
|
govTokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); |
|
|
|
govTokenDto.setRootAgencyId(staffLatestAgency.getAgencyId()); |
|
|
|
govTokenDto.setCustomerId(staffLatestAgency.getCustomerId()); |
|
|
|
|
|
|
|
//设置部门,网格,角色列表
|
|
|
|
govTokenDto.setDeptIdList(getDeptartmentIdList(staffLatestAgency.getStaffId())); |
|
|
|
govTokenDto.setGridIdList(getGridIdList(staffLatestAgency.getStaffId())); |
|
|
|
CustomerAgencyDTO agency = getAgencyByStaffId(staffLatestAgency.getStaffId()); |
|
|
|
if (agency != null) { |
|
|
|
govTokenDto.setAgencyId(agency.getId()); |
|
|
|
govTokenDto.setRoleList(queryGovStaffRoles(staffLatestAgency.getStaffId(), agency.getId())); |
|
|
|
} |
|
|
|
govTokenDto.setOrgIdPath(getOrgIdPath(staffLatestAgency.getStaffId())); |
|
|
|
|
|
|
|
cpUserDetailRedis.set(govTokenDto, expire); |
|
|
|
logger.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss")); |
|
|
|
} |
|
|
|
public Set<String> getDeptartmentIdList(String staffId) { |
|
|
|
try { |
|
|
|
Result<List<DepartmentListResultDTO>> deptListResult = govOrgOpenFeignClient.getDepartmentListByStaffId(staffId); |
|
|
|
if (deptListResult.success()) { |
|
|
|
if (!CollectionUtils.isEmpty(deptListResult.getData())) { |
|
|
|
Set<String> deptIdLists = deptListResult.getData().stream().map(dept -> dept.getDepartmentId()).collect(Collectors.toSet()); |
|
|
|
return deptIdLists; |
|
|
|
} |
|
|
|
} else { |
|
|
|
logger.error("登录:查询部门列表,远程调用返回错误:{}", deptListResult.getMsg()); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
logger.error("登录:查询部门列表异常:{}", errorStackTrace); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 根据工作人员ID查询网格ID列表 |
|
|
|
* @param staffId |
|
|
|
*/ |
|
|
|
public Set<String> getGridIdList(String staffId) { |
|
|
|
Result<List<GridByStaffResultDTO>> result = govOrgOpenFeignClient.listGridsbystaffid(staffId); |
|
|
|
if (!result.success()) { |
|
|
|
logger.error("登录:查询网格列表,远程调用返回错误:{}", result.getMsg()); |
|
|
|
return null; |
|
|
|
} else { |
|
|
|
List<GridByStaffResultDTO> grids = result.getData(); |
|
|
|
return grids.stream().map(grid -> grid.getGridId()).collect(Collectors.toSet()); |
|
|
|
} |
|
|
|
} |
|
|
|
/** |
|
|
|
* 根据staffId查询所属的组织机构 |
|
|
|
* |
|
|
|
* @param staffId |
|
|
|
*/ |
|
|
|
public CustomerAgencyDTO getAgencyByStaffId(String staffId) { |
|
|
|
Result<CustomerAgencyDTO> result = govOrgOpenFeignClient.getAgencyByStaff(staffId); |
|
|
|
if (!result.success()) { |
|
|
|
logger.error("登录:查询登录人所属的机关OrgIdPath失败:{}", result.getMsg()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
return result.getData(); |
|
|
|
} |
|
|
|
/** |
|
|
|
* 查询人员在某机关单位下的角色列表 |
|
|
|
* @param staffId orgId |
|
|
|
*/ |
|
|
|
public List<GovTokenDto.Role> queryGovStaffRoles(String staffId, String orgId) { |
|
|
|
StaffRoleFormDTO formDTO = new StaffRoleFormDTO(); |
|
|
|
formDTO.setStaffId(staffId); |
|
|
|
formDTO.setOrgId(orgId); |
|
|
|
Result<List<GovStaffRoleDTO>> gridResult = epmetUserOpenFeignClient.getRolesOfStaff(formDTO); |
|
|
|
if (!CollectionUtils.isEmpty(gridResult.getData())) { |
|
|
|
//return gridResult.getData().stream().map(role -> role.getId()).collect(Collectors.toSet());
|
|
|
|
return ConvertUtils.sourceToTarget(gridResult.getData(), GovTokenDto.Role.class); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 查询工作人员的OrgIdPath |
|
|
|
* @param staffId |
|
|
|
*/ |
|
|
|
public String getOrgIdPath(String staffId) { |
|
|
|
Result<CustomerAgencyDTO> result = govOrgOpenFeignClient.getAgencyByStaff(staffId); |
|
|
|
if (!result.success()) { |
|
|
|
logger.error("登录:查询登录人所属的机关OrgIdPath失败:{}", result.getMsg()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
CustomerAgencyDTO agency = result.getData(); |
|
|
|
if (agency != null) { |
|
|
|
if ("0".equals(agency.getPid())) { |
|
|
|
// 顶级
|
|
|
|
return agency.getId(); |
|
|
|
} else { |
|
|
|
return agency.getPids().concat(":").concat(agency.getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param formDTO |
|
|
|
* @Author sun |
|
|
|
* @Description 4、自动进入组织-返回token |
|
|
|
**/ |
|
|
|
@Override |
|
|
|
public UserTokenResultDTO enterOrg(SsoEnteOrgFormDTO formDTO) { |
|
|
|
//1、需要校验要登录的客户,是否被禁用
|
|
|
|
CustomerStaffFormDTO customerStaffFormDTO = new CustomerStaffFormDTO(); |
|
|
|
customerStaffFormDTO.setCustomerId(formDTO.getCustomerId()); |
|
|
|
customerStaffFormDTO.setMobile(formDTO.getMobile()); |
|
|
|
Result<CustomerStaffDTO> customerStaffDTOResult = epmetUserOpenFeignClient.getCustomerStaffInfo(customerStaffFormDTO); |
|
|
|
if (!customerStaffDTOResult.success() || null == customerStaffDTOResult.getData()) { |
|
|
|
logger.error(String.format("获取工作人员信息失败,手机号[%s],客户id:[%s],code[%s],msg[%s]", formDTO.getMobile(), formDTO.getCustomerId(), customerStaffDTOResult.getCode(), customerStaffDTOResult.getMsg())); |
|
|
|
throw new RenException(customerStaffDTOResult.getCode()); |
|
|
|
} |
|
|
|
CustomerStaffDTO customerStaff = customerStaffDTOResult.getData(); |
|
|
|
|
|
|
|
//2.客户Id换取第三方apiService,根据ticket换取华为Id
|
|
|
|
Result<ThirdplatApiserviceResultDTO> apiServiceResult = operCrmOpenFeignClient.getApiServiceByCustomerId(new ApiServiceFormDTO(formDTO.getCustomerId())); |
|
|
|
if (!apiServiceResult.success()) { |
|
|
|
throw new RenException("【SSO登录】调用OperCrm获取ApiService接口失败:", apiServiceResult.getInternalMsg()); |
|
|
|
} |
|
|
|
if (apiServiceResult.getData() == null || StringUtils.isBlank(apiServiceResult.getData().getApiServiceName())) { |
|
|
|
throw new RenException("【SSO登录】调用OperCrm获取ApiService,查询到的结果为空:", apiServiceResult.toString()); |
|
|
|
} |
|
|
|
ThirdPlatUserInfo userInfo; |
|
|
|
try { |
|
|
|
AbstractApiService apiService = (AbstractApiService) SpringContextUtils.getBean(apiServiceResult.getData().getApiServiceName()); |
|
|
|
userInfo = apiService.getUserInfoByTicket(formDTO.getTicket()); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RenException(e.getMessage()); |
|
|
|
} |
|
|
|
if (null == userInfo){ |
|
|
|
throw new RenException(EpmetErrorCode.THIRD_PLAT_REQUEST_ERROR.getCode(), |
|
|
|
"【SSO登录】调用第三方平台查询用户信息失败,用户信息为空"); |
|
|
|
} |
|
|
|
|
|
|
|
//3、记录staff_wechat,并记录用户激活状态,激活时间
|
|
|
|
this.savestaffwechat(customerStaff.getUserId(), userInfo.getOpenId()); |
|
|
|
|
|
|
|
//4、记录登录日志
|
|
|
|
StaffLatestAgencyResultDTO staffLatestAgencyResultDTO = new StaffLatestAgencyResultDTO(); |
|
|
|
staffLatestAgencyResultDTO.setCustomerId(formDTO.getCustomerId()); |
|
|
|
staffLatestAgencyResultDTO.setStaffId(customerStaff.getUserId()); |
|
|
|
staffLatestAgencyResultDTO.setWxOpenId(userInfo.getOpenId()); |
|
|
|
staffLatestAgencyResultDTO.setMobile(formDTO.getMobile()); |
|
|
|
staffLatestAgencyResultDTO.setAgencyId(formDTO.getRootAgencyId()); |
|
|
|
this.saveStaffLoginRecord(staffLatestAgencyResultDTO); |
|
|
|
|
|
|
|
//5.1、获取用户token
|
|
|
|
String token = this.generateGovWxmpToken(customerStaff.getUserId(), formDTO.getApp(), formDTO.getClient()); |
|
|
|
//5.2、保存到redis
|
|
|
|
StaffLatestAgencyResultDTO staffLatestAgency = new StaffLatestAgencyResultDTO(); |
|
|
|
staffLatestAgency.setAgencyId(formDTO.getRootAgencyId()); |
|
|
|
staffLatestAgency.setCustomerId(formDTO.getCustomerId()); |
|
|
|
staffLatestAgency.setStaffId(customerStaff.getUserId()); |
|
|
|
this.saveLatestGovTokenDto(staffLatestAgency, userInfo, token); |
|
|
|
|
|
|
|
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO(); |
|
|
|
userTokenResultDTO.setToken(token); |
|
|
|
return userTokenResultDTO; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
String url = "https://epmet-ext9.elinkservice.cn/platform/unifiedAuth/loginCheck"; |
|
|
|
String platformToken = "1348803062424166401_dd08e23b0d524879a5c67e7f2ffd1468"; |
|
|
|