Your Name
2023-05-08 3fe2aea7827eafc7513159db9a337fdf567584c8
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.java110.common.smartMeter.factory.zhongkong;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.common.charge.factory.dingding.DingdingChargeUtils;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.PayUtil;
import org.slf4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
 
public class WyRequestUtils {
    private static Logger logger = LoggerFactory.getLogger(WyRequestUtils.class);
 
    private static String secretKey = "Wr7nGOUMMjC0F8SzsGKAoRo8BpQ10nNk";
 
    /*public static void main(String[] args) {
        String bh="WY_001";
        String amount="100.00";
        Map<String,String> paramMap=new HashMap<>();
        paramMap.put("bh",bh);
        paramMap.put("amount",amount);
        System.out.println(getSign(paramMap));
        //d7ebdef32e1021b4c05320c4d634c1d7
    }*/
 
    /**
     * 使用 Map按key进行排序
     *
     * @param map
     * @return
     */
    private static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        //升序排序
        Map<String, String> sortMap = new TreeMap<>(String::compareTo);
        sortMap.putAll(map);
        return sortMap;
    }
 
    public static String getSign(Map<String, String> map) {
        //按Key进行排序
        Map<String, String> sortMap = sortMapByKey(map);
        StringBuilder signStr = new StringBuilder();
        if (sortMap != null) {
            for (Map.Entry<String, String> entry : sortMap.entrySet()) {
                signStr.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        String sign = signStr.append(secretKey).toString();
        return AuthenticationFactory.md5(sign);
    }
 
    /**
     * 请求充电桩 接口
     * <p>
     * MappingCache.getValue(DING_DING_DOMAIN, DING_DING_APP_ID),
     * MappingCache.getValue(DING_DING_DOMAIN, DING_DING_APP_SECURE
     *
     * @param url
     * @param reqMap
     * @param httpMethod
     * @return
     * @throws Exception
     */
    public static String execute(String url, Map<String, String> reqMap, HttpMethod httpMethod)  {
        RestTemplate outRestTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity httpEntity = null;
        if(reqMap != null) {
            reqMap.put("sign", getSign(reqMap));
            httpEntity = new HttpEntity(JSONObject.toJSONString(reqMap), httpHeaders);
        }else{
            httpEntity = new HttpEntity("", httpHeaders);
        }
        ResponseEntity<String> response = null;
        try {
            response = outRestTemplate.exchange(url, httpMethod, httpEntity, String.class);
        } catch (HttpStatusCodeException e) {
            logger.error("请求异常", e.getResponseBodyAsString());
            return e.getResponseBodyAsString();
        }
 
        return response.getBody();
    }
 
    /**
     * 请求充电桩 接口
     * <p>
     * MappingCache.getValue(DING_DING_DOMAIN, DING_DING_APP_ID),
     * MappingCache.getValue(DING_DING_DOMAIN, DING_DING_APP_SECURE
     *
     * @param url
     * @param reqMap
     * @param httpMethod
     * @return
     * @throws Exception
     */
    public static String executeReads(String url, Map<String, String> reqMap, HttpMethod httpMethod)  {
        RestTemplate outRestTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity httpEntity = null;
        if(reqMap != null) {
            //reqMap.put("sign", getSign(reqMap));
            httpEntity = new HttpEntity(JSONObject.toJSONString(reqMap), httpHeaders);
        }else{
            httpEntity = new HttpEntity("", httpHeaders);
        }
        ResponseEntity<String> response = null;
        try {
            response = outRestTemplate.exchange(url, httpMethod, httpEntity, String.class);
        } catch (HttpStatusCodeException e) {
            logger.error("请求异常", e.getResponseBodyAsString());
            return e.getResponseBodyAsString();
        }
 
        return response.getBody();
    }
}