kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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<String, String> 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<HashMap<String, String>> hashMaps = new ArrayList<>();
        HashMap<String, String> oneAct = new HashMap<>();
        oneAct.put("approvalId", businessId);
        oneAct.put("approvalItem", content);
        hashMaps.add(oneAct);
        HashMap<String, Object> 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<String, Object> 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<JSONObject> requestEntity = new HttpEntity<JSONObject>(result, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
 
        return response.getBody();
    }
}