Your Name 3 년 전
부모
커밋
fb22321d6c

+ 45
- 4
src/main/java/com/yunzhi/marketing/common/WorkWxUtils.java 파일 보기

1
 package com.yunzhi.marketing.common;
1
 package com.yunzhi.marketing.common;
2
 
2
 
3
+import com.alibaba.fastjson.JSONArray;
3
 import com.alibaba.fastjson.JSONObject;
4
 import com.alibaba.fastjson.JSONObject;
4
 import com.yunzhi.marketing.config.WorkConfig;
5
 import com.yunzhi.marketing.config.WorkConfig;
5
 import lombok.extern.slf4j.Slf4j;
6
 import lombok.extern.slf4j.Slf4j;
29
         wxCpService.setWxCpConfigStorage(config);
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
     public String getLivingCode(String livingid, String openid) throws Exception {
70
     public String getLivingCode(String livingid, String openid) throws Exception {
33
         if (StringUtils.isEmpty(livingid) || StringUtils.isEmpty(openid)) {
71
         if (StringUtils.isEmpty(livingid) || StringUtils.isEmpty(openid)) {
34
             throw new Exception("房间号或者人员openid不能为空");
72
             throw new Exception("房间号或者人员openid不能为空");
35
         }
73
         }
36
 
74
 
37
         try {
75
         try {
38
-            String url = wxCpService.getWxCpConfigStorage().getApiUrl("/cgi-bin/living/get_living_code");
39
             JSONObject params = new JSONObject();
76
             JSONObject params = new JSONObject();
40
             params.put("livingid", livingid);
77
             params.put("livingid", livingid);
41
             params.put("openid", openid);
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
             return result.getString("living_code");
80
             return result.getString("living_code");
46
         } catch (WxErrorException e) {
81
         } catch (WxErrorException e) {
47
             if (90601 == e.getError().getErrorCode()) {
82
             if (90601 == e.getError().getErrorCode()) {
54
             throw e;
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
 }

+ 2
- 1
src/main/java/com/yunzhi/marketing/controller/TaLiveActivityController.java 파일 보기

310
                                           HttpServletRequest request) {
310
                                           HttpServletRequest request) {
311
         String openid = getOpenId(request);
311
         String openid = getOpenId(request);
312
         try {
312
         try {
313
-            String livingCode = workWxUtils.getLivingCode(livingid, openid);
313
+            String id = workWxUtils.getLivingId(livingid);
314
+            String livingCode = workWxUtils.getLivingCode(id, openid);
314
             return ResponseBean.success(livingCode);
315
             return ResponseBean.success(livingCode);
315
         } catch (Exception e) {
316
         } catch (Exception e) {
316
             return ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
317
             return ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);

+ 2
- 2
src/main/resources/application-dev.yml 파일 보기

27
 # 企业微信
27
 # 企业微信
28
 workwx:
28
 workwx:
29
   corpid: wwf5f7889b49bb2510
29
   corpid: wwf5f7889b49bb2510
30
-  agentid: 1000006
31
-  secret: qUa67wHPwyFrqoI00ti4gc4q7XLuYhvdok3ERDKuKW8
30
+  agentid: 3010097
31
+  secret: 0zKXGe3lmIoAfedoeLV_6DK96PM-Sg_c79rHqpSlvDs