RopHttpUtil.java
5.29 KB
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
package cn.com.glsx.entmicro.utils;
import cn.com.glsx.entmicro.response.entmicromerchant.GroupItemResponse;
import cn.com.glsx.worktool.model.response.ScrmWorktoolGroupResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilder;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author zhouhaibao
* @version 1.0
* @description: http调用rop端服务
* @date 2021/7/30 11:26
*/
@Slf4j
public class RopHttpUtil {
private final static String appKey = "48e5e13229b82c1b4e6e8c96151f0637";
private final static String secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";
private static void addBaseParam(MultiValueMap<String, String> paramsMap, String method) {
paramsMap.add("v", "1.0.0");
paramsMap.add("method", method);
paramsMap.add("name", "zhouhb");
}
public static <T> T get(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
addBaseParam(paramsMap, method);
MultiValueMap<String, String> params = getSysParams();
params.putAll(paramsMap);
for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
params.remove(entry.getKey());
}
}
String sign = RopUtils.sign(params.toSingleValueMap(), secret);
params.add("sign", sign);
RestTemplate rest = new RestTemplate();
RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(baseurl);
uriTemplateHandler.setDefaultUriVariables(params.toSingleValueMap());
rest.setUriTemplateHandler(uriTemplateHandler);
T forObject = rest.getForObject(baseurl, responseType, params.toSingleValueMap());
return forObject;
}
public static <T> T post(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
addBaseParam(paramsMap, method);
MultiValueMap<String, String> params = getSysParams();
params.putAll(paramsMap);
for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
params.remove(entry.getKey());
}
}
String sign = RopUtils.sign(params.toSingleValueMap(), secret);
params.add("sign", sign);
return new RestTemplate().postForObject(baseurl, params, responseType);
}
public static MultiValueMap<String, String> getSysParams() {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
params.add("appKey", appKey);
params.add("format", "json");
return params;
}
/**
* 获取群列表
* @param requestUrl 请求URL
* @return 群列表
*/
public static List<ScrmWorktoolGroupResponse> getGroupList(String requestUrl) {
try {
RestTemplate restTemplate = new RestTemplate();
log.info("获取群列表,请求URL: {}", requestUrl);
// 发送GET请求获取群列表
GroupItemResponse[] groupItems = restTemplate.getForObject(requestUrl, GroupItemResponse[].class);
if (groupItems == null || groupItems.length == 0) {
log.warn("获取群列表为空");
return new ArrayList<>();
}
List<ScrmWorktoolGroupResponse> result = new ArrayList<>();
for (GroupItemResponse item : groupItems) {
ScrmWorktoolGroupResponse response = new ScrmWorktoolGroupResponse();
response.setId(null);
response.setRootId(null);
response.setDelFlag(0);
response.setGroupName(item.getGroupName());
String groupRemark = item.getGroupRemark();
response.setGroupRemark(groupRemark);
result.add(response);
}
log.info("获取群列表成功,共{}个群", result.size());
return result;
} catch (Exception e) {
log.error("获取群列表异常,URL: {}", requestUrl, e);
throw new RuntimeException("获取群列表失败: " + e.getMessage(), e);
}
}
static class RopUriTemplateHandler extends DefaultUriBuilderFactory {
public RopUriTemplateHandler(String baseUriTemplate) {
super(baseUriTemplate);
}
@Override
public URI expand(String uriTemplate, Map<String, ?> uriVars) {
UriBuilder builder = uriString(uriTemplate);
if (uriVars != null && !uriVars.isEmpty()) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
for (Entry<String, ?> entry : uriVars.entrySet()) {
params.add(entry.getKey(), entry.getValue().toString());
}
builder.queryParams(params);
}
return builder.build(uriVars);
}
}
}