Browse Source

初步完成对接平台的代码,平台不通,待验证

master
wxz 5 years ago
parent
commit
cb6c0078ac
  1. 2
      epmet-cloud-generator/src/main/resources/application.yml
  2. 9
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java
  3. 13
      epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/constant/ThirdPlatformActions.java
  4. 123
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java
  5. 11
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java
  6. 10
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/ThirdplatformActionDao.java
  7. 1
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformActionEntity.java
  8. 5
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformEntity.java
  9. 17
      epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/ThirdplatformActionDao.xml

2
epmet-cloud-generator/src/main/resources/application.yml

@ -9,7 +9,7 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
#MySQL配置 #MySQL配置
driverClassName: com.mysql.jdbc.Driver driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.130:3306/epmet_gov_project?useUnicode=true&characterEncoding=UTF-8&useSSL=false url: jdbc:mysql://192.168.1.130:3306/epmet_third?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: epmet_dba username: epmet_dba
password: EpmEt-dbA-UsEr password: EpmEt-dbA-UsEr
#oracle配置 #oracle配置

9
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java

@ -379,4 +379,13 @@ public class RedisKeys {
public static String listCustomerApiServiceListKey(String customerId) { public static String listCustomerApiServiceListKey(String customerId) {
return rootPrefix.concat("customer:thirdplat:apiservicelist:").concat(customerId); return rootPrefix.concat("customer:thirdplat:apiservicelist:").concat(customerId);
} }
/**
* 查询第三方平台access token
* @param platformId
* @return
*/
public static String getThirdPlatformAccessTokenKey(String platformId) {
return rootPrefix.concat("thirdplatform:accesstoken:").concat(platformId);
}
} }

13
epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/constant/ThirdPlatformActions.java

@ -0,0 +1,13 @@
package com.epmet.constant;
/**
* 第三方平台动作常量
*/
public interface ThirdPlatformActions {
// 获取accessToken
String GET_ACCESS_TOKEN = "GET_ACCESS_TOKEN";
// 项目协助
String PROJECT_ASSIST = "PROJECT_ASSIST";
}

123
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java

@ -2,11 +2,26 @@ package com.epmet.apiservice;
import com.epmet.apiservice.result.ProjectAssistResult; import com.epmet.apiservice.result.ProjectAssistResult;
import com.epmet.commons.tools.exception.RenException; 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.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.ThirdplatformCustomerRegisterDao;
import com.epmet.dao.ThirdplatformDao;
import com.epmet.dto.form.ProjectAssistFormDTO; import com.epmet.dto.form.ProjectAssistFormDTO;
import com.epmet.dto.form.TPFDemoFormDTO; import com.epmet.dto.form.TPFDemoFormDTO;
import com.epmet.entity.ThirdplatformActionEntity;
import com.epmet.entity.ThirdplatformCustomerRegisterEntity; 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对接第三方平台的抽象类 * ApiService对接第三方平台的抽象类
@ -29,6 +44,114 @@ public abstract class ApiService {
return true; 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 判断客户是否注册了指定的平台如果没有注册则抛出异常 * @Description 判断客户是否注册了指定的平台如果没有注册则抛出异常
* @return * @return

11
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java

@ -1,8 +1,11 @@
package com.epmet.apiservice.impl; package com.epmet.apiservice.impl;
import com.alibaba.fastjson.JSON;
import com.epmet.apiservice.ApiService; import com.epmet.apiservice.ApiService;
import com.epmet.apiservice.result.ProjectAssistResult; import com.epmet.apiservice.result.ProjectAssistResult;
import com.epmet.constant.ThirdPlatformActions;
import com.epmet.dto.form.ProjectAssistFormDTO; import com.epmet.dto.form.ProjectAssistFormDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -13,7 +16,13 @@ public class LuzhouGridPlatformApiService extends ApiService {
@Override @Override
public ProjectAssistResult projectAssist(ProjectAssistFormDTO formDTO) { public ProjectAssistResult projectAssist(ProjectAssistFormDTO formDTO) {
String platformId = formDTO.getPlatformId();
String result = sendPostRequest(platformId, ThirdPlatformActions.PROJECT_ASSIST, "{}", null);
ProjectAssistResult projectAssistResult = null;
if (!StringUtils.isBlank(result)) {
projectAssistResult = JSON.parseObject(result, ProjectAssistResult.class);
}
System.out.println("泸州网格化平台项目协助发送成功"); System.out.println("泸州网格化平台项目协助发送成功");
return null; return projectAssistResult;
} }
} }

10
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/ThirdplatformActionDao.java

@ -20,6 +20,7 @@ package com.epmet.dao;
import com.epmet.commons.mybatis.dao.BaseDao; import com.epmet.commons.mybatis.dao.BaseDao;
import com.epmet.entity.ThirdplatformActionEntity; import com.epmet.entity.ThirdplatformActionEntity;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/** /**
* *
@ -29,5 +30,12 @@ import org.apache.ibatis.annotations.Mapper;
*/ */
@Mapper @Mapper
public interface ThirdplatformActionDao extends BaseDao<ThirdplatformActionEntity> { public interface ThirdplatformActionDao extends BaseDao<ThirdplatformActionEntity> {
/**
* @Description 根据平台id和action操作查询
* @return
* @author wxz
* @date 2021.03.16 13:35
*/
ThirdplatformActionEntity getByPlatformIdAndActionKey(@Param("platformId") String platformId, @Param("action") String action);
} }

1
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformActionEntity.java

@ -47,5 +47,6 @@ public class ThirdplatformActionEntity extends BaseEpmetEntity {
* *
*/ */
private String actionKey; private String actionKey;
private String apiUrl;
} }

5
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformEntity.java

@ -58,4 +58,9 @@ public class ThirdplatformEntity extends BaseEpmetEntity {
*/ */
private String apiService; private String apiService;
/**
* 基础url
*/
private String baseUrl;
} }

17
epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/ThirdplatformActionDao.xml

@ -7,6 +7,7 @@
<result property="id" column="ID"/> <result property="id" column="ID"/>
<result property="platformId" column="PLATFORM_ID"/> <result property="platformId" column="PLATFORM_ID"/>
<result property="actionKey" column="ACTION_KEY"/> <result property="actionKey" column="ACTION_KEY"/>
<result property="apiUrl" column="API_URL"/>
<result property="delFlag" column="DEL_FLAG"/> <result property="delFlag" column="DEL_FLAG"/>
<result property="revision" column="REVISION"/> <result property="revision" column="REVISION"/>
<result property="createdBy" column="CREATED_BY"/> <result property="createdBy" column="CREATED_BY"/>
@ -15,5 +16,21 @@
<result property="updatedTime" column="UPDATED_TIME"/> <result property="updatedTime" column="UPDATED_TIME"/>
</resultMap> </resultMap>
<select id="getByPlatformIdAndActionKey" resultType="com.epmet.entity.ThirdplatformActionEntity">
select id,
platform_id,
action_key,
api_url,
del_flag,
revision,
created_by,
created_time,
updated_by,
updated_time
from thirdplatform_action ta
where ta.PLATFORM_ID = #{platformId}
and ACTION_KEY = #{action}
</select>
</mapper> </mapper>
Loading…
Cancel
Save