From 9958f44867a201bb14b3efd3f4506c9714937ea6 Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期三, 12 十一月 2025 15:30:22 +0800
Subject: [PATCH] 添加钉钉推送消息的接口
---
src/main/java/com/gs/xky/service/DingtalkInfoService.java | 11 ++
src/main/java/com/gs/xky/service/Impl/DingtalkInfoServiceImpl.java | 222 ++++++++++++++++++++++++++++++++++++++++++++
src/test/java/com/gs/xky/XkyApplicationTests.java | 67 +++++++++++-
3 files changed, 290 insertions(+), 10 deletions(-)
diff --git a/src/main/java/com/gs/xky/service/DingtalkInfoService.java b/src/main/java/com/gs/xky/service/DingtalkInfoService.java
index a397591..a7a0f7e 100644
--- a/src/main/java/com/gs/xky/service/DingtalkInfoService.java
+++ b/src/main/java/com/gs/xky/service/DingtalkInfoService.java
@@ -11,4 +11,15 @@
public interface DingtalkInfoService extends IService<DingtalkInfo> {
boolean sendMessage(String releaseNo);
+
+ boolean sendActionCardMessage() throws Exception;
+
+ /**
+ * 鍙戦�佹枃浠舵秷鎭�
+ *
+ * @param filePath 鏈湴鏂囦欢璺緞
+ * @return 鏄惁鍙戦�佹垚鍔�
+ * @throws Exception 寮傚父
+ */
+ boolean sendFileMessage(String filePath) throws Exception;
}
diff --git a/src/main/java/com/gs/xky/service/Impl/DingtalkInfoServiceImpl.java b/src/main/java/com/gs/xky/service/Impl/DingtalkInfoServiceImpl.java
index 68af584..1121b42 100644
--- a/src/main/java/com/gs/xky/service/Impl/DingtalkInfoServiceImpl.java
+++ b/src/main/java/com/gs/xky/service/Impl/DingtalkInfoServiceImpl.java
@@ -14,11 +14,13 @@
import com.gs.xky.mapper.MesStaffMapper;
import com.gs.xky.service.DingtalkInfoService;
import com.gs.xky.service.SimpleExample;
+import com.taobao.api.FileItem;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
+import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@@ -130,6 +132,95 @@
}
}
+ @Override
+ public boolean sendActionCardMessage() {
+ try {
+ // 1. 鑾峰彇闇�瑕佹帹閫佺殑鐢ㄦ埛鍒楄〃锛堝彲浠ユ槸鍥哄畾鎺ㄩ�佺敤鎴凤級
+ List<DingtalkInfo> fixedList = baseMapper.selectList(
+ new LambdaQueryWrapper<DingtalkInfo>().eq(DingtalkInfo::getIsHead, 1)
+ );
+
+ List<Long> sidList = fixedList.stream()
+ .map(DingtalkInfo::getSid)
+ .collect(Collectors.toList());
+
+ List<String> userIdList = getDingtalkUserIdListBySids(sidList);
+
+ if (userIdList == null || userIdList.isEmpty()) {
+ log.warn("娌℃湁闇�瑕佸彂閫侀拤閽夋秷鎭殑鐢ㄦ埛");
+ return false;
+ }
+
+ String userIdListStr = String.join(",", userIdList);
+
+ // 2. 鏋勫缓娑堟伅鍐呭
+ String title = "鐢熶骇鏁版嵁鐪嬫澘";
+ String markdown = "璇风偣鍑讳笅鏂规寜閽煡鐪嬭缁咮I鎶ヨ〃";
+ String singleTitle = "鏌ョ湅鎶ヨ〃";
+ String singleUrl = "http://192.168.1.22:8081/design?fid=rpte6045ab079b211f0824bd3cfd50c6b93&fserid=4b198960bedd11f09f6f792bfe147b64&fsharetype=3";
+
+ // 3. 鍙戦�佹秷鎭�
+ OapiMessageCorpconversationAsyncsendV2Response rsp =
+ sendActionCardMessage(userIdListStr, title, markdown, singleTitle, singleUrl);
+
+ log.info("鎴愬姛鍙戦�丄ctionCard娑堟伅: {}", rsp.getBody());
+ return true;
+ } catch (Exception e) {
+ log.error("鍙戦�丄ctionCard娑堟伅澶辫触", e);
+ return false;
+ }
+ }
+
+ @Override
+ public boolean sendFileMessage(String filePath) throws Exception {
+ try {
+ // 1. 妫�鏌ユ枃浠舵槸鍚﹀瓨鍦�
+ File file = new File(filePath);
+ if (!file.exists()) {
+ log.error("鏂囦欢涓嶅瓨鍦�: {}", filePath);
+ return false;
+ }
+
+ // 2. 鑾峰彇闇�瑕佹帹閫佺殑鐢ㄦ埛鍒楄〃锛堝彲鏍规嵁瀹為檯闇�姹傝皟鏁达紝杩欓噷浣跨敤isHead=1鐨勭敤鎴凤級
+ List<DingtalkInfo> fixedList = baseMapper.selectList(
+ new LambdaQueryWrapper<DingtalkInfo>().eq(DingtalkInfo::getIsHead, 1)
+ );
+
+ if (fixedList == null || fixedList.isEmpty()) {
+ log.warn("娌℃湁闇�瑕佸彂閫佹枃浠剁殑鐢ㄦ埛锛坕sHead=1锛�");
+ return false;
+ }
+
+ List<Long> sidList = fixedList.stream()
+ .map(DingtalkInfo::getSid)
+ .collect(Collectors.toList());
+
+ List<String> userIdList = getDingtalkUserIdListBySids(sidList);
+
+ if (userIdList == null || userIdList.isEmpty()) {
+ log.warn("娌℃湁鏈夋晥鐨勯拤閽夌敤鎴稩D");
+ return false;
+ }
+
+ String userIdListStr = String.join(",", userIdList);
+
+ // 3. 涓婁紶鏂囦欢鍒伴拤閽夋湇鍔″櫒
+ log.info("寮�濮嬩笂浼犳枃浠�: {}", filePath);
+ String mediaId = uploadMedia(filePath, "file");
+
+ // 4. 鍙戦�佹枃浠舵秷鎭�
+ log.info("寮�濮嬪彂閫佹枃浠舵秷鎭紝mediaId: {}", mediaId);
+ OapiMessageCorpconversationAsyncsendV2Response response = sendFileMessageByMediaId(userIdListStr, mediaId);
+
+ log.info("鏂囦欢娑堟伅鍙戦�佸搷搴�: {}", response.getBody());
+ return response.getErrcode() == 0;
+
+ } catch (Exception e) {
+ log.error("鍙戦�佹枃浠舵秷鎭け璐�", e);
+ throw e;
+ }
+ }
+
/**
* 鏍规嵁鎸囧畾鐨剆id鍒楄〃鑾峰彇閽夐拤鐢ㄦ埛ID鍒楄〃
*
@@ -200,7 +291,7 @@
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
- request.setAgentId(3917187842L);
+ request.setAgentId(4104598880L);
request.setUseridList(userIdListStr);
request.setToAllUser(false);
@@ -215,6 +306,135 @@
}
/**
+ * 鍙戦�乴ink娑堟伅锛堝湪閽夐拤鍐呯疆娴忚鍣ㄤ腑鎵撳紑锛�
+ *
+ * @param userIdListStr 鐢ㄦ埛ID鍒楄〃锛岄�楀彿鍒嗛殧
+ * @param title 娑堟伅鏍囬
+ * @param text 娑堟伅鍐呭
+ * @param messageUrl 鐐瑰嚮娑堟伅鍚庤烦杞殑URL
+ * @param picUrl 鍥剧墖URL锛堝彲閫夛級
+ * @return 鍝嶅簲缁撴灉
+ * @throws Exception 寮傚父
+ */
+ private OapiMessageCorpconversationAsyncsendV2Response sendLinkMessage(String userIdListStr, String title, String text, String messageUrl, String picUrl) throws Exception {
+
+ String accessToken = simpleExample.getAccessToken();
+
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
+ OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
+ request.setAgentId(4104598880L);
+ request.setUseridList(userIdListStr);
+ request.setToAllUser(false);
+
+ OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
+ msg.setMsgtype("link");
+ msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
+ msg.getLink().setTitle(title);
+ msg.getLink().setText(text);
+ msg.getLink().setMessageUrl(messageUrl);
+ if (StringUtils.hasText(picUrl)) {
+ msg.getLink().setPicUrl(picUrl);
+ }
+ request.setMsg(msg);
+
+ return client.execute(request, accessToken);
+
+ }
+
+ /**
+ * 鍙戦�丄ctionCard娑堟伅锛堝湪澶栭儴娴忚鍣ㄤ腑鎵撳紑閾炬帴锛岄�傚悎BI绛夊閮ㄧ郴缁燂級
+ *
+ * @param userIdListStr 鐢ㄦ埛ID鍒楄〃锛岄�楀彿鍒嗛殧
+ * @param title 娑堟伅鏍囬
+ * @param markdown 娑堟伅鍐呭锛堟敮鎸丮arkdown鏍煎紡锛�
+ * @param singleTitle 鎸夐挳鏂囧瓧锛屼緥濡傦細"鏌ョ湅璇︽儏"
+ * @param singleUrl 鐐瑰嚮鎸夐挳鍚庤烦杞殑URL锛堝閮ㄩ摼鎺ワ級
+ * @return 鍝嶅簲缁撴灉
+ * @throws Exception 寮傚父
+ */
+ private OapiMessageCorpconversationAsyncsendV2Response sendActionCardMessage(String userIdListStr, String title, String markdown, String singleTitle, String singleUrl) throws Exception {
+
+ String accessToken = simpleExample.getAccessToken();
+
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
+ OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
+ request.setAgentId(4104598880L);
+ request.setUseridList(userIdListStr);
+ request.setToAllUser(false);
+
+ OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
+ msg.setMsgtype("action_card");
+ msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
+ msg.getActionCard().setTitle(title);
+ msg.getActionCard().setMarkdown(markdown);
+ msg.getActionCard().setSingleTitle(singleTitle);
+ msg.getActionCard().setSingleUrl(singleUrl);
+ request.setMsg(msg);
+
+ return client.execute(request, accessToken);
+
+ }
+
+ /**
+ * 涓婁紶鏂囦欢鍒伴拤閽夋湇鍔″櫒锛岃幏鍙杕edia_id
+ *
+ * @param filePath 鏈湴鏂囦欢璺緞
+ * @param fileType 鏂囦欢绫诲瀷锛歠ile(鏅�氭枃浠�), voice(璇煶鏂囦欢), video(瑙嗛鏂囦欢), image(鍥剧墖鏂囦欢)
+ * @return media_id
+ * @throws Exception 寮傚父
+ */
+ private String uploadMedia(String filePath, String fileType) throws Exception {
+ String accessToken = simpleExample.getAccessToken();
+
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/media/upload");
+ com.dingtalk.api.request.OapiMediaUploadRequest request = new com.dingtalk.api.request.OapiMediaUploadRequest();
+ request.setType(fileType);
+
+ // 浣跨敤 FileItem 鍖呰鏂囦欢
+ File file = new File(filePath);
+ FileItem fileItem = new FileItem(file);
+ request.setMedia(fileItem);
+
+ com.dingtalk.api.response.OapiMediaUploadResponse response = client.execute(request, accessToken);
+
+ if (response.getErrcode() == 0) {
+ log.info("鏂囦欢涓婁紶鎴愬姛锛宮edia_id: {}", response.getMediaId());
+ return response.getMediaId();
+ } else {
+ log.error("鏂囦欢涓婁紶澶辫触锛岄敊璇爜: {}, 閿欒淇℃伅: {}", response.getErrcode(), response.getErrmsg());
+ throw new Exception("鏂囦欢涓婁紶澶辫触: " + response.getErrmsg());
+ }
+ }
+
+ /**
+ * 閫氳繃media_id鍙戦�佹枃浠舵秷鎭�
+ *
+ * @param userIdListStr 鐢ㄦ埛ID鍒楄〃锛岄�楀彿鍒嗛殧
+ * @param mediaId 鏂囦欢鐨刴edia_id锛堥�氳繃uploadMedia鏂规硶鑾峰彇锛�
+ * @return 鍝嶅簲缁撴灉
+ * @throws Exception 寮傚父
+ */
+ private OapiMessageCorpconversationAsyncsendV2Response sendFileMessageByMediaId(String userIdListStr, String mediaId) throws Exception {
+
+ String accessToken = simpleExample.getAccessToken();
+
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
+ OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
+ request.setAgentId(4104598880L);
+ request.setUseridList(userIdListStr);
+ request.setToAllUser(false);
+
+ OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
+ msg.setMsgtype("file");
+ msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
+ msg.getFile().setMediaId(mediaId);
+ request.setMsg(msg);
+
+ return client.execute(request, accessToken);
+
+ }
+
+ /**
* 鑾峰彇isSendDingtalk=1鐨勭敤鎴风殑閽夐拤鐢ㄦ埛ID鍒楄〃锛堜繚鐣欏師鏈夋柟娉曪紝鍚戝悗鍏煎锛�
*/
private List<String> getDingtalkUserIdList() {
diff --git a/src/test/java/com/gs/xky/XkyApplicationTests.java b/src/test/java/com/gs/xky/XkyApplicationTests.java
index 123a12a..4571852 100644
--- a/src/test/java/com/gs/xky/XkyApplicationTests.java
+++ b/src/test/java/com/gs/xky/XkyApplicationTests.java
@@ -1,11 +1,9 @@
package com.gs.xky;
import com.alibaba.fastjson.JSON;
-import com.gs.xky.config.ApiResponse;
-import com.gs.xky.config.BodyParam;
-import com.gs.xky.config.DataAcquisitionConfiguration;
-import com.gs.xky.config.XkyCommonParam;
+import com.gs.xky.config.*;
import com.gs.xky.dto.BarcodeDeliveryNo;
+import com.gs.xky.dto.EmployeeInfo;
import com.gs.xky.dto.XkyDetail;
import com.gs.xky.dto.XkyEntity;
import com.gs.xky.entity.MesInvItemArn;
@@ -16,6 +14,7 @@
import java.io.IOException;
import java.util.List;
+import java.util.stream.Collectors;
@SpringBootTest
class XkyApplicationTests {
@@ -160,12 +159,62 @@
@Test
void cs3() throws Exception {
-// dingtalkInfoService.sendMessage("CGJY20250412166");
+ DingTalkParam dingTalkParam = new DingTalkParam(1);
+ DingTalkResponse<EmployeeInfo> employeeInfoDingTalkResponse =
+ apiService.sendListRequest(dingTalkParam, EmployeeInfo.class,
+ "http://192.168.1.64/eHR/eHRExternalService/Service.ashx");
-// XkyEntity xkyEntity = new XkyEntity();
-// xkyEntity.setDeliveryNo("222505057541");
-// xkyService.remove1(xkyEntity);
+ List<EmployeeInfo> collect = employeeInfoDingTalkResponse.getData().stream()
+ .filter(s -> "閫犳ⅵ鑰咃紙娴欐睙锛夌鎶�鏈夐檺鍏徃".equals(s.getCUnitName()))
+ .collect(Collectors.toList());
- deliveryNoticeService.setDeliveryNotice();
+ }
+
+ /**
+ * 娴嬭瘯鍙戦�丄ctionCard娑堟伅锛堝湪澶栭儴娴忚鍣ㄤ腑鎵撳紑閾炬帴锛�
+ * 閫傜敤鍦烘櫙锛欱I鎶ヨ〃銆佸閮ㄧ郴缁熼摼鎺ョ瓑闇�瑕佸湪澶栭儴娴忚鍣ㄦ墦寮�鐨勫満鏅�
+ */
+ @Test
+ void testSendActionCardMessage() throws Exception {
+ System.out.println("=== 寮�濮嬫祴璇曞彂閫丄ctionCard娑堟伅 ===");
+
+ boolean result = dingtalkInfoService.sendActionCardMessage();
+
+ if (result) {
+ System.out.println("鉁� ActionCard娑堟伅鍙戦�佹垚鍔�");
+ } else {
+ System.out.println("鉁� ActionCard娑堟伅鍙戦�佸け璐�");
+ }
+
+ System.out.println("=== 娴嬭瘯缁撴潫 ===");
+ }
+
+ /**
+ * 娴嬭瘯鍙戦�佹枃浠舵秷鎭�
+ * 閫傜敤鍦烘櫙锛氬彂閫丒xcel鎶ヨ〃銆丳DF鏂囨。銆乄ord鏂囨。绛夊悇绫绘枃浠�
+ */
+ @Test
+ void testSendFileMessage() throws Exception {
+ System.out.println("=== 寮�濮嬫祴璇曞彂閫佹枃浠舵秷鎭� ===");
+
+ // 鎸囧畾瑕佸彂閫佺殑鏂囦欢璺緞锛堣淇敼涓哄疄闄呭瓨鍦ㄧ殑鏂囦欢璺緞锛�
+ String filePath = "D:\\test\\report.xlsx"; // 绀轰緥锛欵xcel鎶ヨ〃鏂囦欢
+ // String filePath = "D:\\test\\document.pdf"; // 绀轰緥锛歅DF鏂囨。
+ // String filePath = "D:\\test\\data.docx"; // 绀轰緥锛歐ord鏂囨。
+
+ try {
+ boolean result = dingtalkInfoService.sendFileMessage(filePath);
+
+ if (result) {
+ System.out.println("鉁� 鏂囦欢娑堟伅鍙戦�佹垚鍔�");
+ } else {
+ System.out.println("鉁� 鏂囦欢娑堟伅鍙戦�佸け璐�");
+ }
+ } catch (Exception e) {
+ System.out.println("鉁� 鍙戦�佹枃浠舵椂鍙戠敓寮傚父: " + e.getMessage());
+ e.printStackTrace();
+ }
+
+ System.out.println("=== 娴嬭瘯缁撴潫 ===");
}
}
\ No newline at end of file
--
Gitblit v1.9.3