日照智慧社区接口服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

177 lines
6.2 KiB

package com.epmet.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.epmet.auth.constants.AuthOperationConstants;
import com.epmet.commons.rocketmq.messages.LoginMQMsg;
import com.epmet.commons.tools.constant.AppClientConstant;
import com.epmet.commons.tools.constant.ServiceConstant;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.feign.ResultDataResolver;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.redis.RedisUtils;
import com.epmet.commons.tools.security.dto.IcTokenDto;
import com.epmet.commons.tools.utils.CpUserDetailRedis;
import com.epmet.commons.tools.utils.IpUtils;
import com.epmet.commons.tools.utils.Result;
import com.epmet.dto.CustomerAgencyDTO;
import com.epmet.dto.form.SystemMsgFormDTO;
import com.epmet.dto.result.UserTokenResultDTO;
import com.epmet.feign.EpmetMessageOpenFeignClient;
import com.epmet.feign.GovOrgOpenFeignClient;
import com.epmet.jwt.JwtTokenProperties;
import com.epmet.jwt.JwtTokenUtils;
import com.epmet.service.IcLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@Service
@Slf4j
public class IcLoginServiceImpl implements IcLoginService, ResultDataResolver {
@Autowired
private JwtTokenUtils jwtTokenUtils;
@Autowired
private RedisUtils redisUtils;
@Autowired
private CpUserDetailRedis cpUserDetailRedis;
@Autowired
private JwtTokenProperties jwtTokenProperties;
@Autowired
private EpmetMessageOpenFeignClient messageOpenFeignClient;
@Autowired
private GovOrgOpenFeignClient govOrgOpenFeignClient;
@Autowired
private ThirdLoginServiceImpl thirdLoginService;
@Override
public UserTokenResultDTO login(String staffId, String orgId) {
String app = AppClientConstant.APP_IC;
String client = AppClientConstant.CLIENT_WEB;
// 1.获取用户token
String token = this.generateIcToken(staffId, app, client);
Result<CustomerAgencyDTO> agencyResult = govOrgOpenFeignClient.getAgencyById(orgId);
CustomerAgencyDTO agencyInfo = getResultDataOrThrowsException(agencyResult, ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), "【IC平台登录】获取组织信息失败", null);
// 2.缓存token
cacheToken(app, client, staffId, token, orgId, agencyInfo.getCustomerId());
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
userTokenResultDTO.setToken(token);
userTokenResultDTO.setCustomerId(agencyInfo.getCustomerId());
//7.发送登录事件
try {
sendLoginEvent(staffId, app, client);
} catch (RenException e) {
log.error(e.getInternalMsg());
} catch (Exception e) {
log.error("【工作端workLogin登录】发送登录事件失败,程序继续执行。");
}
return userTokenResultDTO;
}
/**
* @param staffId
* @return
* @description 生成Ic平台的Token
* @author wxz
* @date 2021.10.26 13:42:36
*/
private String generateIcToken(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);
return token;
}
/**
* @param userId
* @param fromApp
* @param fromClient
* @return
* @description 发布登录时间
* @author wxz
* @date 2021.10.26 13:45:59
*/
private void sendLoginEvent(String userId, String fromApp, String fromClient) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
LoginMQMsg loginMQMsg = new LoginMQMsg();
loginMQMsg.setUserId(userId);
loginMQMsg.setLoginTime(new Date());
loginMQMsg.setIp(IpUtils.getIpAddr(request));
loginMQMsg.setFromApp(fromApp);
loginMQMsg.setFromClient(fromClient);
SystemMsgFormDTO form = new SystemMsgFormDTO();
form.setMessageType(AuthOperationConstants.LOGIN);
form.setContent(loginMQMsg);
messageOpenFeignClient.sendSystemMsgByMQ(form);
}
/**
* @description 缓存token到redis
*
* @param app
* @param client
* @param userId
* @param token
* @param rootAgencyId
* @return
* @author wxz
* @date 2021.10.26 14:19:07
*/
private void cacheToken(String app,
String client,
String userId,
String token,
String rootAgencyId,
String customerId) {
IcTokenDto tokenDto = new IcTokenDto();
int expire = jwtTokenProperties.getExpire();
long expireTime = jwtTokenUtils.getExpiration(token).getTime();
tokenDto.setApp(app);
tokenDto.setClient(client);
tokenDto.setUserId(userId);
tokenDto.setToken(token);
tokenDto.setExpireTime(expireTime);
tokenDto.setRootAgencyId(rootAgencyId);
tokenDto.setCustomerId(customerId);
//设置部门,网格,角色列表
tokenDto.setDeptIdList(thirdLoginService.getDeptartmentIdList(userId));
tokenDto.setGridIdList(thirdLoginService.getGridIdList(userId));
CustomerAgencyDTO agency = thirdLoginService.getAgencyByStaffId(userId);
if (agency != null) {
tokenDto.setAgencyId(agency.getId());
tokenDto.setRoleList(thirdLoginService.queryGovStaffRoles(userId, agency.getId()));
}
tokenDto.setOrgIdPath(thirdLoginService.getOrgIdPath(userId));
String key = RedisKeys.getCpUserKey(app, client, userId);
Map<String, Object> map = BeanUtil.beanToMap(tokenDto, false, true);
redisUtils.hMSet(key, map, expire);
}
}