feat:first commit

This commit is contained in:
Zhujx
2025-06-06 16:01:37 +08:00
commit ec437bb088
55 changed files with 2513 additions and 0 deletions

52
facade/pom.xml Normal file
View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xiang</groupId>
<artifactId>xservice-scirpt</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>facade</artifactId>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- &lt;!&ndash; 新版钉钉SDK &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>dingtalk</artifactId>-->
<!-- <version>2.2.16</version>-->
<!-- </dependency>-->
<!-- 旧版的钉钉SDK-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 生成可执行的jar的名字xxx-exec.jar -->
<!-- 不固定写成abcd都可以 -->
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,30 @@
package com.xiang.dingTalk.common.enums;
import lombok.Getter;
/**
* @Author: xiang
* @Date: 2025-05-09 15:54
*/
@Getter
public enum DingTalkUrlEnum {
/**
* 钉钉接口枚举
*/
DING_TALK_GET_ENTERPRISE_INTER_TOKEN("https://oapi.dingtalk.com/gettoken", "获取企业内部应用Token"),
DING_TALK_ASYNC_SEND_MESSAGE("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2", "异步发送工作通知"),
DING_TALK_CHAR_MESSAGE("https://oapi.dingtalk.com/chat/send", "发送消息到企业群旧版SDK"),
;
final String url;
final String desc;
DingTalkUrlEnum(String url, String desc) {
this.url = url;
this.desc = desc;
}
}

View File

@@ -0,0 +1,19 @@
package com.xiang.dingTalk.common.enums;
import lombok.Getter;
/**
* @Author: xiang
* @Date: 2025-05-09 16:00
*/
@Getter
public enum HttpMethod {
GET("get"),
;
final String method;
HttpMethod(String method) {
this.method = method;
}
}

View File

@@ -0,0 +1,118 @@
package com.xiang.dingTalk.service;
import com.alibaba.fastjson2.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiChatSendRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiChatSendResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.xiang.dingTalk.common.enums.DingTalkUrlEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @Author: xiang
* @Date: 2025-05-09 15:06
*/
@Slf4j
@Service
public class DingTalkService {
/**
* 应用凭证id
* https://open-dev.dingtalk.com/fe/ai?hash=%23%2Fapp%2F3fa4c9a7-27f2-4d6f-bbe7-41d2a17b5c11%2Fbaseinfo#/app/3fa4c9a7-27f2-4d6f-bbe7-41d2a17b5c11/baseinfo
*/
private static final String APP_ID = "3fa4c9a7-27f2-4d6f-bbe7-41d2a17b5c11";
/**
* 原企业内部应用AgentID
*/
private static final String AGENT_ID = "3829551658";
/**
* 组织ID
*
*/
private static final String CORP_ID = "dingf2c4425cd179a26ef2c783f7214b6d69";
private static final String CLIENT_ID = "dingcc9fikz1c0e5wb9v";
private static final String CLIENT_SECRET = "wyapsH6y8P1K_wuTPKGKwG0mquj1uth9Dxn6HcRpta3sh8Syukl0C8nOmR1PeBzs";
private static final String GRANT_TYPE = "client_credentials";
private static final String USER_ID = "450841600726084717";
private static final String MSG_TYPE = "text";
// userID 450841600726084717
// chatID chatd16d8daeea33b36b73588c676d508096
/**
* 发送消息到企业群
* @return
*/
public String sendChatMessage(String chatId, String message) throws Exception{
String token = getToken();
DefaultDingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_CHAR_MESSAGE.getUrl());
OapiChatSendRequest req = new OapiChatSendRequest();
req.setChatid(chatId);
OapiChatSendRequest.Msg msg = new OapiChatSendRequest.Msg();
OapiChatSendRequest.Text text = new OapiChatSendRequest.Text();
text.setContent(message);
msg.setText(text);
msg.setMsgtype("text");
req.setMsg(msg);
OapiChatSendResponse rsp = client.execute(req, token);
log.info("[DingTalk] send chat message, req:{}, token:{}, response:{}", JSONObject.toJSONString(req), token, JSONObject.toJSONString(rsp));
return rsp.getMessageId();
}
/**
* 异步发送工作同志--文本类型
* @param userId
* @param message
* @return
* @throws Exception
*/
public String asyncSendMessage(String userId, String message) throws Exception {
String token = getToken();
DingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_ASYNC_SEND_MESSAGE.getUrl());
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(Long.parseLong(AGENT_ID));
req.setUseridList(userId);
req.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg obj1 = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
obj1.setMsgtype(MSG_TYPE);
OapiMessageCorpconversationAsyncsendV2Request.Text obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Text();
obj2.setContent(message);
obj1.setText(obj2);
req.setMsg(obj1);
log.info("send Ding Talk message, userId:{}, message:{}", userId, message);
OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, token);
log.info("send Ding Talk message response, taskId:{}", JSONObject.toJSONString(rsp));
return rsp.getTaskId().toString();
}
/**
* 获取企业内部应用token
*
* @return
* @throws Exception
*/
public String getToken() throws Exception {
DingTalkClient client = new DefaultDingTalkClient(DingTalkUrlEnum.DING_TALK_GET_ENTERPRISE_INTER_TOKEN.getUrl());
OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey(CLIENT_ID);
req.setAppsecret(CLIENT_SECRET);
req.setHttpMethod("GET");
OapiGettokenResponse rsp = client.execute(req);
return rsp.getAccessToken();
}
}