zoujiajian před 1 rokem
rodič
revize
bf0c4a4ef4

+ 33 - 1
netflix-service/src/main/java/com/cyksj/service/corp/impl/WxCorpOpsImpl.java

@@ -96,7 +96,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 		if (StringUtils.isNotBlank(accessToken)) {
 			return accessToken;
 		}
-		return getAccessToken(corpId);
+		return getKfAccessToken(corpId);
 	}
 
 	@Override
@@ -799,6 +799,38 @@ public class WxCorpOpsImpl implements WxCorpOps {
 		return JSONUtil.parseObj(res.body()).getJSONArray("ids").toList(String.class);
 	}
 
+	public String getKfAccessToken(String corpId) throws Exception {
+		WxCorpApp wxCorpApp = WeChatCorpFactory.getCorpConfigStrategy(corpId);
+		if (StringUtils.isBlank(wxCorpApp.getMsgSecret())) {
+			throw BusinessRuntimeException.getInstance("该企业会话存档权限尚未开通.请联系管理员配置api secret");
+		}
+		String accessToken = redisService.getStr(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId);
+		if(StringUtils.isBlank(accessToken)){
+			String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" +
+					"?corpid=" + corpId +"&corpsecret=" + wxCorpApp.getMsgSecret();
+
+			HttpResponse<String> res =
+					J11HttpC.custom()
+							.ofGet()
+							.url(url)
+							.send(HttpResponse.BodyHandlers.ofString());
+
+			if (200 != res.statusCode()) {
+				throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + res.body());
+			}
+			JSONObject result = new JSONObject(res.body());
+
+			final Integer errcode = result.getInt("errcode");
+			if (null != errcode && 0 != errcode) {
+				throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + result.getStr("errmsg") + "[" + errcode + "]");
+			}
+			accessToken = result.getStr("access_token");
+			redisService.set(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId, accessToken, result.getInt("expires_in") - 300L);
+			return accessToken;
+		}
+		return accessToken;
+	}
+
 	@Override
 	public WxCpKfAccountAddResp addAccount(String corpId, WxCpKfAccountAdd add) throws Exception {
 		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/add?access_token=" + getAccessToken(corpId);