日照智慧社区接口服务
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.

168 lines
6.4 KiB

package com.epmet.service.impl;
5 years ago
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epmet.commons.thirdplat.apiservice.jcet.JcetApiService;
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.ConvertUtils;
import com.epmet.commons.tools.utils.DateUtils;
import com.epmet.commons.tools.utils.HttpClientManager;
import com.epmet.commons.tools.utils.Result;
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;
5 years ago
import org.apache.commons.lang3.StringUtils;
5 years ago
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
5 years ago
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 {
5 years ago
@Autowired
private SsoRedis ssoRedis;
@Autowired
private JwtTokenUtils jwtTokenUtils;
@Autowired
private JwtTokenProperties jwtTokenProperties;
@Autowired
private JcetApiService jcetApiService;
@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 {
userInfo = jcetApiService.getUserInfoByTicket(formDTO.getTicket());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (null == userInfo){
throw new RenException(SsoConstant.USER_INFO_IS_NULL);
}
UserInfoFormDTO userInfoFormDTO = ConvertUtils.sourceToTarget(userInfo, UserInfoFormDTO.class);
userInfoFormDTO.setApp(formDTO.getApp());
5 years ago
userInfoFormDTO.setUid(userInfo.getId());
userInfoFormDTO.setName(userInfo.getUserName());
5 years ago
Result<UserDTO> userDTOResult = epmetUserOpenFeignClient.saveUserInfo(userInfoFormDTO);
if (!userDTOResult.success()){
throw new RenException(SsoConstant.INSERT_UPDATE_USER_FAILURE);
}
userId = userDTOResult.getData().getId();
}
5 years ago
if (StringUtils.isBlank(userId)){
throw new RenException(SsoConstant.USER_ID_IS_NULL);
}
5 years ago
//生成业务token
String token = this.generateToken(formDTO.getApp(),formDTO.getClient(), userId);
//存放Redis
5 years ago
String customerId = getCustomerId(formDTO.getAppId());
if (StringUtils.isBlank(customerId)){
throw new RenException(SsoConstant.CUSTOMER_ID_IS_NULL);
}
this.disposeTokenDto(formDTO, userId, token, customerId);
5 years ago
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<String, Object> 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<String, Object> map = (Map) json.get("customer");
PaCustomerDTO customer = ConvertUtils.mapToEntity(map, PaCustomerDTO.class);
log.info("小程序登陆third服务获取客户用户信息PaCustomerDTO->" + customer);
return customer.getId();
}
}