package com.highdatas.mdm.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.highdatas.mdm.pojo.ActivitiBusinessType; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.http.*; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @ConfigurationProperties(prefix = "todo") @Component public class TodoClient { @Value("${todo.url}") String url; String prefix ; private TodoClient() { this.prefix = "/api/datacvg/activiti/"; } public boolean putRedisVal(String key, String value) { String url = this.url + prefix + "put"; Map params = new LinkedHashMap<>(); params.put("key",key); params.put("value",value); params.put("type",Constant.MainData); String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params); JSONObject result = (JSONObject) JSON.parse(s); String sucess = result.getString(Constant.Success); if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) { return false; }else { return true; } } public boolean open(String key, String businessId,String content, String userId, ActivitiBusinessType type) { String url = this.url + prefix + "open"; ArrayList> hashMaps = new ArrayList<>(); HashMap oneAct = new HashMap<>(); oneAct.put("approvalId", businessId); oneAct.put("approvalItem", content); hashMaps.add(oneAct); HashMap params = new LinkedHashMap<>(); params.put("functionName","MasActivitiServiceImpl"); params.put("approvalType","MAS"); params.put("businessId",businessId); params.put("expectValue","nobody"); params.put("userId",userId); params.put("businessType", type); params.put("approvalList", hashMaps); String json = HttpRestClient(url, HttpMethod.POST, params, null, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(json); String sucess = result.getString(Constant.Success); if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) { return false; }else { return true; } } public String HttpRestClient(String url, HttpMethod method, HashMap formParams, String getParams, MediaType mediaType) { if (!StringUtils.isEmpty(getParams)) { url = url + Constant.QUESTION + getParams; } SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(10*1000); requestFactory.setReadTimeout(10*1000); RestTemplate client = new RestTemplate(requestFactory); client.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); HttpHeaders headers = new HttpHeaders(); if (mediaType == null){ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); }else { headers.setContentType(mediaType); } String s = JSON.toJSONString(formParams); JSONObject result = (JSONObject) JSON.parse(s); HttpEntity requestEntity = new HttpEntity(result, headers); // 执行HTTP请求 ResponseEntity response = client.exchange(url, method, requestEntity, String.class); return response.getBody(); } }