chenbiao hace 2 años
padre
commit
d5c278a729

+ 2 - 0
netflix-common/src/main/java/com/cyksj/dto/RedisKey.java

@@ -22,6 +22,8 @@ public class RedisKey {
      */
     public static String CORP_ACCESS_TOKEN_KEY = "corp:access_token:";
 
+    public static String CORP_MSG_ACCESS_TOKEN_KEY = "corp::msg:access_token:";
+
     /**
      * corp_js_ticket
      */

+ 3 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/WxCorpApp.java

@@ -30,6 +30,9 @@ public class WxCorpApp {
     @TableField(value = "corpSecret")
     private String corpSecret;
 
+    @TableField(value = "msgSecret")
+    private String msgSecret;
+
     private String token;
 
     @TableField(value = "encodingAESKey")

+ 6 - 0
netflix-service/src/main/java/com/cyksj/service/corp/WxCorpOps.java

@@ -39,6 +39,12 @@ public interface WxCorpOps {
 	 */
 	String getAccessToken(String corpId) throws Exception;
 
+	/**
+	 * 通过企业微信id 获取消息accessstoken
+	 * @param corpId
+	 */
+	String getMsgAccessToken(String corpId) throws Exception;
+
 	/**
 	 * 获取企业标签库
 	 *

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

@@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.common.util.IoKit;
 import com.cyksj.common.util.J11HttpC;
@@ -84,6 +85,15 @@ public class WxCorpOpsImpl implements WxCorpOps {
 		}
 		return access_token(corpId);
 	}
+	@Override
+	public String getMsgAccessToken(String corpId) throws Exception {
+		corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
+		String accessToken = redisService.getStr(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId);
+		if (StringUtils.isNotBlank(accessToken)) {
+			return accessToken;
+		}
+		return getKfAccessToken(corpId);
+	}
 
 	@Override
 	public CorpUserExternalTagGroupList getCorpTagList(String corpId, List<String> tagIds, List<String> groupIds) throws Exception {
@@ -638,6 +648,38 @@ public class WxCorpOpsImpl implements WxCorpOps {
 		return accessToken;
 	}
 
+	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;
+	}
+
 	public String js_ticket(String corpId) throws Exception {
 		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=%s", this.getAccessToken(corpId));
 		HttpResponse<byte[]> res =
@@ -762,7 +804,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public List<String> getPermitUserList(String corpId, String type) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_permit_user_list?access_token=" + getAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_permit_user_list?access_token=" + getMsgAccessToken(corpId);
 
 		JSONObject params = new JSONObject();
 		if (StringUtils.isNotBlank(type)) {

+ 2 - 2
netflix-web/src/main/java/com/cyksj/web/controller/manage/corp/WxCorpController.java

@@ -275,7 +275,7 @@ public class WxCorpController {
 	 * @param externalUserid 用户 externalUserid
 	 */
 	@GetMapping("/getMsgAuditChat")
-	public Result<SearchResult<CorpQyMsgAuditChatView>> getMsgAuditChat(String externalUserid) {
+	public Result<List<CorpQyMsgAuditChatView>> getMsgAuditChat(String externalUserid) {
 		String followId = getCorpUuid();
 		List<CorpMsgFollowAssociation> corpMsgFollowAssociations = beanSearcher.searchAll(CorpMsgFollowAssociation.class, MapUtils.builder().field(CorpMsgFollowAssociation::getFollowId, followId).build());
 		List<String> followIds = corpMsgFollowAssociations.stream().map(CorpMsgFollowAssociation::getAssociationFollowId).collect(Collectors.toList());
@@ -283,7 +283,7 @@ public class WxCorpController {
 		if (StringUtils.isBlank(externalUserid)) {
 			throw BusinessRuntimeException.getInstance("缺少必填参数.");
 		}
-		SearchResult<CorpQyMsgAuditChatView> search = beanSearcher.search(CorpQyMsgAuditChatView.class, MapUtils.flatBuilder(request.getParameterMap())
+		List<CorpQyMsgAuditChatView> search = beanSearcher.searchAll(CorpQyMsgAuditChatView.class, MapUtils.flatBuilder(request.getParameterMap())
 				.field(CorpQyMsgAuditChatView::getExternalUserid, externalUserid)
 				.put("followId", followIds.stream()
 						.map(String::valueOf)