|
|
|
@ -1,20 +1,18 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
|
|
import cn.hutool.core.date.DateUnit;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
|
|
import com.dingtalk.api.request.OapiGetJsapiTicketRequest;
|
|
|
|
|
import com.dingtalk.api.request.OapiGettokenRequest;
|
|
|
|
|
import com.dingtalk.api.response.OapiGetJsapiTicketResponse;
|
|
|
|
|
import com.dingtalk.api.response.OapiGettokenResponse;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.plugin.redis.Redis;
|
|
|
|
|
import com.jfinal.plugin.redis.RedisPlugin;
|
|
|
|
|
import com.taobao.api.ApiException;
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
public class TestDingTalk {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -23,7 +21,7 @@ public class TestDingTalk {
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject getAccessToken(String appKey, String appSecret) throws ApiException {
|
|
|
|
|
public static String getAccessToken(String appKey, String appSecret) throws ApiException {
|
|
|
|
|
final String KEY = "DingTalkAccessToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
|
@ -36,12 +34,33 @@ public class TestDingTalk {
|
|
|
|
|
jo.put("start_time", DateTime.now());
|
|
|
|
|
Redis.use().setex(KEY, 5400, jo.toString());//5400:一个半小时过期
|
|
|
|
|
System.out.println("本轮是从从阿里云获取到Token:" + jo.getString("access_token"));
|
|
|
|
|
return jo;
|
|
|
|
|
return jo.getString("access_token");
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(str);
|
|
|
|
|
System.out.println("本轮是从缓存中获取到Token:" + jo.getString("access_token"));
|
|
|
|
|
return jo;
|
|
|
|
|
return jo.getString("access_token");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的JsApiToken,有效期7200秒,即2小时,每小时最好重新获取一次
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getJsApiToken(String access_token) throws ApiException {
|
|
|
|
|
final String KEY = "DingTalkJsApiToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/get_jsapi_ticket");
|
|
|
|
|
OapiGetJsapiTicketRequest req = new OapiGetJsapiTicketRequest();
|
|
|
|
|
req.setHttpMethod("GET");
|
|
|
|
|
OapiGetJsapiTicketResponse rsp = client.execute(req, access_token);
|
|
|
|
|
Redis.use().setex(KEY, 5400, rsp.getBody());//5400:一个半小时过期
|
|
|
|
|
System.out.println("本轮是从从阿里云获取到JsApiToken:" + rsp.getBody());
|
|
|
|
|
return rsp.getBody();
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
System.out.println("本轮是从缓存中获取到JsApiToken:" + str);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -56,9 +75,9 @@ public class TestDingTalk {
|
|
|
|
|
RedisPlugin redis = new RedisPlugin("myRedis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000);
|
|
|
|
|
redis.start();
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= 100; i++) {
|
|
|
|
|
JSONObject jo = getAccessToken(appKey, appSecret);
|
|
|
|
|
System.out.println(jo);
|
|
|
|
|
}
|
|
|
|
|
String accessToken = getAccessToken(appKey, appSecret);
|
|
|
|
|
System.out.println(accessToken);
|
|
|
|
|
String jsApiToken = getJsApiToken(accessToken);
|
|
|
|
|
System.out.println(jsApiToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|