From bf2e8cf916b544ce0ba9327cfb7aeeb2cc7e691e Mon Sep 17 00:00:00 2001 From: liyawei Date: Thu, 10 Mar 2022 14:04:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=9E=E4=B9=A6=E6=B6=88=E6=81=AF=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../document/service/IFeishuService.java | 29 +++++ .../service/impl/FeishuServiceImpl.java | 114 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IFeishuService.java create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/FeishuServiceImpl.java diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IFeishuService.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IFeishuService.java new file mode 100644 index 000000000..9f18cf153 --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IFeishuService.java @@ -0,0 +1,29 @@ +package com.jero.modules.document.service; + +import java.io.IOException; + +/** + * @Author: liyawei + * @Description: + * @Date: Created in 13:40 2022/3/10 + */ +public interface IFeishuService { + + /** + * 获取token + * @return + * @throws IOException + */ + String getTenantAccessToken() throws IOException; + + /** + * 批量发送消息 + * @param userIds 用户域账号,对应数据库字段:third_id + * @param message 消息内容 + * @param title 消息类型 + * @param backUrl 消息内容中的跳转链接 + * @return + * @throws IOException + */ + String batchSendMessage(String[] userIds, String message, String title, String backUrl) throws IOException; +} diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/FeishuServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/FeishuServiceImpl.java new file mode 100644 index 000000000..23732c555 --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/FeishuServiceImpl.java @@ -0,0 +1,114 @@ +package com.jero.modules.document.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.jero.modules.document.service.IFeishuService; +import com.jero.modules.system.util.HttpRequestUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @Author: liyawei + * @Description: + * @Date: Created in 13:41 2022/3/10 + */ +@Slf4j +@Service +public class FeishuServiceImpl implements IFeishuService { + @Value("${Feishu.appId}") + private String appId; + + @Value("${Feishu.appSecret}") + private String appSecret; + + @Value("${Feishu.getTenantAccessTokenUrl}") + private String getTenantAccessTokenUrl; + + @Value("${Feishu.batchSendMessageUrl}") + private String batchSendMessageUrl; + + /** + * 获取token + * @return + * @throws IOException + */ + public String getTenantAccessToken() throws IOException { + Map headerMap = new HashMap<>(); + headerMap.put("Content-Type", "application/json; charset=utf-8"); + Map paramsMap = new HashMap<>(); + paramsMap.put("app_id", appId); + paramsMap.put("app_secret", appSecret); + String response = HttpRequestUtil.getResponseOfPOST(getTenantAccessTokenUrl, headerMap, JSON.toJSONString(paramsMap)); + //返回结果样例: {"code":0,"expire":7167,"msg":"ok","tenant_access_token":"t-3c05f981f36a8a92050718c6875e3c8b02c62304"} + JSONObject tokenJson = JSONObject.parseObject(response); + if(!tokenJson.get("code").toString().equals("0")) { + log.error("请求出现异常:" + tokenJson.get("msg") + " " + tokenJson); + return null; + } + String tenantAccessToken = tokenJson.get("tenant_access_token").toString(); + return tenantAccessToken; + } + + /** + * 批量发送消息 + * @param userIds 用户域账号,对应数据库字段:third_id + * @param message 消息内容 + * @param title 消息类型 + * @param backUrl 消息内容中的跳转链接 + * @return + * @throws IOException + */ + public String batchSendMessage(String[] userIds, String message, String title, String backUrl) throws IOException { + String token = getTenantAccessToken(); + // 设置请求头 + Map headerMap = new HashMap<>(); + headerMap.put("Authorization", "Bearer " + token); + headerMap.put("Content-Type", "application/json; charset=utf-8"); + // 设置消息标题 + JSONObject zh_cn = new JSONObject(); + zh_cn.put("title", title); + // 设置消息内容 + // 第一行 + JSONObject contentOne = new JSONObject(); + contentOne.put("tag", "text"); + contentOne.put("text", message); + // 第二行 + JSONObject contentTwo = new JSONObject(); + contentTwo.put("tag", "a"); + contentTwo.put("text", "跳转链接"); + contentTwo.put("href", backUrl); + List contentList = new ArrayList<>(); + contentList.add(contentOne); + contentList.add(contentTwo); + + List> list = new ArrayList<>(); + list.add(contentList); + zh_cn.put("content", list); + + JSONObject post = new JSONObject(); + post.put("zh_cn", zh_cn); + JSONObject contentObj = new JSONObject(); + contentObj.put("post", post); + // 设置请求参数 + JSONObject paramsMap = new JSONObject(); + paramsMap.put("user_ids", userIds); + paramsMap.put("msg_type", "post"); + paramsMap.put("content", contentObj); + // 发送请求给飞书 + String response = HttpRequestUtil.getResponseOfPOST(batchSendMessageUrl, headerMap, paramsMap.toJSONString()); + // 返回结果样例:{"code":0,"data":{"invalid_department_ids":[],"invalid_open_ids":[],"invalid_union_ids":[],"invalid_user_ids":[],"message_id":"bm-d595567e4f448fafe397bc916f0be646"},"msg":"ok"} + // 获取返回结果 + JSONObject tokenJson = JSONObject.parseObject(response); + if(!tokenJson.get("code").toString().equals("0")) { + log.error("请求出现异常:" + tokenJson.get("msg") + " " + tokenJson); + } + return tokenJson.get("msg").toString(); + } +}