From 4ea9515d7096a60a047ce007f94d04b58c7c439c Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期二, 24 二月 2026 09:57:38 +0800
Subject: [PATCH] 代码提交
---
ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java | 115 ++++++++++++++++++++++++++++++++++-----------------------
1 files changed, 68 insertions(+), 47 deletions(-)
diff --git a/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java b/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java
index 9974eb6..17e8744 100644
--- a/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java
+++ b/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java
@@ -3,6 +3,16 @@
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.dingtalk.api.DefaultDingTalkClient;
+import com.dingtalk.api.DingTalkClient;
+import com.dingtalk.api.request.OapiGettokenRequest;
+import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
+import com.dingtalk.api.request.OapiUserListidRequest;
+import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
+import com.dingtalk.api.response.OapiGettokenResponse;
+import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
+import com.dingtalk.api.response.OapiUserListidResponse;
+import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.base.BaseException;
@@ -62,32 +72,37 @@
String accessToken = null;
try {
- accessToken = dingTalkProxyClient.getAccessToken();
+ accessToken = getAccessToken();
} catch (ApiException e) {
e.printStackTrace();
}
if (StringUtils.isNotEmpty(dingTalkReqVo.getNumber())) {
- // 浣跨敤浠g悊瀹㈡埛绔皟鐢ㄨ幏鍙栫敤鎴稩D鎺ュ彛
- Map<String, String> params = new HashMap<>();
- params.put("access_token", accessToken);
- params.put("mobile", dingTalkReqVo.getNumber());
+ DingTalkClient clientT = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
+ OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
+ req.setMobile(dingTalkReqVo.getNumber());
+ OapiV2UserGetbymobileResponse rsp = null;
try {
- body = dingTalkProxyClient.executeGet("/topapi/v2/user/getbymobile", params);
+ rsp = clientT.execute(req, accessToken);
+ body = rsp.getBody();
String jsonObject = JSONObject.parseObject(body).getJSONObject("result").get("userid").toString();
userIdlist.add(jsonObject);
+
} catch (ApiException e) {
e.printStackTrace();
}
} else {
- // 浣跨敤浠g悊瀹㈡埛绔皟鐢ㄨ幏鍙栭儴闂ㄧ敤鎴稩D鍒楄〃鎺ュ彛
- Map<String, String> params = new HashMap<>();
- params.put("access_token", accessToken);
- params.put("dept_id", String.valueOf(dingTalkReqVo.getDeptId()));
+ DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
+
+ OapiUserListidRequest request2 = new OapiUserListidRequest();
+ request2.setDeptId(dingTalkReqVo.getDeptId()); // 璁剧疆瑕佽幏鍙栫殑閮ㄩ棬 ID锛岃繖閲屼互鏍归儴闂ㄤ负渚�675909059L
+ // 璁剧疆閮ㄩ棬 ID锛屽鏋滆幏鍙栨墍鏈夊憳宸ユ墜鏈哄彿锛屽彲浠ヨ缃负鏍归儴闂ㄧ殑 ID
+ OapiUserListidResponse response2 = null;
try {
- body = dingTalkProxyClient.executeGet("/topapi/user/listid", params);
+ response2 = client3.execute(request2, accessToken);
} catch (ApiException e) {
e.printStackTrace();
}
+ body = response2.getBody();
// 瑙f瀽涓篔SONObject
JSONObject jsonObject = JSONObject.parseObject(body);
if (jsonObject != null) {
@@ -102,62 +117,60 @@
//userid鏁扮粍
for (String urid : userIdlist) {
- // 鏋勫缓鍙戦�佹秷鎭殑瀹屾暣JSON瀵硅薄锛岀鍚堥拤閽堿PI鏍煎紡
- Map<String, Object> messageData = new HashMap<>();
- messageData.put("userid_list", urid);
- messageData.put("agent_id", agentId);
- messageData.put("to_all_user", false);
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
+ OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
+ request.setUseridList(urid);
+ request.setAgentId(agentId);
+ request.setToAllUser(false);
//鍙戦�佸唴瀹瑰鐞�
List<ConcurrentHashMap<String, Object>> contents = dingTalkReqVo.getContents();
- List<Map<String, Object>> objects = new ArrayList<>();
+ ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>();
for (int i = 0; i < contents.size(); i++) {
ConcurrentHashMap<String, Object> map = contents.get(i);
for (Map.Entry<String, Object> entry : map.entrySet()) {
- Map<String, Object> formItem = new HashMap<>();
- formItem.put("key", entry.getKey());
+ OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form();
+ formSeverityDesc.setKey(entry.getKey());
if (entry.getKey().contains("瀹℃壒鏃堕棿")) {
Date date = (Date) entry.getValue();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = simpleDateFormat.format(date);
- formItem.put("value", format);
+ formSeverityDesc.setValue(format);
} else {
if (entry.getValue() != null) {
- formItem.put("value", entry.getValue().toString());
- } else {
- formItem.put("value", "");
+ formSeverityDesc.setValue(entry.getValue().toString());
}
}
- objects.add(formItem);
+ objects.add(formSeverityDesc);
}
}
- Map<String, Object> msg = new HashMap<>();
- Map<String, Object> oa = new HashMap<>();
- oa.put("message_url", dingTalkReqVo.getUrl());
- oa.put("pc_message_url", dingTalkReqVo.getUrl());
- Map<String, Object> head = new HashMap<>();
- head.put("bgcolor", "00409eff");
- oa.put("head", head);
- Map<String, Object> bodyContent = new HashMap<>();
- bodyContent.put("title", dingTalkReqVo.getTitle());
- bodyContent.put("form", objects);
- oa.put("body", bodyContent);
- msg.put("oa", oa);
- msg.put("msgtype", "oa");
- messageData.put("msg", msg);
+ OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
+ msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
+ msg.getOa().setMessageUrl(dingTalkReqVo.getUrl());
+ msg.getOa().setPcMessageUrl(dingTalkReqVo.getUrl());
+ msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
+ // msg.getOa().getHead().setText("浣犲湪璇村暐鍛㈢粧椤舵浛鑻涘ず闃挎柉钂傝姮闃挎柉钂傝姮");
+ msg.getOa().getHead().setBgcolor("00409eff");
+ msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
+ msg.getOa().getBody().setTitle(dingTalkReqVo.getTitle());
+ // msg.getOa().getBody().setContent("寰呭鐞�:" + "123" + "鏉�");
- // 灏哸ccess_token浣滀负URL鍙傛暟浼犻��
- String messageJson = JSONObject.toJSONString(messageData);
+ msg.getOa().getBody().setForm(objects);
+ msg.setMsgtype("oa");
+ request.setMsg(msg);
+ // log.info("鑾峰彇鍙戦�侀�氱煡娑堟伅浣撳拰鑾峰彇鍙戦�侀�氱煡浜哄畬鎴�");
+ OapiMessageCorpconversationAsyncsendV2Response response = null;
try {
- // 浣跨敤浠g悊瀹㈡埛绔彂閫丳OST璇锋眰
- body = dingTalkProxyClient.executePost("/topapi/message/corpconversation/asyncsend_v2?access_token=" + accessToken, messageJson);
- JSONObject response = JSONObject.parseObject(body);
- result = response.getInteger("errcode") == 0;
+ response = client.execute(request, accessToken);
} catch (ApiException e) {
- log.error("鍙戦�侀拤閽夋秷鎭け璐�: " + e.getErrMsg());
+ e.getErrMsg();
}
+ // log.info("娑堟伅浠诲姟ID"+response.getTaskId());
+ System.out.println(response.getTaskId());
+
+ result = response.isSuccess();
}
return result;
}
@@ -245,7 +258,15 @@
* 鑾峰彇 access_token
*/
private String getAccessToken() throws ApiException {
- return dingTalkProxyClient.getAccessToken();
+ DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
+ OapiGettokenRequest request = new OapiGettokenRequest();
+ //Appkey
+ request.setAppkey(dingAppid);
+ //Appsecret
+ request.setAppsecret(dingAppSecret);
+ /*璇锋眰鏂瑰紡*/
+ request.setHttpMethod("GET");
+ OapiGettokenResponse response = client.execute(request);
+ return response.getAccessToken();
}
-
}
--
Gitblit v1.9.3