|
|
@ -2,11 +2,26 @@ package com.epmet.apiservice; |
|
|
|
|
|
|
|
import com.epmet.apiservice.result.ProjectAssistResult; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.redis.RedisKeys; |
|
|
|
import com.epmet.commons.tools.utils.HttpClientManager; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.commons.tools.utils.SpringContextUtils; |
|
|
|
import com.epmet.constant.ThirdPlatformActions; |
|
|
|
import com.epmet.dao.PaUserDao; |
|
|
|
import com.epmet.dao.ThirdplatformActionDao; |
|
|
|
import com.epmet.dao.ThirdplatformCustomerRegisterDao; |
|
|
|
import com.epmet.dao.ThirdplatformDao; |
|
|
|
import com.epmet.dto.form.ProjectAssistFormDTO; |
|
|
|
import com.epmet.dto.form.TPFDemoFormDTO; |
|
|
|
import com.epmet.entity.ThirdplatformActionEntity; |
|
|
|
import com.epmet.entity.ThirdplatformCustomerRegisterEntity; |
|
|
|
import com.epmet.entity.ThirdplatformEntity; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
|
* ApiService,对接第三方平台的抽象类 |
|
|
@ -29,6 +44,114 @@ public abstract class ApiService { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送get请求。目前仅支持GET/POST |
|
|
|
* @param platformId |
|
|
|
* @param action |
|
|
|
* @param params |
|
|
|
* @param headers |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public String sendGetRequest(String platformId, String action, Map<String, Object> params, Map<String, Object> headers) { |
|
|
|
// 1.获取token
|
|
|
|
String accessToken = getAccessToken(platformId); |
|
|
|
//2.获取url
|
|
|
|
ThirdplatformEntity thirdplatform = SpringContextUtils.getBean(ThirdplatformDao.class).selectById(platformId); |
|
|
|
ThirdplatformActionEntity actionEntity = SpringContextUtils.getBean(ThirdplatformActionDao.class) |
|
|
|
.getByPlatformIdAndActionKey(platformId, action); |
|
|
|
|
|
|
|
if (headers == null) { |
|
|
|
headers = new HashMap<>(); |
|
|
|
} |
|
|
|
|
|
|
|
// 填充access token到头当中
|
|
|
|
headers.put("X-Access-Token", accessToken); |
|
|
|
|
|
|
|
Result<String> result = HttpClientManager.getInstance().sendGet(thirdplatform.getBaseUrl().concat(actionEntity.getApiUrl()), |
|
|
|
thirdplatform.getBaseUrl().startsWith("https://"), |
|
|
|
params, |
|
|
|
headers); |
|
|
|
|
|
|
|
if (result == null) { |
|
|
|
throw new RenException("请求第三方平台,获取AccessToken失败。result为null"); |
|
|
|
} |
|
|
|
if (result.success()) { |
|
|
|
throw new RenException("请求第三方平台,获取AccessToken失败。"); |
|
|
|
} |
|
|
|
return result.getData(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送post请求 |
|
|
|
* @param platformId |
|
|
|
* @param action |
|
|
|
* @param jsonString |
|
|
|
* @param headers |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public String sendPostRequest(String platformId, String action, String jsonString, Map<String, Object> headers) { |
|
|
|
// 1.获取token
|
|
|
|
String accessToken = getAccessToken(platformId); |
|
|
|
//2.获取url
|
|
|
|
ThirdplatformEntity thirdplatform = SpringContextUtils.getBean(ThirdplatformDao.class).selectById(platformId); |
|
|
|
ThirdplatformActionEntity actionEntity = SpringContextUtils.getBean(ThirdplatformActionDao.class) |
|
|
|
.getByPlatformIdAndActionKey(platformId, action); |
|
|
|
|
|
|
|
if (headers == null) { |
|
|
|
headers = new HashMap<>(); |
|
|
|
} |
|
|
|
|
|
|
|
// 填充access token到头当中
|
|
|
|
headers.put("X-Access-Token", accessToken); |
|
|
|
|
|
|
|
Result<String> result = HttpClientManager.getInstance().sendPost(thirdplatform.getBaseUrl().concat(actionEntity.getApiUrl()), |
|
|
|
thirdplatform.getBaseUrl().startsWith("https://"), |
|
|
|
jsonString, |
|
|
|
headers); |
|
|
|
|
|
|
|
if (result == null) { |
|
|
|
throw new RenException("请求第三方平台,获取AccessToken失败。result为null"); |
|
|
|
} |
|
|
|
if (result.success()) { |
|
|
|
throw new RenException("请求第三方平台,获取AccessToken失败。"); |
|
|
|
} |
|
|
|
return result.getData(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 获取accessToken |
|
|
|
* @return |
|
|
|
* @author wxz |
|
|
|
* @date 2021.03.16 13:45 |
|
|
|
*/ |
|
|
|
private String getAccessToken(String platformId) { |
|
|
|
RedisTemplate<String, String> rt = SpringContextUtils.getBean("redisTemplate", RedisTemplate.class); |
|
|
|
String token = rt.opsForValue().get(RedisKeys.getThirdPlatformAccessTokenKey(platformId)); |
|
|
|
if (StringUtils.isBlank(token)) { |
|
|
|
ThirdplatformEntity thirdplatform = SpringContextUtils.getBean(ThirdplatformDao.class).selectById(platformId); |
|
|
|
ThirdplatformActionEntity actionEntity = SpringContextUtils.getBean(ThirdplatformActionDao.class) |
|
|
|
.getByPlatformIdAndActionKey(platformId, ThirdPlatformActions.GET_ACCESS_TOKEN); |
|
|
|
|
|
|
|
String baseUrl = thirdplatform.getBaseUrl(); |
|
|
|
String platformKey = thirdplatform.getPlatformKey(); |
|
|
|
String platformSecret = thirdplatform.getPlatformSecret(); |
|
|
|
|
|
|
|
HashMap<String, Object> params = new HashMap<>(); |
|
|
|
params.put("appKey", platformKey); |
|
|
|
params.put("appSecret", platformSecret); |
|
|
|
Result<String> result = HttpClientManager.getInstance().sendGet(baseUrl.concat(actionEntity.getApiUrl()), params); |
|
|
|
if (result == null) { |
|
|
|
throw new RenException("请求第三方平台,获取AccessToken失败。result为null"); |
|
|
|
} |
|
|
|
if (result.success()) { |
|
|
|
throw new RenException("请求第三方平台,获取AccessToken失败。"); |
|
|
|
} |
|
|
|
token = result.getData(); |
|
|
|
rt.opsForValue().set(RedisKeys.getThirdPlatformAccessTokenKey(platformId), token); |
|
|
|
} |
|
|
|
return token; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 判断客户是否注册了指定的平台,如果没有注册,则抛出异常 |
|
|
|
* @return |
|
|
|