package com.doumee.core.dingTalk;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.aliyun.dingtalkoauth2_1_0.Client;
|
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
|
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
|
import com.aliyun.dingtalkoauth2_1_0.models.GetTokenResponse;
|
import com.aliyun.dingtalktodo_1_0.models.*;
|
import com.aliyun.tea.TeaException;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.dingtalk.api.DefaultDingTalkClient;
|
import com.dingtalk.api.DingTalkClient;
|
import com.dingtalk.api.request.*;
|
import com.dingtalk.api.response.*;
|
import com.doumee.biz.system.SystemDictDataBiz;
|
import com.doumee.core.constants.Constants;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.dao.system.dto.DingLoginDTO;
|
import com.doumee.dao.system.model.SystemDictData;
|
import com.taobao.api.ApiException;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* Created by IntelliJ IDEA.
|
*
|
* @Author : Rk
|
* @create 2025/9/24 16:25
|
*/
|
@Slf4j
|
@Service
|
public class DingTalk {
|
|
@Autowired
|
private SystemDictDataBiz systemDictDataBiz;
|
|
|
@Value("${dingtalk.clientId}")
|
private String clientId;
|
@Value("${dingtalk.clientSecret}")
|
private String clientSecret;
|
|
/**
|
* 使用 Token 初始化账号Client 数据同步类
|
* @return Client com.aliyun.dingtalkoauth2_1_0.
|
* @throws Exception
|
*/
|
public static Client createClient() throws Exception {
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
config.protocol = "https";
|
config.regionId = "central";
|
return new Client(config);
|
}
|
|
/**
|
* 待办通知类 链接池
|
* @return
|
* @throws Exception
|
*/
|
public static com.aliyun.dingtalktodo_1_0.Client createV1Client() throws Exception {
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
config.protocol = "https";
|
config.regionId = "central";
|
return new com.aliyun.dingtalktodo_1_0.Client(config);
|
}
|
|
|
/**
|
* 获取钉钉Token
|
* @return
|
*/
|
public String getToken(){
|
String accessToken = systemDictDataBiz.queryByCode(Constants.DD_TALK,Constants.ACCESS_TOKEN).getCode();
|
return accessToken;
|
}
|
|
private String getAccessToken() throws ApiException {
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
OapiGettokenRequest req = new OapiGettokenRequest();
|
req.setAppkey(clientId);
|
req.setAppsecret(clientSecret);
|
req.setHttpMethod("GET");
|
OapiGettokenResponse rsp = client.execute(req);
|
return rsp.getAccessToken();
|
}
|
|
|
public OapiV2UserGetuserinfoResponse.UserGetByCodeResponse getDDUserByCode(DingLoginDTO dto) throws ApiException {
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
|
OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
|
req.setCode(dto.getCode());
|
OapiV2UserGetuserinfoResponse rsp = client.execute(req, getAccessToken());
|
if(rsp.getErrcode().equals(Constants.DD_ERR_CODE)){
|
return rsp.getResult();
|
}else{
|
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),rsp.getMessage());
|
}
|
}
|
|
|
|
}
|