LiveCodeService.java
5.8 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cn.com.glsx.stoassistant.service;
import cn.com.glsx.stoassistant.config.GSCServicePoolConfig;
import cn.com.glsx.stoassistant.constans.GSCServiceConstants;
import cn.com.glsx.stoassistant.request.GroupAllocRequest;
import cn.com.glsx.stoassistant.request.LiveCodeRequest;
import cn.com.glsx.stoassistant.response.GroupAllocResponse;
import cn.com.glsx.stoassistant.response.LiveCodeResponse;
import cn.com.glsx.stoassistant.utils.SpringCloudHttpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 活码服务
*/
@Slf4j
@Service
public class LiveCodeService {
@Autowired
private GSCServicePoolConfig gscServicePoolConfig;
/**
* 获取群活码
*/
public LiveCodeResponse getLiveCode(LiveCodeRequest request) {
// 默认调用 gsc-vcp-pad-facade 服务
return getLiveCode(request, GSCServiceConstants.GSC_VCP_PAD_FACADE);
}
/**
* 获取群活码(支持指定服务)
*
* @param request 请求参数
* @param serviceName 服务名称(如 gsc-vcp-pad-facade、gsc-vcp-web-facade)
* @return 活码响应
*/
public LiveCodeResponse getLiveCode(LiveCodeRequest request, String serviceName) {
try {
// 获取指定服务的配置
GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(serviceName);
// 构建请求参数
Map<String, Object> params = new HashMap<>();
params.put("reportUuidId", request.getReportUuidId());
log.info("调用服务:{},请求参数:{}", serviceName, params);
// 调用指定服务的接口
String result = SpringCloudHttpUtil.post(
serviceParam,
params,
String.class,
"/liveCode/generate",
null
);
log.info("调用服务:{} 返回结果:{}", serviceName, result);
// 解析返回结果
Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
if (resultMap == null) {
throw new RuntimeException("调用服务失败:返回结果为空");
}
Integer code = (Integer) resultMap.get("code");
if (code == null || code != 0) {
String msg = (String) resultMap.get("msg");
throw new RuntimeException("调用服务失败:" + msg);
}
Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
if (data == null) {
throw new RuntimeException("调用服务失败:data为空");
}
LiveCodeResponse response = new LiveCodeResponse();
response.setH5Url((String) data.get("h5Url"));
response.setQrCodeUrl((String) data.get("qrCodeUrl"));
return response;
} catch (Exception e) {
log.error("获取群活码失败,服务:{}", serviceName, e);
throw new RuntimeException("获取群活码失败:" + e.getMessage(), e);
}
}
/**
* 扫码分配空闲企微群
*/
public GroupAllocResponse allocateGroup(GroupAllocRequest request) {
return allocateGroup(request, GSCServiceConstants.GSC_VCP_PAD_FACADE);
}
/**
* 扫码分配空闲企微群(支持指定服务)
*/
public GroupAllocResponse allocateGroup(GroupAllocRequest request, String serviceName) {
try {
GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(serviceName);
Map<String, Object> params = new HashMap<>();
params.put("liveCode", request.getLiveCode());
params.put("openid", request.getOpenid());
params.put("signature", request.getSignature());
params.put("exp", request.getExp());
params.put("nonce", request.getNonce());
params.put("saUserId", request.getSaUserId());
params.put("carConditionCode", request.getCarConditionCode());
log.info("调用服务:{},扫码分配群,请求参数:{}", serviceName, params);
String result = SpringCloudHttpUtil.post(
serviceParam,
params,
String.class,
"/liveCode/allocate",
null
);
log.info("调用服务:{} 扫码分配群返回结果:{}", serviceName, result);
Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
if (resultMap == null) {
throw new RuntimeException("调用服务失败:返回结果为空");
}
Integer code = (Integer) resultMap.get("code");
if (code == null || code != 0) {
String msg = (String) resultMap.get("msg");
throw new RuntimeException("调用服务失败:" + msg);
}
Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
if (data == null) {
throw new RuntimeException("调用服务失败:data为空");
}
GroupAllocResponse response = new GroupAllocResponse();
response.setQrCodeUrl((String) data.get("qrCodeUrl"));
response.setGroupName((String) data.get("groupName"));
response.setLockExpireTime((String) data.get("lockExpireTime"));
return response;
} catch (Exception e) {
log.error("扫码分配群失败,服务:{}", serviceName, e);
throw new RuntimeException("扫码分配群失败:" + e.getMessage(), e);
}
}
}