diff --git a/fw-oa/src/main/java/com/hzya/frame/seeyon/service/impl/SeeYonInterFaceImpl.java b/fw-oa/src/main/java/com/hzya/frame/seeyon/service/impl/SeeYonInterFaceImpl.java index 619d0eca..d8c718ce 100644 --- a/fw-oa/src/main/java/com/hzya/frame/seeyon/service/impl/SeeYonInterFaceImpl.java +++ b/fw-oa/src/main/java/com/hzya/frame/seeyon/service/impl/SeeYonInterFaceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @@ -13,12 +14,14 @@ import com.hzya.frame.seeyon.dao.ISeeYonInterFaceDao; import com.hzya.frame.seeyon.entity.OAWorkflowEventDataEntity; import com.hzya.frame.seeyon.entity.SeeYonInterFaceEntity; import com.hzya.frame.seeyon.service.ISeeYonInterFace; +import com.hzya.frame.sys.sysenum.SysEnum; import com.hzya.frame.sysnew.application.api.entity.SysApplicationApiEntity; import com.hzya.frame.sysnew.application.api.service.ISysApplicationApiService; import com.hzya.frame.sysnew.application.database.dao.ISysApplicationDatabaseDao; import com.hzya.frame.sysnew.application.database.entity.SysApplicationDatabaseEntity; import com.hzya.frame.sysnew.application.entity.SysApplicationEntity; import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity; +import com.hzya.frame.sysnew.application.service.impl.ApplicationCache; import com.hzya.frame.util.PluginUtils; import com.hzya.frame.web.entity.BaseResult; import com.hzya.frame.web.entity.JsonResultEntity; @@ -26,12 +29,22 @@ import com.hzya.frame.web.exception.BaseSystemException; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -77,6 +90,10 @@ public class SeeYonInterFaceImpl implements ISeeYonInterFace { private ISeeYonInterFaceDao seeYonInterFaceDao; //上一次同步时间 private final String LAST_SYNCHRONISED_TIME = ""; + @Value("${zt.url}") + private String url ; + @Resource + private ApplicationCache applicationCache; { eventTypeBuffer.append("发起前事件 onBeforeStart ,"); @@ -564,4 +581,264 @@ public class SeeYonInterFaceImpl implements ISeeYonInterFace { } + @Override + public SysExtensionApiEntity getToken(SysExtensionApiEntity entity) { + Map headers = entity.getHeaders(); + SysApplicationApiEntity sysApplicationEntity = entity.getReceiveApi(); + SysApplicationEntity sendApp = entity.getSendApp(); + SysApplicationEntity receiveApp = entity.getReceiveApp(); + if("1".equals(sysApplicationEntity.getNeedLogin()) && sysApplicationEntity.getAuthenticationPort() != null){ + SysApplicationApiEntity loginApi = getApiByAppIdApiCode(sysApplicationEntity.getAuthenticationPort()); + if (null == loginApi) { + return entity; + } + String rzquerys = getQuery(loginApi,null,null); + Map headersa = new HashMap<>(); + headersa.put("publicKey", sendApp.getPublicKey()); + headersa.put("secretKey", sendApp.getSecretKey()); + headersa.put("appId", receiveApp.getAppId().toString()); + headersa.put("apiCode", loginApi.getApiCode().toString()); + Map rzheaders = getHeaders(loginApi,headersa,null); + String rzbodys = getBodys(loginApi,null,null); + JsonResultEntity rzjsonResultEntity = sendData(loginApi,rzheaders,rzbodys,rzquerys); + if (!rzjsonResultEntity.isFlag()) { + return entity; + } + JSONObject attritube = JSONObject.parseObject(rzjsonResultEntity.getAttribute().toString()); + headers.put("token",attritube.getJSONObject("attribute").getString("id")); + } + entity.setHeaders(headers); + return entity; + } + private SysApplicationApiEntity getApiByAppIdApiCode(String apiId) { + + String str = "apiId" + apiId ; + Object o = applicationCache.get("5", str); + if (o != null) { + return (SysApplicationApiEntity) o; + } + return null; + } + private JsonResultEntity sendData(SysApplicationApiEntity applicationApiEntity, Map headers ,String bodys ,String querys) { + StringBuffer urls = new StringBuffer(url); + if (querys != null) { + urls.append("?"); + urls.append(querys); + } + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); + // HttpClient + CloseableHttpClient closeableHttpClient = httpClientBuilder.disableCookieManagement().build(); + HttpPost post = new HttpPost(urls.toString()); + CloseableHttpResponse response = null; + + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Integer.valueOf(applicationApiEntity.getTimeoutPeriod())).build(); + post.setConfig(requestConfig);//设置请求参数【超时时间】 + + if (headers != null && headers.size() > 0) { + for (String key : headers.keySet()) { + post.setHeader(key, headers.get(key)); + } + } + StringBuilder body = new StringBuilder(); + boolean flag = true; + try { + if (bodys != null && !"".equals(bodys)) { + ByteArrayEntity entity = new ByteArrayEntity(bodys.getBytes("UTF-8")); + entity.setContentType("application/json"); + post.setEntity(entity); + } + response = closeableHttpClient.execute(post); + + HttpEntity entity = response.getEntity(); + body.append(EntityUtils.toString(entity,"UTF-8")); + flag = true; + logger.info("返回结果:" + body); + } catch (Exception e) { + logger.error("请求错误:" + e.getMessage()); + body.append(e.getMessage()); + flag = false; + } finally { + try { + // 关闭响应对象 + if (response != null) { + response.close(); + } + // 关闭响应对象 + if (closeableHttpClient != null) { + closeableHttpClient.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + if (flag) { + if (JSONUtil.isTypeJSON(body.toString())) { + JsonResultEntity jsonResultEntity = JSONObject.parseObject(body.toString(),JsonResultEntity.class); + if(jsonResultEntity.isFlag()){ + return BaseResult.getSuccessMessageEntity("转发成功", body); + }else { + return BaseResult.getFailureMessageEntity("转发失败", body); + } + }else { + return BaseResult.getFailureMessageEntity("转发失败", body); + } + } else { + return BaseResult.getFailureMessageEntity("转发失败", body); + } + } + + private String getBodys(SysApplicationApiEntity loginApi,String sendDatastr, JSONObject loginData) { + JSONObject sendData = new JSONObject(); + if(sendDatastr != null ){ + sendData = JSONObject.parseObject(sendDatastr); + } + if(loginData == null){ + loginData = new JSONObject(); + } + if (loginApi.getBodyIn() != null && !"".equals(loginApi.getBodyIn())) { + if (JSONUtil.isTypeJSONArray(loginApi.getBodyIn())) { + JSONArray headerbodyArray = JSON.parseArray(loginApi.getBodyIn()); + JSONObject bodyjson = headerbodyArray.getJSONObject(0); + JSONArray headerArray = bodyjson.getJSONArray("children"); + for (int i = 0; i < headerArray.size(); i++) { + //获取到第一个数据 + JSONObject querys = headerArray.getJSONObject(i); + if (SysEnum.FUNDAMENTAL.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//认证类型 + String query = querys.getString(SysEnum.EXAMPLE.getValue()); + if (query != null && !"".equals(query)) { + sendData.put(querys.getString(SysEnum.PARAMETERNAME.getValue()),query); + } + } + } + } + } + return sendData.toString(); + } + + private Map getHeaders(SysApplicationApiEntity loginApi,Map map,JSONObject loginData) { + if(loginData == null){ + loginData = new JSONObject(); + } + if (loginApi.getHeaderIn() != null && !"".equals(loginApi.getHeaderIn())) { + if (JSONUtil.isTypeJSONArray(loginApi.getHeaderIn())) { + JSONArray headerArray = JSON.parseArray(loginApi.getHeaderIn()); + for (int i = 0; i < headerArray.size(); i++) { + JSONObject querys = headerArray.getJSONObject(i); + //query 只有基本类型,不用循环判断下级 + //判断参数是否有值 + //获取对象下面的层级数据 + if (SysEnum.AUTHPORT.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//认证类型 + String query = querys.getString(SysEnum.EXAMPLE.getValue()); + if (query != null && !"".equals(query)) { + JSONArray example = JSONArray.parseArray(query); + String logValue = getObjectValue(loginData, example); + map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), logValue); + } + } else { + if (querys.getString(SysEnum.EXAMPLE.getValue()) != null && !"".equals(querys.getString(SysEnum.EXAMPLE.getValue()))) {//入参没有值用实例值,如果没有不添加 + if(map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null){ + map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), querys.getString(SysEnum.EXAMPLE.getValue())); + } + } else {//没有值直接拼接 + if(map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null){ + map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), ""); + } + } + } + } + } + } + return map; + + + + + } + + /** + * @param loginData + * @param example + * @return java.lang.String + * @Author lvleigang + * @Description 根据jsonArray 获取jsonobject中的值 + * @Date 11:47 上午 2023/8/31 + **/ + private String getObjectValue(JSONObject loginData, JSONArray example) { + String values = ""; + if (example != null && example.size() > 0) { + for (int i = 0; i < example.size(); i++) { + if (loginData.getString(example.getString(i)) != null && !"".equals(loginData.getString(example.getString(i)))) { + if (i == (example.size() - 1)) { + values = loginData.getString(example.getString(i)); + } else { + loginData = JSONObject.parseObject(loginData.getString(example.getString(i))); + } + } else { + return values; + } + } + } + return values; + } + private String getQuery(SysApplicationApiEntity loginApi,String sendDatastr,JSONObject loginData) { + Map map = new HashMap<>(); + if(sendDatastr != null){ + String[] parts = sendDatastr.split("&"); + if(parts != null && parts.length > 0){ + for (int i = 0; i < parts.length; i++) { + String[] part = parts[i].split("="); + if(part != null && part.length >=2 ){ + for (int a = 0; a < part.length; a++) { + map.put(part[0],part[1]); + } + } + } + } + } + if(loginData == null){ + loginData = new JSONObject(); + } + if (loginApi.getQueryIn() != null && !"".equals(loginApi.getQueryIn())) { + if (JSONUtil.isTypeJSONArray(loginApi.getQueryIn())) { + JSONArray headerArray = JSON.parseArray(loginApi.getQueryIn()); + for (int i = 0; i < headerArray.size(); i++) { + JSONObject querys = headerArray.getJSONObject(i); + //query 只有基本类型,不用循环判断下级 + //判断参数是否有值 + //获取对象下面的层级数据 + if (SysEnum.AUTHPORT.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//认证类型 + String query = querys.getString(SysEnum.EXAMPLE.getValue()); + if (query != null && !"".equals(query)) { + JSONArray example = JSONArray.parseArray(query); + String logValue = getObjectValue(loginData, example); + map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), logValue); + } + } else { + //不是认证类型直接取值 + if (querys.getString(SysEnum.EXAMPLE.getValue()) != null && !"".equals(querys.getString(SysEnum.EXAMPLE.getValue()))) {//入参没有值用实例值,如果没有不添加 + if(map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null){ + map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), querys.getString(SysEnum.EXAMPLE.getValue())); + } + } else {//没有值直接拼接 + if(map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null){ + map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), ""); + } + } + } + } + } + } + StringBuffer returnStr = new StringBuffer(); + if(map != null && map.size() > 0){ + for (String key : map.keySet()) { + if("".equals(returnStr)){ + returnStr.append(key).append("=").append(map.get(key)); + }else { + returnStr.append("&").append(key).append("=").append(map.get(key)); + } + } + } + return returnStr.toString(); + } }