|
@@ -1,6 +1,8 @@
|
|
|
-package com.fjhx.utils;
|
|
|
+package com.fjhx.dingding.utils;
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.aliyun.dingtalkoauth2_1_0.Client;
|
|
|
import com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenRequest;
|
|
|
import com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenResponseBody;
|
|
@@ -8,13 +10,20 @@ import com.aliyun.tea.TeaException;
|
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
+import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
|
|
|
import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
|
|
|
+import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
|
|
|
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
|
|
|
-import com.fjhx.constant.Constant;
|
|
|
+import com.fjhx.dingding.constant.Constant;
|
|
|
+import com.fjhx.dingding.entity.suite.po.SuiteTicketInfo;
|
|
|
+import com.fjhx.dingding.service.suite.SuiteTicketInfoService;
|
|
|
+import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
-import com.taobao.api.ApiException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 钉钉第三方企业应用 - h5微应用
|
|
@@ -29,22 +38,39 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@Slf4j
|
|
|
public class DingUtil {
|
|
|
|
|
|
+ // private static final RedisCache redisCache = SpringUtil.getBean(RedisCache.class);
|
|
|
+ private static final RedisCache redisCache =null;
|
|
|
+
|
|
|
+ // private static final SuiteTicketInfoService suiteTicketInfoService = SpringUtil.getBean(SuiteTicketInfoService.class);
|
|
|
+ private static final SuiteTicketInfoService suiteTicketInfoService = null;
|
|
|
+
|
|
|
+ private static final String corpAccessTokenRedisKey = "corpAccessToken:";
|
|
|
+
|
|
|
/**
|
|
|
* 获取第三方应用授权企业的accessToken
|
|
|
* https://open.dingtalk.com/document/isvapp/obtain-the-access_token-of-the-authorized-enterprise
|
|
|
*/
|
|
|
- public static GetCorpAccessTokenResponseBody getCorpAccessToken(String corpId) {
|
|
|
+ public static String getCorpAccessToken(String corpId) {
|
|
|
+
|
|
|
+ String redisKey = corpAccessTokenRedisKey + corpId;
|
|
|
+ String corpAccessToken = redisCache.getCacheObject(redisKey);
|
|
|
+ if (StrUtil.isNotBlank(corpAccessToken)) {
|
|
|
+ return corpAccessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ SuiteTicketInfo suiteTicketInfo = suiteTicketInfoService.getOne(q -> q.eq(SuiteTicketInfo::getCorpId, corpId));
|
|
|
|
|
|
GetCorpAccessTokenRequest getCorpAccessTokenRequest = new GetCorpAccessTokenRequest()
|
|
|
.setSuiteKey(Constant.SUITE_KEY)
|
|
|
.setSuiteSecret(Constant.SUITE_SECRET)
|
|
|
.setAuthCorpId(corpId)
|
|
|
- .setSuiteTicket("Kn3hJvJR34e8rUEob0W7nqxK2YHqobAyPsuIQXYR35SsJMp6rE5XQBz9nwXTrJRZaVmXc4z9KjYkzXdkUdS8O3");
|
|
|
+ .setSuiteTicket(suiteTicketInfo.getSuiteTicket());
|
|
|
|
|
|
try {
|
|
|
Client client = getClient2();
|
|
|
- GetCorpAccessTokenResponseBody corpAccessToken = client.getCorpAccessToken(getCorpAccessTokenRequest).getBody();
|
|
|
- return corpAccessToken;
|
|
|
+ GetCorpAccessTokenResponseBody body = client.getCorpAccessToken(getCorpAccessTokenRequest).getBody();
|
|
|
+ redisCache.setCacheObject(redisKey, body.getAccessToken(), Convert.toInt(body.getExpireIn() - 60), TimeUnit.SECONDS);
|
|
|
+ return body.getAccessToken();
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
TeaException teaException;
|
|
@@ -80,12 +106,31 @@ public class DingUtil {
|
|
|
req.setCode(code);
|
|
|
OapiV2UserGetuserinfoResponse rsp = client.execute(req, corpAccessToken);
|
|
|
return rsp.getResult();
|
|
|
- } catch (ApiException e) {
|
|
|
- log.error("通过免登码获取用户信息失败");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("通过免登码获取用户信息失败", e);
|
|
|
throw new ServiceException("通过免登码获取用户信息失败");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
+ getDeptList("", 1L);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<OapiV2DepartmentListsubResponse.DeptBaseResponse> getDeptList(String corpId, Long deptId) {
|
|
|
+ try {
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
|
|
|
+ OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
|
|
|
+ req.setDeptId(deptId);
|
|
|
+ req.setLanguage("zh_CN");
|
|
|
+ // OapiV2DepartmentListsubResponse rsp = client.execute(req, getCorpAccessToken(corpId));
|
|
|
+ OapiV2DepartmentListsubResponse rsp = client.execute(req, "53b78b270b28320e869bf98a9f8e8486");
|
|
|
+ return rsp.getResult();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取用户部门失败", e);
|
|
|
+ throw new ServiceException("获取用户部门失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
private static com.aliyun.dingtalkcontact_1_0.Client getClient1() throws Exception {
|
|
|
Config config = new Config();
|