package com.epmet.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.epmet.commons.thirdplat.apiservice.AbstractApiService; import com.epmet.commons.thirdplat.apiservice.jcet.JcetApiService; import com.epmet.commons.thirdplat.constants.ApiServiceBeansConstants; import com.epmet.commons.thirdplat.dto.result.jcet.UserInfoResultDTO; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.*; import com.epmet.constant.SsoConstant; import com.epmet.dto.PaCustomerDTO; import com.epmet.dto.UserDTO; import com.epmet.dto.form.SsoLoginFormDTO; import com.epmet.dto.form.UserInfoFormDTO; import com.epmet.dto.result.SsoLoginResultDTO; import com.epmet.feign.EpmetUserOpenFeignClient; import com.epmet.jwt.JwtTokenProperties; import com.epmet.jwt.JwtTokenUtils; import com.epmet.redis.SsoRedis; import com.epmet.service.SsoService; 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 java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; /** * @Author zxc * @DateTime 2021/1/18 下午4:35 */ @Service @Slf4j public class SsoServiceImpl implements SsoService { Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private SsoRedis ssoRedis; @Autowired private JwtTokenUtils jwtTokenUtils; @Autowired private JwtTokenProperties jwtTokenProperties; @Autowired private EpmetUserOpenFeignClient epmetUserOpenFeignClient; /** * @Description 0、入口:得到token * @Param formDTO * @author zxc * @date 2021/1/18 下午4:59 */ @Override public SsoLoginResultDTO ssoLogin(SsoLoginFormDTO formDTO) { String userId = ""; if (formDTO.getAppId().equals(SsoConstant.WINE_CITY_E_OPEN_APP_ID)) { UserInfoResultDTO userInfo = null; try { AbstractApiService apiService = (AbstractApiService) SpringContextUtils.getBean(ApiServiceBeansConstants.JCET_API_SERVICE); userInfo = apiService.getUserInfoByTicket(formDTO.getTicket()); } catch (Exception e) { logger.error("调用第三方平台,根据用户ticket查询用户信息失败,错误信息"); e.printStackTrace(); } if (null == userInfo){ throw new RenException(SsoConstant.USER_INFO_IS_NULL); } UserInfoFormDTO userInfoFormDTO = ConvertUtils.sourceToTarget(userInfo, UserInfoFormDTO.class); userInfoFormDTO.setApp(formDTO.getApp()); userInfoFormDTO.setUid(userInfo.getId()); userInfoFormDTO.setName(userInfo.getName()); userInfoFormDTO.setMobile(userInfo.getMobile()); Result userDTOResult = epmetUserOpenFeignClient.saveUserInfo(userInfoFormDTO); if (!userDTOResult.success()){ throw new RenException(SsoConstant.INSERT_UPDATE_USER_FAILURE); } userId = userDTOResult.getData().getId(); } if (StringUtils.isBlank(userId)){ throw new RenException(SsoConstant.USER_ID_IS_NULL); } //生成业务token String token = this.generateToken(formDTO.getApp(),formDTO.getClient(), userId); //存放Redis String customerId = getCustomerId(formDTO.getAppId()); if (StringUtils.isBlank(customerId)){ throw new RenException(SsoConstant.CUSTOMER_ID_IS_NULL); } this.disposeTokenDto(formDTO, userId, token, customerId); return new SsoLoginResultDTO(token); } /** * @Description token放缓存 * @Param formDTO * @Param userId * @Param token * @Param customerId * @author zxc * @date 2021/1/18 下午5:32 */ public void disposeTokenDto(SsoLoginFormDTO formDTO, String userId, String token, String customerId){ int expire = jwtTokenProperties.getExpire(); TokenDto tokenDto = new TokenDto(); tokenDto.setCustomerId(customerId); tokenDto.setApp(formDTO.getApp()); tokenDto.setClient(formDTO.getClient()); tokenDto.setUserId(userId); tokenDto.setToken(token); tokenDto.setUpdateTime(System.currentTimeMillis()); tokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); ssoRedis.set(tokenDto, expire); log.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss")); } /** * @Description 居民端登陆生成业务token的key * @Param app * @Param client * @Param userId * @author zxc * @date 2021/1/18 下午5:14 */ private String generateToken(String app,String client, String userId) { Map map = new HashMap<>(16); map.put("app", app); map.put("client", client); map.put("userId", userId); String token = jwtTokenUtils.createToken(map); log.info("app:" + app + ";client:" + client + ";userId:" + userId + ";生成token[" + token + "]"); return token; } /** * @Description 获取customerId * @Param appId * @author zxc * @date 2021/1/19 下午1:47 */ public String getCustomerId(String appId){ JSONObject jsonObject = new JSONObject(); String customerMsgUrl = "https://epmet-cloud.elinkservice.cn/api/third/customermp/getcustomermsg/"; String data = HttpClientManager.getInstance().sendPostByJSON(customerMsgUrl + appId, JSON.toJSONString(jsonObject)).getData(); log.info("调用third服务,根据appId查询客户信息:httpclient->url:" + customerMsgUrl + ",结果->" + data); JSONObject toResult = JSON.parseObject(data); Result mapToResult = ConvertUtils.mapToEntity(toResult, Result.class); if (null != toResult.get("code")) { mapToResult.setCode(((Integer) toResult.get("code")).intValue()); } if (!mapToResult.success()) { log.error(String.format("根据appId查询客户信息失败,对应appId->" + appId)); throw new RenException(mapToResult.getMsg()); } Object publicCustomerResultDTO = mapToResult.getData(); JSONObject json = JSON.parseObject(publicCustomerResultDTO.toString()); Map map = (Map) json.get("customer"); PaCustomerDTO customer = ConvertUtils.mapToEntity(map, PaCustomerDTO.class); log.info("小程序登陆third服务获取客户用户信息PaCustomerDTO->" + customer); return customer.getId(); } }