|
@@ -1,5 +1,7 @@
|
|
|
package com.ruoyi.web.controller.dingding.service;
|
|
|
|
|
|
+import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders;
|
|
|
+import com.aliyun.dingtalkcontact_1_0.models.GetUserResponse;
|
|
|
import com.aliyun.dingtalkoauth2_1_0.Client;
|
|
|
import com.aliyun.dingtalkoauth2_1_0.models.*;
|
|
|
import com.aliyun.tea.TeaException;
|
|
@@ -7,10 +9,8 @@ import com.aliyun.teaopenapi.models.Config;
|
|
|
import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
-import com.dingtalk.api.request.OapiV2UserGetRequest;
|
|
|
-import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
|
|
|
-import com.dingtalk.api.response.OapiV2UserGetResponse;
|
|
|
-import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
|
|
|
+import com.dingtalk.api.request.*;
|
|
|
+import com.dingtalk.api.response.*;
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.utils.dingding.config.DingAppConfig;
|
|
@@ -37,7 +37,7 @@ public class DingThirdTokenService {
|
|
|
@Resource
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
- public static Client createClient() throws Exception {
|
|
|
+ public static Client createClient2_1_0() throws Exception {
|
|
|
Config config = new Config();
|
|
|
config.protocol = "https";
|
|
|
config.regionId = "central";
|
|
@@ -47,7 +47,7 @@ public class DingThirdTokenService {
|
|
|
public GetUserTokenResponse getUserAccessToken(String code) {
|
|
|
GetUserTokenResponse userToken = null;
|
|
|
try {
|
|
|
- Client client = this.createClient();
|
|
|
+ Client client = this.createClient2_1_0();
|
|
|
GetUserTokenRequest request = new GetUserTokenRequest()
|
|
|
.setClientSecret(dingAppConfig.getAppSecret())
|
|
|
.setClientId(dingAppConfig.getAppKey())
|
|
@@ -82,7 +82,7 @@ public class DingThirdTokenService {
|
|
|
return corpAccessToken;
|
|
|
}
|
|
|
try {
|
|
|
- com.aliyun.dingtalkoauth2_1_0.Client client = this.createClient();
|
|
|
+ com.aliyun.dingtalkoauth2_1_0.Client client = this.createClient2_1_0();
|
|
|
String suiteTicket = redisCache.getCacheObject(redisKeyPrefix + Constants.DAILY_DING_SUITE_TICKET);
|
|
|
log.info("从Redis缓存中获取到的第三方企业{},suiteTicket = {}", corpId, suiteTicket);
|
|
|
com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenRequest getCorpAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenRequest()
|
|
@@ -112,7 +112,56 @@ public class DingThirdTokenService {
|
|
|
return corpAccessToken;
|
|
|
}
|
|
|
|
|
|
- public OapiV2UserGetuserinfoResponse getUserUnfo(String code,String access_token) {
|
|
|
+ public Object q() {
|
|
|
+ DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
|
|
|
+ OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
|
|
|
+ req.setAuthCorpid("dingc365fcxxxx");
|
|
|
+ try {
|
|
|
+ OapiServiceGetCorpTokenResponse execute = client.execute(req, dingAppConfig.getAppKey(), dingAppConfig.getAppSecret(), "suiteTicket");
|
|
|
+ } catch (ApiException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ //服务商获取第三方应用授权企业的access_token
|
|
|
+ public String getThirdCorpAccessToken(String corpId) {
|
|
|
+ String redisKeyPrefix = Constants.DAILY_DING_AUTH + corpId + ":";
|
|
|
+ // 从持久化存储中读取
|
|
|
+ String corpAccessToken = redisCache.getCacheObject(redisKeyPrefix + Constants.DAILY_DING_CORP_ACCESS_TOKEN);
|
|
|
+ log.info("从Redis缓存中获取到的第三方企业{},corpAccessToken = {}", corpAccessToken);
|
|
|
+ if (corpAccessToken != null) {
|
|
|
+ return corpAccessToken;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String suiteTicket = redisCache.getCacheObject(redisKeyPrefix + Constants.DAILY_DING_SUITE_TICKET);
|
|
|
+ log.info("从Redis缓存中获取到的第三方企业{},suiteTicket = {}", corpId, suiteTicket);
|
|
|
+ DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
|
|
|
+ OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
|
|
|
+ req.setAuthCorpid(corpId);
|
|
|
+ OapiServiceGetCorpTokenResponse execute = client.execute(req, dingAppConfig.getAppKey(), dingAppConfig.getAppSecret(), suiteTicket);
|
|
|
+ corpAccessToken = execute.getAccessToken();
|
|
|
+ Long expireIn = execute.getExpiresIn();
|
|
|
+ log.info("从Redis缓存中获取到的第三方企业{},corpAccessToken = {}", execute.getAccessToken());
|
|
|
+ redisCache.setCacheObject(Constants.DAILY_DING_CORP_ACCESS_TOKEN, corpAccessToken, expireIn.intValue(), TimeUnit.MINUTES);
|
|
|
+ } catch (TeaException err) {
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ log.error("getAccessToken failed", err.message);
|
|
|
+ }
|
|
|
+ return corpAccessToken;
|
|
|
+ } 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("getAccessToken failed", err.message);
|
|
|
+ }
|
|
|
+ return corpAccessToken;
|
|
|
+ }
|
|
|
+ return corpAccessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OapiV2UserGetuserinfoResponse getUserUnfo(String code, String access_token) {
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
|
|
|
OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
|
|
|
req.setCode(code);
|
|
@@ -124,6 +173,7 @@ public class DingThirdTokenService {
|
|
|
}
|
|
|
return rsp;
|
|
|
}
|
|
|
+
|
|
|
public OapiV2UserGetResponse getAuthUser(String accessToken, String userId) {
|
|
|
DingTalkClient client = new DefaultDingTalkClient(DingUrlConstant.URL_USER_GET_V2);
|
|
|
OapiV2UserGetResponse response;
|
|
@@ -138,11 +188,12 @@ public class DingThirdTokenService {
|
|
|
}
|
|
|
return response;
|
|
|
}
|
|
|
+
|
|
|
//获取应用管理后台免登的用户信息
|
|
|
public Object getSsoUserInfo(String xAcsDingtalkAccessToken, String code) {
|
|
|
GetSsoUserInfoResponse ssoUserInfoWithOptions = null;
|
|
|
try {
|
|
|
- com.aliyun.dingtalkoauth2_1_0.Client client = this.createClient();
|
|
|
+ com.aliyun.dingtalkoauth2_1_0.Client client = this.createClient2_1_0();
|
|
|
GetSsoUserInfoHeaders getSsoUserInfoHeaders = new GetSsoUserInfoHeaders();
|
|
|
getSsoUserInfoHeaders.xAcsDingtalkAccessToken = xAcsDingtalkAccessToken;
|
|
|
GetSsoUserInfoRequest getSsoUserInfoRequest = new GetSsoUserInfoRequest()
|
|
@@ -161,4 +212,132 @@ public class DingThirdTokenService {
|
|
|
return ssoUserInfoWithOptions;
|
|
|
}
|
|
|
|
|
|
+ public com.aliyun.dingtalkcontact_1_0.Client createClient_1_0() throws Exception {
|
|
|
+ Config config = new Config();
|
|
|
+ config.protocol = "https";
|
|
|
+ config.regionId = "central";
|
|
|
+ return new com.aliyun.dingtalkcontact_1_0.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 获取用户通讯录个人信息
|
|
|
+ * @param: accessToken
|
|
|
+ * @param: unionId
|
|
|
+ * @return: java.lang.Object
|
|
|
+ * nick 用户的钉钉昵称。
|
|
|
+ * avatarUrl头像URL。
|
|
|
+ * mobile用户的手机号。
|
|
|
+ * openId用户的openId。
|
|
|
+ * unionId用户的unionId。
|
|
|
+ * email用户的个人邮箱。
|
|
|
+ * stateCode手机号对应的国家号。
|
|
|
+ */
|
|
|
+ public GetUserResponse getAddressBookUserInfo(String accessToken, String unionId) {
|
|
|
+ GetUserResponse response = null;
|
|
|
+ try {
|
|
|
+ com.aliyun.dingtalkcontact_1_0.Client client = this.createClient_1_0();
|
|
|
+ GetUserHeaders getUserHeaders = new GetUserHeaders();
|
|
|
+ getUserHeaders.xAcsDingtalkAccessToken = accessToken;
|
|
|
+ response = client.getUserWithOptions(unionId, getUserHeaders, new RuntimeOptions());
|
|
|
+ } catch (TeaException err) {
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ } 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 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //获取第三方企业应用的suite_access_token
|
|
|
+ public OapiServiceGetSuiteTokenResponse getSuiteToken(String corpId) {
|
|
|
+ OapiServiceGetSuiteTokenResponse rsp = null;
|
|
|
+ String redisKeyPrefix = Constants.DAILY_DING_AUTH + corpId + ":";
|
|
|
+ // 从持久化存储中读取
|
|
|
+ String corpAccessToken = redisCache.getCacheObject(redisKeyPrefix + Constants.DAILY_DING_CORP_ACCESS_TOKEN);
|
|
|
+ log.info("从Redis缓存中获取到的第三方企业{},corpAccessToken = {}", corpAccessToken);
|
|
|
+ try {
|
|
|
+ String suiteTicket = redisCache.getCacheObject(redisKeyPrefix + Constants.DAILY_DING_SUITE_TICKET);
|
|
|
+ log.info("从Redis缓存中获取到的第三方企业{},suiteTicket = {}", corpId, suiteTicket);
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_suite_token");
|
|
|
+ OapiServiceGetSuiteTokenRequest req = new OapiServiceGetSuiteTokenRequest();
|
|
|
+ req.setSuiteKey(dingAppConfig.getAppKey());
|
|
|
+ req.setSuiteSecret(dingAppConfig.getAppSecret());
|
|
|
+ req.setSuiteTicket(suiteTicket);
|
|
|
+ rsp = client.execute(req);
|
|
|
+ redisCache.setCacheObject(Constants.DAILY_DING_CORP_ACCESS_TOKEN, rsp.getSuiteAccessToken(), rsp.getExpiresIn().intValue(), TimeUnit.MINUTES);
|
|
|
+ } catch (TeaException err) {
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ log.error("getAccessToken failed", err.message);
|
|
|
+ }
|
|
|
+ } 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("getAccessToken failed", err.message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rsp;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取企业授权信息
|
|
|
+ public Object getAuthInfoRequest(String corpId) {
|
|
|
+ OapiServiceGetAuthInfoResponse rsp = null;
|
|
|
+ String redisKeyPrefix = Constants.DAILY_DING_AUTH + corpId + ":";
|
|
|
+ // 从持久化存储中读取
|
|
|
+ String corpAccessToken = redisCache.getCacheObject(redisKeyPrefix + Constants.DAILY_DING_CORP_ACCESS_TOKEN);
|
|
|
+ log.info("从Redis缓存中获取到的第三方企业{},corpAccessToken = {}", corpAccessToken);
|
|
|
+ try {
|
|
|
+ String suiteTicket = redisCache.getCacheObject(redisKeyPrefix + Constants.DAILY_DING_SUITE_TICKET);
|
|
|
+ log.info("从Redis缓存中获取到的第三方企业{},suiteTicket = {}", corpId, suiteTicket);
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_auth_info");
|
|
|
+ OapiServiceGetAuthInfoRequest req = new OapiServiceGetAuthInfoRequest();
|
|
|
+ req.setSuiteKey(suiteTicket);
|
|
|
+ req.setAuthCorpid(corpId);
|
|
|
+ // 第三方企业应用的填写应用SuiteKey和SuiteSecret。
|
|
|
+ // 定制应用填写应用的CustomKey和CustomSecret。
|
|
|
+ rsp = client.execute(req, dingAppConfig.getAppKey(), dingAppConfig.getAppSecret(), suiteTicket);
|
|
|
+ } catch (TeaException err) {
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ log.error("getAccessToken failed", err.message);
|
|
|
+ }
|
|
|
+ } 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("getAccessToken failed", err.message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rsp;
|
|
|
+ }
|
|
|
+//
|
|
|
+// public OapiServiceGetUnactiveCorpResponse getUnactiveCorpResponseetUnactiveCorpResponse(Long appId,String suiteAccessToken) {
|
|
|
+// OapiServiceGetUnactiveCorpResponse rsp = null;
|
|
|
+// try {
|
|
|
+// DingTalkClient client = new DefaultDingTalkClient(
|
|
|
+// "https://oapi.dingtalk.com/service/get_unactive_corp?suite_access_token="+suiteAccessToken+"");
|
|
|
+// OapiServiceGetUnactiveCorpRequest req = new OapiServiceGetUnactiveCorpRequest();
|
|
|
+// req.setAppId(appId);
|
|
|
+// rsp = client.execute(req);
|
|
|
+// } catch (TeaException err) {
|
|
|
+// if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+// // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+// log.error("getAccessToken failed", err.message);
|
|
|
+// }
|
|
|
+// } 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("getAccessToken failed", err.message);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return rsp;
|
|
|
+// }
|
|
|
+
|
|
|
}
|