parent
0de8911009
commit
4637728035
|
@ -0,0 +1,74 @@
|
||||||
|
package com.hzya.frame.plugin.kjs.listener;
|
||||||
|
|
||||||
|
import com.dingtalk.open.app.api.GenericEventListener;
|
||||||
|
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
|
||||||
|
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
|
||||||
|
import com.dingtalk.open.app.api.security.AuthClientCredential;
|
||||||
|
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
|
||||||
|
import com.hzya.frame.plugin.kjs.service.GenericEventConsumer;
|
||||||
|
import com.hzya.frame.plugin.kjs.util.GenericEventThreadPool;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 钉钉通讯录事件订阅
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/8/27 14:43
|
||||||
|
**/
|
||||||
|
@Component
|
||||||
|
@Order(9999)
|
||||||
|
public class GenericEventSubscribe {
|
||||||
|
|
||||||
|
Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
//线程池
|
||||||
|
GenericEventThreadPool threadPool = new GenericEventThreadPool();
|
||||||
|
@Resource
|
||||||
|
private GenericEventConsumer genericEventConsumer;
|
||||||
|
@Value("${dingtalk.appKey:}")
|
||||||
|
private String appKey;
|
||||||
|
@Value("${dingtalk.appSecret:}")
|
||||||
|
private String appSecret;
|
||||||
|
/**
|
||||||
|
* 通讯录事件订阅
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void subscribe(){
|
||||||
|
try {
|
||||||
|
logger.info("通讯录事件订阅");
|
||||||
|
OpenDingTalkStreamClientBuilder
|
||||||
|
.custom()
|
||||||
|
.credential(new AuthClientCredential(appKey, appSecret))
|
||||||
|
//注册事件监听
|
||||||
|
.registerAllEventListener(new GenericEventListener() {
|
||||||
|
public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
//处理事件
|
||||||
|
threadPool.executeTask(() ->{
|
||||||
|
genericEventConsumer.consume(event);
|
||||||
|
});
|
||||||
|
threadPool.close();
|
||||||
|
}catch (Exception e){
|
||||||
|
logger.error("GenericEventThreadPool错误:{}",e);
|
||||||
|
}finally {
|
||||||
|
threadPool.close();
|
||||||
|
}
|
||||||
|
//消费成功
|
||||||
|
return EventAckStatus.SUCCESS;
|
||||||
|
} catch (Exception e) {
|
||||||
|
//消费失败
|
||||||
|
return EventAckStatus.LATER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build().start();
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,119 @@
|
||||||
|
package com.hzya.frame.plugin.kjs.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.map.MapBuilder;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
|
||||||
|
import com.dingtalk.api.response.OapiV2UserGetResponse;
|
||||||
|
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
|
||||||
|
import com.hzya.frame.dingtalk.enums.OrgEventEnum;
|
||||||
|
import com.hzya.frame.dingtalk.service.IDingTalkService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import shade.com.alibaba.fastjson2.JSONArray;
|
||||||
|
import shade.com.alibaba.fastjson2.JSONObject;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 通讯录事件订阅消费者
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/8/27 15:33
|
||||||
|
**/
|
||||||
|
public class GenericEventConsumer {
|
||||||
|
Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
@Resource
|
||||||
|
private IDingTalkService dingtalkService;
|
||||||
|
/**
|
||||||
|
* 消费通讯录事件订阅消息
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
*/
|
||||||
|
public void consume(GenericOpenDingTalkEvent event) {
|
||||||
|
//事件唯一Id
|
||||||
|
String eventId = event.getEventId();
|
||||||
|
//事件类型
|
||||||
|
String eventType = event.getEventType();
|
||||||
|
//事件产生时间
|
||||||
|
Long bornTime = event.getEventBornTime();
|
||||||
|
//获取事件体
|
||||||
|
JSONObject bizData = event.getData();
|
||||||
|
logger.info("事件唯一id{},事件类型:{},参数:{}",eventId,eventType,JSONObject.toJSONString(bizData));
|
||||||
|
OrgEventEnum orgEventEnum = OrgEventEnum.getByCode(eventType);
|
||||||
|
String apiCode = "";
|
||||||
|
JSONObject mdmObj = new JSONObject();
|
||||||
|
String type = "";
|
||||||
|
switch (orgEventEnum){
|
||||||
|
//新增到主数据
|
||||||
|
case USER_ADD_ORG:
|
||||||
|
type = "user";
|
||||||
|
apiCode = "8000040022";
|
||||||
|
break;
|
||||||
|
case USER_MODIFY_ORG:
|
||||||
|
case USER_LEAVE_ORG:
|
||||||
|
type = "user";
|
||||||
|
apiCode = "8000040023";
|
||||||
|
break;
|
||||||
|
case ORG_DEPT_CREATE:
|
||||||
|
type = "dept";
|
||||||
|
apiCode = "8000040022";
|
||||||
|
break;
|
||||||
|
case ORG_DEPT_MODIFY:
|
||||||
|
type = "dept";
|
||||||
|
apiCode = "8000040023";
|
||||||
|
break;
|
||||||
|
case ORG_DEPT_REMOVE:
|
||||||
|
type = "dept";
|
||||||
|
apiCode = "8000040024";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ("user".equals(type)){
|
||||||
|
//解析报文
|
||||||
|
JSONArray userIds = bizData.getJSONArray("userId");
|
||||||
|
for (int i=0; i<userIds.size();i++){
|
||||||
|
OapiV2UserGetResponse.UserGetResponse userInfo = dingtalkService.getUserById(userIds.getString(i));
|
||||||
|
Map<String,Object> mdmMap = new LinkedHashMap<>();
|
||||||
|
mdmMap.put("mdmCode","10031");
|
||||||
|
mdmMap.put("optionName","系统管理员");
|
||||||
|
Map<String,Object> userMap = new LinkedHashMap<>();
|
||||||
|
userMap.put("data_id", IdUtil.fastUUID());
|
||||||
|
userMap.put("unionid",userInfo.getUnionid());
|
||||||
|
userMap.put("userid",userInfo.getUserid());
|
||||||
|
userMap.put("name",userInfo.getName());
|
||||||
|
userMap.put("mobile",userInfo.getMobile());
|
||||||
|
userMap.put("job_number",userInfo.getJobNumber());
|
||||||
|
userMap.put("title",userInfo.getTitle());
|
||||||
|
userMap.put("email",userInfo.getEmail());
|
||||||
|
userMap.put("org_email",userInfo.getOrgEmail());
|
||||||
|
if (null != userInfo.getHiredDate()){
|
||||||
|
userMap.put("hired_date", DateUtil.date(userInfo.getHiredDate()));
|
||||||
|
}
|
||||||
|
mdmMap.put("mdm_dd_user",userMap);
|
||||||
|
//保存到人员主数据
|
||||||
|
Map<String, String> headerMap = MapBuilder.<String, String>create(true)
|
||||||
|
.put("apiCode", apiCode)//中台接口
|
||||||
|
.put("publicKey","ZJYAn/EBWEhLUMezDLU4iZ1vTO9kc6pM6XrYLajnqnK60Q9Ce7eDIk+3zDUT+v578prj")//钉钉应用
|
||||||
|
.put("secretKey","ctMIYyauwoKSFeU4tg5gH1aWC/3OJK6HsKJrSR0oyDmdmdvRNgdoTzX0C1OQ+whrj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=")//钉钉应用
|
||||||
|
.put("appId","800004")//中台应用
|
||||||
|
.build();
|
||||||
|
//String body = HttpRequest.post("http://127.0.0.1:9999/kangarooDataCenterV3/entranceController/externalCallInterfaceToESB").addHeaders(headerMap).body(com.alibaba.fastjson.JSONObject.toJSONString(mdmObj)).timeout(60000).execute().body();
|
||||||
|
}
|
||||||
|
}else if ("dept".equals(type)){
|
||||||
|
JSONArray deptIdArr = bizData.getJSONArray("deptId");
|
||||||
|
for (int i=0; i<deptIdArr.size(); i++){
|
||||||
|
OapiV2DepartmentGetResponse.DeptGetResponse deptInfo = dingtalkService.getDeptById(deptIdArr.getLong(i));
|
||||||
|
Map<String,Object> mdmMap = new LinkedHashMap<>();
|
||||||
|
mdmMap.put("mdmCode","10033");
|
||||||
|
mdmMap.put("optionName","系统管理员");
|
||||||
|
Map<String,Object> deptMap = new LinkedHashMap<>();
|
||||||
|
deptMap.put("dept_id",deptInfo.getDeptId());
|
||||||
|
deptMap.put("name",deptInfo.getName());
|
||||||
|
deptMap.put("parent_id",deptInfo.getParentId());
|
||||||
|
mdmMap.put("mdm_dd_dept",deptMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.hzya.frame.plugin.kjs.util;
|
||||||
|
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 用线程消费订阅事件的消息
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/8/27 15:10
|
||||||
|
**/
|
||||||
|
public class GenericEventThreadPool {
|
||||||
|
private static final int MAX_THREADS = 10; // 最大线程数,可以根据需要调整
|
||||||
|
private static final int TASK_QUEUE_SIZE = 100; // 任务队列大小,可以根据需要调整
|
||||||
|
|
||||||
|
private final ExecutorService executorService;
|
||||||
|
|
||||||
|
public GenericEventThreadPool() {
|
||||||
|
// 创建固定线程数的线程池
|
||||||
|
executorService = new ThreadPoolExecutor(
|
||||||
|
MAX_THREADS,
|
||||||
|
MAX_THREADS,
|
||||||
|
0L,
|
||||||
|
TimeUnit.MILLISECONDS,
|
||||||
|
new ArrayBlockingQueue<>(TASK_QUEUE_SIZE),
|
||||||
|
new ThreadPoolExecutor.CallerRunsPolicy());
|
||||||
|
}
|
||||||
|
|
||||||
|
//提交任务
|
||||||
|
public void executeTask(Runnable task) {
|
||||||
|
executorService.execute(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
//关闭线程池
|
||||||
|
public void close() {
|
||||||
|
if (executorService!= null &&!executorService.isShutdown()) {
|
||||||
|
executorService.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -6,19 +6,19 @@ logging:
|
||||||
encodings: UTF-8
|
encodings: UTF-8
|
||||||
file:
|
file:
|
||||||
# 日志保存路径
|
# 日志保存路径
|
||||||
path: /Users/xiangerlin/work/app/logs/ydc
|
path: /Users/xiangerlin/work/app/logs/kjs
|
||||||
spring:
|
spring:
|
||||||
datasource:
|
datasource:
|
||||||
dynamic:
|
dynamic:
|
||||||
datasource:
|
datasource:
|
||||||
master:
|
master:
|
||||||
url: jdbc:mysql://ufidahz.com.cn:9014/businesscenter?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowLoadLocalInfile=false&autoReconnect=true&failOverReadOnly=false&connectTimeout=30000&socketTimeout=30000&autoReconnectForPools=true
|
url: jdbc:mysql://ufidahz.com.cn:9014/businesscenter_kjs?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowLoadLocalInfile=false&autoReconnect=true&failOverReadOnly=false&connectTimeout=30000&socketTimeout=30000&autoReconnectForPools=true
|
||||||
username: root
|
username: root
|
||||||
password: 62e4295b615a30dbf3b8ee96f41c820b
|
password: 62e4295b615a30dbf3b8ee96f41c820b
|
||||||
driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
|
driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
|
||||||
savefile:
|
savefile:
|
||||||
# 文件保存路径
|
# 文件保存路径
|
||||||
path: /Users/xiangerlin/work/app/logs/ydc
|
path: /Users/xiangerlin/work/app/logs/kjs
|
||||||
cbs8:
|
cbs8:
|
||||||
appId: 1P4AGrpz
|
appId: 1P4AGrpz
|
||||||
appSecret: 2c2369ae5dc04382844bbe3a5abf39e1bea9cd3a
|
appSecret: 2c2369ae5dc04382844bbe3a5abf39e1bea9cd3a
|
||||||
|
@ -33,3 +33,8 @@ cbs8:
|
||||||
elec_path: /Users/xiangerlin/Downloads/
|
elec_path: /Users/xiangerlin/Downloads/
|
||||||
OA:
|
OA:
|
||||||
data_source_code: yc-test
|
data_source_code: yc-test
|
||||||
|
dingtalk:
|
||||||
|
appKey: dingbltm9wszcl7hoxyg
|
||||||
|
appSecret: ex3a9qwdTXWf_VKJEddAg8YE7x98kIl3Nsy_7g0NdwvK3w3nXdUhJ3XQP4lUYs2d
|
||||||
|
appId: dd39187a-079a-466c-b4b7-9ef3172e9e61
|
||||||
|
agentId: 3209295620 appkey:dingbltm9wszcl7hoxyg
|
||||||
|
|
|
@ -4,4 +4,5 @@
|
||||||
<bean name="deliveryOrderPluginServiceImpl" class="com.hzya.frame.plugin.kjs.service.impl.DeliveryOrderPluginServiceImpl" />
|
<bean name="deliveryOrderPluginServiceImpl" class="com.hzya.frame.plugin.kjs.service.impl.DeliveryOrderPluginServiceImpl" />
|
||||||
<bean name="momOrderPluginServiceImpl" class="com.hzya.frame.plugin.kjs.service.impl.MomOrderPluginServiceImpl" />
|
<bean name="momOrderPluginServiceImpl" class="com.hzya.frame.plugin.kjs.service.impl.MomOrderPluginServiceImpl" />
|
||||||
<bean name="kjsPluginBaseService" class="com.hzya.frame.plugin.kjs.service.impl.KjsPluginBaseService" />
|
<bean name="kjsPluginBaseService" class="com.hzya.frame.plugin.kjs.service.impl.KjsPluginBaseService" />
|
||||||
|
<bean name="genericEventConsumer" class="com.hzya.frame.plugin.kjs.service.GenericEventConsumer" />
|
||||||
</beans>
|
</beans>
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.hzya.frame.dingtalk.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 通讯录事件类型
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/8/27 15:58
|
||||||
|
**/
|
||||||
|
public enum OrgEventEnum {
|
||||||
|
USER_ADD_ORG("user_add_org","通讯录用户新增"),
|
||||||
|
USER_MODIFY_ORG("user_modify_org","通讯录用户更改"),
|
||||||
|
USER_LEAVE_ORG("user_leave_org","通讯录用户离职"),
|
||||||
|
USER_ACTIVE_ORG("user_active_org","加入企业后用户激活"),
|
||||||
|
ORG_DEPT_CREATE("org_dept_create","通讯录企业部门创建"),
|
||||||
|
ORG_DEPT_MODIFY("org_dept_modify","通讯录企业部门更改"),
|
||||||
|
ORG_DEPT_REMOVE("org_dept_remove","通讯录企业部门删除"),
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
private String explain;
|
||||||
|
|
||||||
|
OrgEventEnum(String code, String explain) {
|
||||||
|
this.code = code;
|
||||||
|
this.explain = explain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExplain() {
|
||||||
|
return explain;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据code获取事件类型
|
||||||
|
* @param code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static OrgEventEnum getByCode(String code){
|
||||||
|
for (OrgEventEnum org : OrgEventEnum.values()) {
|
||||||
|
if (org.getCode().equals(code)){
|
||||||
|
return org;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.hzya.frame.dingtalk.service;
|
||||||
|
|
||||||
|
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
|
||||||
|
import com.dingtalk.api.response.OapiV2UserGetResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 钉钉service
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/8/27 16:17
|
||||||
|
**/
|
||||||
|
public interface IDingTalkService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据userid获取用户详情
|
||||||
|
* @param userId 钉钉userid
|
||||||
|
* @param appKey
|
||||||
|
* @param appSecret
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
OapiV2UserGetResponse.UserGetResponse getUserById(String userId,String appKey,String appSecret);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据userid获取用户详情
|
||||||
|
* @param userId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
OapiV2UserGetResponse.UserGetResponse getUserById(String userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据部门id获取部门详情
|
||||||
|
* @param deptId 钉钉部门id
|
||||||
|
* @param appKey
|
||||||
|
* @param appSecret
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
OapiV2DepartmentGetResponse.DeptGetResponse getDeptById(Long deptId,String appKey,String appSecret);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据部门id获取部门详情
|
||||||
|
* @param deptId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
OapiV2DepartmentGetResponse.DeptGetResponse getDeptById(Long deptId);
|
||||||
|
}
|
|
@ -0,0 +1,110 @@
|
||||||
|
package com.hzya.frame.dingtalk.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.dingtalk.api.DefaultDingTalkClient;
|
||||||
|
import com.dingtalk.api.DingTalkClient;
|
||||||
|
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
|
||||||
|
import com.dingtalk.api.request.OapiV2UserGetRequest;
|
||||||
|
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
|
||||||
|
import com.dingtalk.api.response.OapiV2UserGetResponse;
|
||||||
|
import com.hzya.frame.dingtalk.service.IDingTalkService;
|
||||||
|
import com.hzya.frame.dingtalk.util.DingTalkAccessToken;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 钉钉service
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/8/27 16:17
|
||||||
|
**/
|
||||||
|
@Service()
|
||||||
|
public class DingTalkServiceImpl implements IDingTalkService {
|
||||||
|
Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
@Value("${dingtalk.appKey:}")
|
||||||
|
private String dAppKey;
|
||||||
|
@Value("${dingtalk.appSecret:}")
|
||||||
|
private String dAppSecret;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据userid获取用户详情
|
||||||
|
*
|
||||||
|
* @param userId 钉钉userid
|
||||||
|
* @param appKey
|
||||||
|
* @param appSecret
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public OapiV2UserGetResponse.UserGetResponse getUserById(String userId, String appKey, String appSecret) {
|
||||||
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
|
||||||
|
OapiV2UserGetRequest req = new OapiV2UserGetRequest();
|
||||||
|
req.setUserid(userId);
|
||||||
|
req.setLanguage("zh_CN");
|
||||||
|
try {
|
||||||
|
OapiV2UserGetResponse rsp = client.execute(req, DingTalkAccessToken.getAccessToken(appKey,appSecret));
|
||||||
|
if (rsp.isSuccess()){
|
||||||
|
OapiV2UserGetResponse.UserGetResponse result = rsp.getResult();
|
||||||
|
String s = JSONObject.toJSONString(result);
|
||||||
|
logger.info("人员详情信息:{}",s);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
logger.error("根据部门id获取钉钉用户详情出错:{}",e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据userid获取用户详情
|
||||||
|
*
|
||||||
|
* @param userId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public OapiV2UserGetResponse.UserGetResponse getUserById(String userId) {
|
||||||
|
return getUserById(userId,dAppKey,dAppSecret);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据部门id获取部门详情
|
||||||
|
*
|
||||||
|
* @param deptId 钉钉部门id
|
||||||
|
* @param appKey
|
||||||
|
* @param appSecret
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public OapiV2DepartmentGetResponse.DeptGetResponse getDeptById(Long deptId, String appKey, String appSecret) {
|
||||||
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
|
||||||
|
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
|
||||||
|
req.setDeptId(deptId);
|
||||||
|
req.setLanguage("zh_CN");
|
||||||
|
try {
|
||||||
|
OapiV2DepartmentGetResponse rsp = client.execute(req, DingTalkAccessToken.getAccessToken(appKey,appSecret));
|
||||||
|
if (rsp.isSuccess()){
|
||||||
|
OapiV2DepartmentGetResponse.DeptGetResponse result = rsp.getResult();
|
||||||
|
String s = JSONObject.toJSONString(result);
|
||||||
|
logger.info("部门详情信息:{}",s);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
logger.error("根据部门id获取钉钉部门出错:{}",e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据部门id获取部门详情
|
||||||
|
*
|
||||||
|
* @param deptId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public OapiV2DepartmentGetResponse.DeptGetResponse getDeptById(Long deptId) {
|
||||||
|
return getDeptById(deptId,dAppKey,dAppSecret);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,103 @@
|
||||||
|
package com.hzya.frame.dingtalk.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
|
||||||
|
import com.aliyun.tea.TeaException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 钉钉获取accessToken
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/8/27 14:05
|
||||||
|
**/
|
||||||
|
public class DingTalkAccessToken {
|
||||||
|
static Logger logger = LoggerFactory.getLogger(DingTalkAccessToken.class);
|
||||||
|
//token
|
||||||
|
private static String accessToken;
|
||||||
|
//过期时间
|
||||||
|
private static Instant expireTime;
|
||||||
|
private static final Long CACHE_EXPIRY_TIME = 7000L; // 缓存有效时间(秒)
|
||||||
|
//应用key
|
||||||
|
private static String appKey;
|
||||||
|
//应用密钥
|
||||||
|
private static String appSecret;
|
||||||
|
@Value("${dingtalk.appKey:}")
|
||||||
|
public static void setAppKey(String appKey) {
|
||||||
|
DingTalkAccessToken.appKey = appKey;
|
||||||
|
}
|
||||||
|
@Value("${dingtalk.appSecret:}")
|
||||||
|
public static void setAppSecret(String appSecret) {
|
||||||
|
DingTalkAccessToken.appSecret = appSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取token
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getAccessToken(){
|
||||||
|
return getAccessToken(appKey,appSecret);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取accessToken
|
||||||
|
*
|
||||||
|
* @param appKey
|
||||||
|
* @param appSecret
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getAccessToken(String appKey,String appSecret) {
|
||||||
|
//判断是否过期 如果没过期直接返回
|
||||||
|
if (null != accessToken && expireTime != null && Instant.now().isBefore(expireTime)) {
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
//获取新的accessToken
|
||||||
|
accessToken = fetchNewAccessToken(appKey,appSecret);
|
||||||
|
//过期时间设置成当前事件+7000s,预留200s的时间
|
||||||
|
expireTime = Instant.now().plusSeconds(CACHE_EXPIRY_TIME);
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取新的accessToken
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static String fetchNewAccessToken(String appKey,String appSecret) {
|
||||||
|
try {
|
||||||
|
//查询应用上配置的钉钉信息
|
||||||
|
if (StrUtil.isNotEmpty(appKey) && StrUtil.isNotEmpty(appSecret)) {
|
||||||
|
//查询应用上的信息
|
||||||
|
com.aliyun.dingtalkoauth2_1_0.Client client = DingTalkAccessToken.createClient();
|
||||||
|
com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
|
||||||
|
.setAppKey(appKey)
|
||||||
|
.setAppSecret(appSecret);
|
||||||
|
GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
|
||||||
|
String accessToken1 = accessToken.getBody().getAccessToken();
|
||||||
|
return accessToken1;
|
||||||
|
}
|
||||||
|
} catch (Exception _err) {
|
||||||
|
TeaException err = new TeaException(_err.getMessage(), _err);
|
||||||
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
||||||
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
||||||
|
}
|
||||||
|
logger.error("获取钉钉token出错:{}", _err);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 Token 初始化账号Client
|
||||||
|
*
|
||||||
|
* @return Client
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static com.aliyun.dingtalkoauth2_1_0.Client createClient() throws Exception {
|
||||||
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
||||||
|
config.protocol = "https";
|
||||||
|
config.regionId = "central";
|
||||||
|
return new com.aliyun.dingtalkoauth2_1_0.Client(config);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue