|
@@ -1,5 +1,6 @@
|
1
|
1
|
package com.yunzhi.marketing.common;
|
2
|
2
|
|
|
3
|
+import com.alibaba.fastjson.JSONArray;
|
3
|
4
|
import com.alibaba.fastjson.JSONObject;
|
4
|
5
|
import com.yunzhi.marketing.config.WorkConfig;
|
5
|
6
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -29,19 +30,53 @@ public class WorkWxUtils {
|
29
|
30
|
wxCpService.setWxCpConfigStorage(config);
|
30
|
31
|
}
|
31
|
32
|
|
|
33
|
+ /**
|
|
34
|
+ * 获取房间ID
|
|
35
|
+ * @param userId
|
|
36
|
+ * @return
|
|
37
|
+ * @throws Exception
|
|
38
|
+ */
|
|
39
|
+ public String getLivingId(String userId) throws Exception {
|
|
40
|
+ if (StringUtils.isEmpty(userId)) {
|
|
41
|
+ throw new Exception("未指定用户ID");
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ try {
|
|
45
|
+ JSONObject params = new JSONObject();
|
|
46
|
+ params.put("userid", userId);
|
|
47
|
+ params.put("limit", 10);
|
|
48
|
+ JSONObject result = post("/cgi-bin/living/get_user_all_livingid", params);
|
|
49
|
+ JSONArray lst = result.getJSONArray("livingid_list");
|
|
50
|
+ return lst.size() > 0 ? (String) lst.get(0) : null;
|
|
51
|
+ } catch (WxErrorException e) {
|
|
52
|
+ if (90601 == e.getError().getErrorCode()) {
|
|
53
|
+ throw new Exception("直播房间不存在");
|
|
54
|
+ } else {
|
|
55
|
+ throw new Exception(String.format("获取直播凭证失败: %s", e.getError().getErrorMsg()));
|
|
56
|
+ }
|
|
57
|
+ } catch (Exception e) {
|
|
58
|
+ e.printStackTrace();
|
|
59
|
+ throw e;
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ /**
|
|
64
|
+ * 获取直播授权码
|
|
65
|
+ * @param livingid
|
|
66
|
+ * @param openid
|
|
67
|
+ * @return
|
|
68
|
+ * @throws Exception
|
|
69
|
+ */
|
32
|
70
|
public String getLivingCode(String livingid, String openid) throws Exception {
|
33
|
71
|
if (StringUtils.isEmpty(livingid) || StringUtils.isEmpty(openid)) {
|
34
|
72
|
throw new Exception("房间号或者人员openid不能为空");
|
35
|
73
|
}
|
36
|
74
|
|
37
|
75
|
try {
|
38
|
|
- String url = wxCpService.getWxCpConfigStorage().getApiUrl("/cgi-bin/living/get_living_code");
|
39
|
76
|
JSONObject params = new JSONObject();
|
40
|
77
|
params.put("livingid", livingid);
|
41
|
78
|
params.put("openid", openid);
|
42
|
|
-
|
43
|
|
- String response = wxCpService.post(url, params.toJSONString());
|
44
|
|
- JSONObject result = JSONObject.parseObject(response);
|
|
79
|
+ JSONObject result = post("/cgi-bin/living/get_living_code", params);
|
45
|
80
|
return result.getString("living_code");
|
46
|
81
|
} catch (WxErrorException e) {
|
47
|
82
|
if (90601 == e.getError().getErrorCode()) {
|
|
@@ -54,4 +89,10 @@ public class WorkWxUtils {
|
54
|
89
|
throw e;
|
55
|
90
|
}
|
56
|
91
|
}
|
|
92
|
+
|
|
93
|
+ private JSONObject post(String url, JSONObject data) throws WxErrorException {
|
|
94
|
+ String uri = wxCpService.getWxCpConfigStorage().getApiUrl(url);
|
|
95
|
+ String response = wxCpService.post(uri, data.toJSONString());
|
|
96
|
+ return JSONObject.parseObject(response);
|
|
97
|
+ }
|
57
|
98
|
}
|