xueyiming 6 months ago
parent
commit
f5107a0c18

+ 2 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/common/constant/RedisConstant.java

@@ -5,6 +5,8 @@ public interface RedisConstant {
     String PUSH_MESSAGE_TEXT = "push_message_text";
     String ACCESS_TOKEN = "ACCESS_TOKEN";
 
+    String WE_COM_ACCESS_TOKEN = "WE_COM_ACCESS_TOKEN";
+
     //小程序保底视频列表key
     String GUARANTEED_MINIPROGRAM_KEY = "guaranteed_miniprogram_list";
 }

+ 6 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/common/constant/WeComConstant.java

@@ -14,4 +14,10 @@ public interface WeComConstant {
     String POST_MESSAGE_PUSH_URL = "https://open.weibanzhushou.com/open-api/group_msg/add";
     //获取删除用户接口
     String GET_DELETE_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/list/outflow";
+
+    String GET_WE_COM_ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
+
+    String WE_COM_SECRET = "X13JWOI5ud_IsT2RBXGDZmknysLKqZfGdAO8eszA090";
+
+    String WE_COM_CROP_ID = "wwa4015dc7d652a21f";
 }

+ 37 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/controller/TestController.java

@@ -0,0 +1,37 @@
+package com.tzld.piaoquan.wecom.controller;
+
+import com.tzld.piaoquan.wecom.service.AccessTokenService;
+import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
+import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.IOException;
+
+@Slf4j
+@RestController
+@RequestMapping("/test")
+public class TestController {
+
+    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
+
+    @Autowired
+    private AccessTokenService accessTokenService;
+
+
+    @GetMapping("/ok")
+    public void test() throws IOException {
+        String weComAccessToken = accessTokenService.getWeComAccessToken();
+        log.info("weComAccessToken={}", weComAccessToken);
+        if (weComAccessToken == null) {
+            return;
+        }
+        String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_follow_user_list?access_token=" + weComAccessToken;
+        String s = httpPoolClientDefault.get(url);
+        log.info("getTest={}", s);
+
+    }
+}

+ 3 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/AccessTokenService.java

@@ -3,4 +3,7 @@ package com.tzld.piaoquan.wecom.service;
 public interface AccessTokenService {
 
     String getAccessToken();
+
+    String getWeComAccessToken();
+
 }

+ 23 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/AccessTokenServiceImpl.java

@@ -14,6 +14,7 @@ import java.io.IOException;
 import java.util.concurrent.TimeUnit;
 
 import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.ACCESS_TOKEN;
+import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.WE_COM_ACCESS_TOKEN;
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
 
 @Slf4j
@@ -46,4 +47,26 @@ public class AccessTokenServiceImpl implements AccessTokenService {
         }
         return "";
     }
+
+    @Override
+    public String getWeComAccessToken() {
+        String accessToken = (String) redisTemplate.opsForValue().get(ACCESS_TOKEN);
+        if (StringUtils.isNotEmpty(accessToken)) {
+            return accessToken;
+        }
+        JSONObject param = new JSONObject();
+        param.put("corp_id", CROP_ID);
+        param.put("secret", SECRET);
+        try {
+            String res = httpPoolClientDefault.get(String.format(GET_WE_COM_ACCESS_TOKEN_URL + "?corpid=%s&corpsecret=%s", WE_COM_CROP_ID, WE_COM_SECRET));
+            JSONObject jsonObject = JSONObject.parseObject(res);
+            Long expiresIn = jsonObject.getLong("expires_in");
+            String newAccessToken = jsonObject.getString("access_token");
+            redisTemplate.opsForValue().set(WE_COM_ACCESS_TOKEN, newAccessToken, expiresIn, TimeUnit.SECONDS);
+            return newAccessToken;
+        } catch (IOException e) {
+            log.error("getWeComAccessToken error", e);
+        }
+        return "";
+    }
 }