diff --git a/epmet-cloud-generator/src/main/resources/application.yml b/epmet-cloud-generator/src/main/resources/application.yml index bfea2783f9..5a36af7c15 100644 --- a/epmet-cloud-generator/src/main/resources/application.yml +++ b/epmet-cloud-generator/src/main/resources/application.yml @@ -9,7 +9,7 @@ spring: type: com.alibaba.druid.pool.DruidDataSource #MySQL配置 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 password: EpmEt-dbA-UsEr #oracle配置 diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java index 220d621195..3a2e5b0fa9 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java +++ b/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) { 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); + } } diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/constant/ThirdPlatformActions.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/constant/ThirdPlatformActions.java new file mode 100644 index 0000000000..43390a2e14 --- /dev/null +++ b/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"; + +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java index da169d165a..c0326a98a5 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java +++ b/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.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 params, Map 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 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 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 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 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 params = new HashMap<>(); + params.put("appKey", platformKey); + params.put("appSecret", platformSecret); + Result 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 diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java index 4081bfad61..646ce55af9 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java +++ b/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; +import com.alibaba.fastjson.JSON; import com.epmet.apiservice.ApiService; import com.epmet.apiservice.result.ProjectAssistResult; +import com.epmet.constant.ThirdPlatformActions; import com.epmet.dto.form.ProjectAssistFormDTO; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; /** @@ -13,7 +16,13 @@ public class LuzhouGridPlatformApiService extends ApiService { @Override 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("泸州网格化平台项目协助发送成功"); - return null; + return projectAssistResult; } } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/ThirdplatformActionDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/ThirdplatformActionDao.java index 9b309c15fc..50de9d3629 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/ThirdplatformActionDao.java +++ b/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.entity.ThirdplatformActionEntity; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * @@ -29,5 +30,12 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface ThirdplatformActionDao extends BaseDao { - + + /** + * @Description 根据平台id和action操作查询 + * @return + * @author wxz + * @date 2021.03.16 13:35 + */ + ThirdplatformActionEntity getByPlatformIdAndActionKey(@Param("platformId") String platformId, @Param("action") String action); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformActionEntity.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformActionEntity.java index 88c7a8cd2f..97283b9c05 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformActionEntity.java +++ b/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 apiUrl; } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformEntity.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformEntity.java index 4fede6dcdf..d756c81b88 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/ThirdplatformEntity.java +++ b/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; + /** + * 基础url + */ + private String baseUrl; + } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/ThirdplatformActionDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/ThirdplatformActionDao.xml index 03cf514cda..39de22dca5 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/ThirdplatformActionDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/ThirdplatformActionDao.xml @@ -7,6 +7,7 @@ + @@ -15,5 +16,21 @@ + + \ No newline at end of file