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(String corpId) { com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config(); config.protocol = "https"; config.regionId = "central"; try { com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config); com.aliyun.dingtalkoauth2_1_0.models.GetTokenRequest getTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetTokenRequest() .setClientId(clientId) .setClientSecret(clientSecret) .setGrantType("client_credentials"); GetTokenResponse response = client.getToken(corpId, getTokenRequest); return response.getBody().accessToken; } catch (TeaException err) { if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { // err 中含有 code 和 message 属性,可帮助开发定位问题 log.error("Error getting access token: {}", err.getMessage()); } } 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 属性,可帮助开发定位问题 log.error("Error getting access token: {}", err.getMessage()); } } return null; } 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(dto.getCorpId())); if(rsp.getErrcode().equals(Constants.DD_ERR_CODE)){ return rsp.getResult(); }else{ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),rsp.getMessage()); } } }