MDM接口抽提优化

This commit is contained in:
feng.li
2023-09-12 11:17:33 +08:00
parent f983fce978
commit 94612500cf
2 changed files with 112 additions and 59 deletions

View File

@@ -0,0 +1,95 @@
package com.cool.store.http;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.dto.mdm.AccessTokenDTO;
import com.cool.store.dto.response.MDMResultDTO;
import com.cool.store.exception.ApiException;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.RpcCreateQualifyVerifyReq;
import com.cool.store.request.RpcGetMdmTokenReq;
import com.cool.store.utils.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.Map;
import java.util.Objects;
/**
* @author Fun Li 2023/9/12 10:07
* @version 1.0
*/
@Slf4j
@Service
public class MDMHttpRequest {
@Value("${hs.mdm.baseUrl:null}")
private String mdmBaseUrl;
@Value("${hs.mdm.appkey:null}")
private String mdmAppKey;
@Value("${hs.mdm.appsec:null}")
private String mdmAppSec;
public String getMdmAccessToken() throws ApiException {
RpcGetMdmTokenReq rpcGetMDMTokenReq = new RpcGetMdmTokenReq();
rpcGetMDMTokenReq.setAppKey(mdmAppKey);
rpcGetMDMTokenReq.setAppSecret(mdmAppSec);
String url = mdmBaseUrl + "/api/oauth2/accessToken";
ResponseEntity<MDMResultDTO> responseEntity = null;
try {
responseEntity = RestTemplateUtil.post(url,rpcGetMDMTokenReq, MDMResultDTO.class);
log.info("url:{}, response:{}", url, JSONObject.toJSONString(responseEntity));
if (Objects.nonNull(responseEntity.getBody()) && responseEntity.getBody().isSuccess()) {
AccessTokenDTO accessTokenDTO = JSONObject.parseObject(JSONObject.toJSONString(responseEntity.getBody().getData()), AccessTokenDTO.class);
if(accessTokenDTO==null || StringUtils.isBlank(accessTokenDTO.getAccessToken())){
throw new ServiceException("获取Mdm token失败!");
}
return accessTokenDTO.getAccessToken();
}
} catch (Exception e) {
log.info("获取MDM Token 出错 url:\t{}, e:\t{}", url, e);
throw new ApiException(e.getMessage());
}
return null;
}
public String createQualifyVerify(Map<String, String> headers, RpcCreateQualifyVerifyReq rpcRequest) throws ApiException{
String url = mdmBaseUrl + "/api/openapi/runtime/form/startFraQualExamWithData";
ResponseEntity<MDMResultDTO> responseEntity = null;
try {
responseEntity = RestTemplateUtil.post(url, headers, rpcRequest, MDMResultDTO.class);
log.info("url:{}, header:{}, request:{} response:{}", url, JSONObject.toJSONString(headers), JSONObject.toJSONString(rpcRequest), JSONObject.toJSONString(responseEntity));
if (Objects.nonNull(responseEntity.getBody()) && responseEntity.getBody().isSuccess()) {
return JSONObject.toJSONString(responseEntity.getBody().getData());
}
} catch (Exception e) {
log.info("调用MDM接口出错 url{}, e{}", url, e);
throw new ApiException(e.getMessage());
}
return null;
}
public ResponseEntity<MDMResultDTO> uploadFile(Map<String, String> headers, Resource resource) throws ApiException {
String url = mdmBaseUrl + "/api/openapi/ext/upload/file";
MultiValueMap<String,Object> param = new LinkedMultiValueMap<>();
try {
param.add("file", resource);
ResponseEntity<MDMResultDTO> responseEntity = RestTemplateUtil.post(url, headers, param, MDMResultDTO.class);
log.info("url:{}, header:{}, response statusCode:{}", url, JSONObject.toJSONString(headers), JSONObject.toJSONString(responseEntity.getStatusCode()));
return responseEntity;
} catch (Exception e) {
log.info("调用MDM接口出错 url{}, e{}", url, e);
throw new ApiException(e.getMessage());
}
}
}

View File

