Your Name vor 3 Jahren
Ursprung
Commit
fb22321d6c

+ 45
- 4
src/main/java/com/yunzhi/marketing/common/WorkWxUtils.java Datei anzeigen

@@ -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
 }

+ 2
- 1
src/main/java/com/yunzhi/marketing/controller/TaLiveActivityController.java Datei anzeigen

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

+ 2
- 2
src/main/resources/application-dev.yml Datei anzeigen

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