|
|
|
@ -55,7 +55,9 @@ 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.OapiGettokenRequest;
|
|
|
|
|
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
|
|
|
|
|
import com.dingtalk.api.response.OapiGettokenResponse;
|
|
|
|
|
import com.dingtalk.api.response.OapiV2UserGetResponse;
|
|
|
|
|
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
|
|
|
|
|
import com.google.common.cache.Cache;
|
|
|
|
@ -72,6 +74,7 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
@ -1169,6 +1172,8 @@ public class WhiteController extends CoreBaseController {
|
|
|
|
|
private static final Long ACCESS_TOKEN_EXPIRE_TIME = 7080L;
|
|
|
|
|
private static final String ACCESS_TOKEN_KEY = "ding_talk:access_token";
|
|
|
|
|
|
|
|
|
|
private static final String ACCESS_MENU_TOKEN_KEY = "ding_talk:menu_access_token";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 钉钉单聊机器人批量发送消息
|
|
|
|
|
*
|
|
|
|
@ -1349,6 +1354,7 @@ public class WhiteController extends CoreBaseController {
|
|
|
|
|
private String getAccessToken() {
|
|
|
|
|
Object accessTokenCached = redisCore.getObject(ACCESS_TOKEN_KEY);
|
|
|
|
|
if (accessTokenCached != null) {
|
|
|
|
|
LOGGER.info("通过缓存获取到的钉钉token, key={},value={}", ACCESS_TOKEN_KEY, accessTokenCached);
|
|
|
|
|
return String.valueOf(accessTokenCached);
|
|
|
|
|
}
|
|
|
|
|
Config config = new Config();
|
|
|
|
@ -1368,6 +1374,7 @@ public class WhiteController extends CoreBaseController {
|
|
|
|
|
try {
|
|
|
|
|
GetAccessTokenResponse response = client.getAccessToken(getAccessTokenRequest);
|
|
|
|
|
if (!StringUtils.isEmpty(response.body.accessToken)) {
|
|
|
|
|
LOGGER.info("插入钉钉token缓存, key={},value={}", ACCESS_TOKEN_KEY, response.body.accessToken);
|
|
|
|
|
redisCore.putObject(ACCESS_TOKEN_KEY, response.body.accessToken, ACCESS_TOKEN_EXPIRE_TIME);
|
|
|
|
|
}
|
|
|
|
|
return response.body.accessToken;
|
|
|
|
@ -1510,7 +1517,7 @@ public class WhiteController extends CoreBaseController {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Optional<OapiV2UserGetResponse> userInfo =
|
|
|
|
|
dingTalkService.getUserInfo(dingTalkUrl, dingTalkAppKey, dingTalkAppSecret, dingTalkTmpAuthCode);
|
|
|
|
|
dingTalkService.getUserInfo(dingTalkUrl, getMenuAccessToken(dingTalkUrl, dingTalkAppKey, dingTalkAppSecret), dingTalkTmpAuthCode);
|
|
|
|
|
if (userInfo.isPresent()) {
|
|
|
|
|
String mobile = userInfo.get().getResult().getMobile();
|
|
|
|
|
if (StringUtils.isEmpty(mobile)) {
|
|
|
|
@ -1526,4 +1533,35 @@ public class WhiteController extends CoreBaseController {
|
|
|
|
|
}
|
|
|
|
|
return sysUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取accessToken
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private String getMenuAccessToken(String dingUrl, String appKey, String appSecret) {
|
|
|
|
|
Object accessTokenCached = redisCore.getObject(ACCESS_MENU_TOKEN_KEY);
|
|
|
|
|
if (accessTokenCached != null) {
|
|
|
|
|
LOGGER.info("通过缓存获取到的钉钉menu_token, key={},value={}", ACCESS_MENU_TOKEN_KEY, accessTokenCached);
|
|
|
|
|
return String.valueOf(accessTokenCached);
|
|
|
|
|
}
|
|
|
|
|
String result = null;
|
|
|
|
|
DefaultDingTalkClient client = new DefaultDingTalkClient(dingUrl + "/gettoken");
|
|
|
|
|
OapiGettokenRequest request = new OapiGettokenRequest();
|
|
|
|
|
request.setAppkey(appKey);
|
|
|
|
|
request.setAppsecret(appSecret);
|
|
|
|
|
request.setHttpMethod(HttpMethod.GET.name());
|
|
|
|
|
try {
|
|
|
|
|
OapiGettokenResponse response = client.execute(request);
|
|
|
|
|
result = response.isSuccess() ? response.getAccessToken() : null;
|
|
|
|
|
if (!StringUtils.isEmpty(result)) {
|
|
|
|
|
LOGGER.info("插入钉钉menu_token缓存, key={},value={}", ACCESS_MENU_TOKEN_KEY, response.getAccessToken());
|
|
|
|
|
redisCore.putObject(ACCESS_MENU_TOKEN_KEY, response.getAccessToken(), ACCESS_TOKEN_EXPIRE_TIME);
|
|
|
|
|
}
|
|
|
|
|
} catch (ApiException e) {
|
|
|
|
|
LOGGER.error("获取钉钉AccessToken错误", e);
|
|
|
|
|
throw new ImppBusiException("获取钉钉AccessToken错误 " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|