@@ -22,6 +22,7 @@ import com.cool.store.enums.OperateTypeEnum;
import com.cool.store.enums.WorkflowStatusEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.exception.ServiceException;
import com.cool.store.http.MDMHttpRequest;
import com.cool.store.mapper.*;
import com.cool.store.oss.OSSServer;
import com.cool.store.request.*;
@@ -104,6 +105,9 @@ public class FlowServiceImpl implements FlowService {
@Autowired
private HyInspectionMapper inspectionMapper;
@Autowired
private MDMHttpRequest mdmHttpRequest;
@Override
@Transactional
public void createQualifyVerify(CreateQualifyVerifyReq request) throws ApiException, IOException {
@@ -180,12 +184,16 @@ public class FlowServiceImpl implements FlowService {
}
//通过 rpc 请求审核系统获取返回数据
//上传证明文件数据
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", mdmHttpRequest.getMdmAccessToken());
if (StringUtils.isNotEmpty(request.getSignerRealControlRelationCert())) {
List<String> certFileList = Arrays.asList(request.getSignerRealControlRelationCert().split(","));
List<SkrRelshipProve> relshipProves = OSSFileToMDMFile(certFileList);
List<SkrRelshipProve> relshipProves = OSSFileToMDMFile(headers, certFileList);
rpcRequest.getData().setSkrRelshipProve(relshipProves);
}
Map<String, String> qualifyVerifyRespData = JSON.parseObject(createQualifyVerify(rpcRequest), new TypeReference<HashMap<String,String>>() {});
//调用接口发起审批
String qualifyVerify = mdmHttpRequest.createQualifyVerify(headers, rpcRequest);
Map<String, String> qualifyVerifyRespData = JSON.parseObject(qualifyVerify, new TypeReference<HashMap<String,String>>() {});
//2.更新审核信息
HyPartnerCertificationInfoDO partnerCertificationInfoDO = new HyPartnerCertificationInfoDO();
@@ -299,14 +307,10 @@ public class FlowServiceImpl implements FlowService {
}
}
public List<SkrRelshipProve> OSSFileToMDMFile(List<String> fileUrlList) throws ApiException, IOException {
String url = mdmBaseUrl + "/api/openapi/ext/upload/file";
ResponseEntity<MDMResultDTO> responseEntity = null;
RpcGetMdmTokenReq rpcGetMDMTokenReq = new RpcGetMdmTokenReq();
rpcGetMDMTokenReq.setAppKey(mdmAppKey);
rpcGetMDMTokenReq.setAppSecret(mdmAppSec);
private List<SkrRelshipProve> OSSFileToMDMFile(Map<String, String> headers, List<String> fileUrlList) throws ApiException, IOException {
ByteArrayOutputStream outputStream = null;
List<SkrRelshipProve> relshipProves = new ArrayList<>();
ResponseEntity<MDMResultDTO> responseEntity = null;
//逐个处理文件
for (String fileUrl : fileUrlList) {
@@ -315,76 +319,30 @@ public class FlowServiceImpl implements FlowService {
outputStream = ossServer.downloadFileServer(fileName);
//2. 将下载到的文件上传到 MDM 系统中
try {
//获取 token 设置到 header
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getMdmAccessToken(rpcGetMDMTokenReq));
//将文件流编辑为 formdata 格式的数据
MultiValueMap<String,Object> param = new LinkedMultiValueMap<>();
ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray()) {
@Override
public String getFilename() throws IllegalStateException {
return fileName;
}
};
param.add("file", resource);
//上传文件
responseEntity = RestTemplateUtil.post(url, headers, param, MDMResultDTO.class);
log.info("url:{}, header:{}, response statusCode:{}", url, JSONObject.toJSONString(headers), JSONObject.toJSONString(responseEntity.getStatusCode()));
responseEntity = mdmHttpRequest.uploadFile(headers, resource);
Map<String, String> data = (Map<String, String>) responseEntity.getBody().getData();
SkrRelshipProve skrRelshipProve= BeanUtil.fillBeanWithMap(data, new SkrRelshipProve(), false);
relshipProves.add(skrRelshipProve);
} catch (Exception e) {
log.info("调用MDM接口出错 url{}, fileUrl{}, e{}", url, fileUrl, e);
log.info("上传文件到MDM出错, e{}", e);
throw new ApiException(e.getMessage());
} finally {
outputStream.close();
if (outputStream != null) {
outputStream.close();
}
}
}
return relshipProves;
}
public String createQualifyVerify(RpcCreateQualifyVerifyReq rpcRequest) throws ApiException{
String url = mdmBaseUrl + "/api/openapi/runtime/form/startFraQualExamWithData";
ResponseEntity<MDMResultDTO> responseEntity = null;
try {
RpcGetMdmTokenReq rpcGetMDMTokenReq = new RpcGetMdmTokenReq();
rpcGetMDMTokenReq.setAppKey(mdmAppKey);
rpcGetMDMTokenReq.setAppSecret(mdmAppSec);
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getMdmAccessToken(rpcGetMDMTokenReq));
responseEntity = RestTemplateUtil.post(url, headers, rpcRequest, MDMResultDTO.class);
log.info("url:{}, header:{}, request:{} response:{}", url, JSONObject.toJSONString(headers), JSONObject.toJSONString(rpcRequest), JSONObject.toJSONString(responseEntity));
if (Objects.nonNull(responseEntity.getBody()) && responseEntity.getBody().isSuccess()) {
return JSONObject.toJSONString(responseEntity.getBody().getData());
}
} catch (Exception e) {
log.info("调用MDM接口出错 url{}, e{}", url, e);
throw new ApiException(e.getMessage());
}
return null;
}
public String getMdmAccessToken(RpcGetMdmTokenReq rpcGetMDMTokenReq) throws ApiException {
String url = mdmBaseUrl + "/api/oauth2/accessToken";
ResponseEntity<MDMResultDTO> responseEntity = null;
try {
responseEntity = RestTemplateUtil.post(url,rpcGetMDMTokenReq, MDMResultDTO.class);
log.info("url:{}, response:{}", url, JSONObject.toJSONString(responseEntity));
if (Objects.nonNull(responseEntity.getBody()) && responseEntity.getBody().isSuccess()) {
AccessTokenDTO accessTokenDTO = JSONObject.parseObject(JSONObject.toJSONString(responseEntity.getBody().getData()), AccessTokenDTO.class);
if(accessTokenDTO==null || StringUtils.isBlank(accessTokenDTO.getAccessToken())){
throw new ServiceException("获取Mdm token失败!");
}
return accessTokenDTO.getAccessToken();
}
} catch (Exception e) {
log.info("获取MDM Token 出错 url:\t{}, e:\t{}", url, e);
throw new ApiException(e.getMessage());
}
return null;
}
/**
* 生成授权码