You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.dsideal.FengHuang.Util;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.*;
import com.aliyun.tea.TeaException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.jfinal.plugin.redis.Redis;
import com.taobao.api.ApiException;
public class DingTalkUtil {
/**
* 使用 Token 初始化账号Client
*
* @return Client
* @throws Exception
*/
private static com.aliyun.dingtalkoauth2_1_0.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
config.protocol = "https";
config.regionId = "central";
return new com.aliyun.dingtalkoauth2_1_0.Client(config);
}
/*
功能获取钉钉的AccessToken
作者:黄海
时间2023-06-06
*/
public static String getAccessToken(String appKey, String appSecret) throws Exception {
final String KEY = "DingTalkAccessToken";
if (!Redis.use().exists(KEY)) {
String accessToken = null;
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
.setAppKey(appKey)
.setAppSecret(appSecret);
try {
GetAccessTokenResponse res = client.getAccessToken(getAccessTokenRequest);
accessToken = res.getBody().getAccessToken();
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(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)) {
}
}
Redis.use().setex(KEY, 5400, accessToken);//5400:一个半小时过期
return accessToken;
} else {
return Redis.use().get(KEY);
}
}
/*
功能获取钉钉的JsApiToken->ticket
作者:黄海
时间2023-06-06
*/
public static String getJsApiToken(String access_token) throws Exception {
final String KEY = "DingTalkJsApiToken";
if (!Redis.use().exists(KEY)) {
String jsApiToken = null;
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders createJsapiTicketHeaders = new com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders();
createJsapiTicketHeaders.xAcsDingtalkAccessToken = access_token;
try {
CreateJsapiTicketResponse res = client.createJsapiTicketWithOptions(createJsapiTicketHeaders, new com.aliyun.teautil.models.RuntimeOptions());
jsApiToken = res.getBody().getJsapiTicket();
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(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)) {
}
}
Redis.use().setex(KEY, 5400, jsApiToken);//5400:一个半小时过期
return jsApiToken;
} else {
return Redis.use().get(KEY);
}
}
/*
功能获取钉钉的SsoToken
作者:黄海
时间2023-06-06
*/
public static String getSsoToken(String corpId, String ssoSecret) throws Exception {
final String KEY = "DingTalkSsoToken";
if (!Redis.use().exists(KEY)) {
String ssoToken = null;
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
GetSsoAccessTokenRequest getSsoAccessTokenRequest = new GetSsoAccessTokenRequest()
.setCorpid(corpId)
.setSsoSecret(ssoSecret);
try {
GetSsoAccessTokenResponse res = client.getSsoAccessToken(getSsoAccessTokenRequest);
ssoToken = res.getBody().getAccessToken();
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(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)) {
}
}
Redis.use().setex(KEY, 5400, ssoToken);//5400:一个半小时过期
return ssoToken;
} else {
return Redis.use().get(KEY);
}
}
/**
* 功能:获取单个人员信息
*
* @param access_token
* @param userId
* @return
*/
public static String getPerson(String access_token, String userId) {
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiV2UserGetRequest req = new OapiV2UserGetRequest();
req.setUserid(userId);
req.setLanguage("zh_CN");
OapiV2UserGetResponse rsp = client.execute(req, access_token);
return rsp.getBody();
} catch (ApiException e) {
e.printStackTrace();
}
return null;
}
public static String getPersonToken(String appKey, String appSecret) throws Exception {
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
.setClientId(appKey)
.setClientSecret(appSecret)
//.setCode("abcd")
.setRefreshToken("abcd")
.setGrantType("authorization_code");
try {
GetUserTokenResponse res = client.getUserToken(getUserTokenRequest);
System.out.println(res.getBody().getAccessToken());
System.out.println(res.getBody().getRefreshToken());
} catch (TeaException err) {
System.out.println(err);
} catch (Exception _err) {
System.out.println(_err);
}
return null;
}
/**
* 功能:创建部门
*
* @param access_token
* @throws ApiException
*/
public static long createDept(String access_token, String deptName, long parentId, long orderId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/create");
OapiV2DepartmentCreateRequest req = new OapiV2DepartmentCreateRequest();
req.setParentId(parentId);
req.setOrder(orderId);
req.setName(deptName);
OapiV2DepartmentCreateResponse rsp = client.execute(req, access_token);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
if (jo.getLong("errcode") > 0) {
return -1;
}
return jo.getJSONObject("result").getLong("dept_id");
}
/**
* 功能:删除部门
*
* @param access_token
* @param deptId
* @throws ApiException
*/
public static void delDept(String access_token, long deptId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/delete");
OapiV2DepartmentDeleteRequest req = new OapiV2DepartmentDeleteRequest();
req.setDeptId(deptId);
OapiV2DepartmentDeleteResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
/**
* 功能:更新部门信息
*
* @param access_token
* @param deptId
* @param deptName
* @param orderId
* @throws ApiException
*/
public static void updateDept(String access_token, long deptId, String deptName, long orderId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/update");
OapiV2DepartmentUpdateRequest req = new OapiV2DepartmentUpdateRequest();
req.setDeptId(deptId);
req.setOrder(orderId);
req.setName(deptName);
req.setLanguage("zh_CN");
OapiV2DepartmentUpdateResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
/**
* 功能:获取部门列表
*
* @param access_token
* @throws ApiException
*/
public static void getDeptList(String access_token, long dept_id) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
req.setDeptId(dept_id);
req.setLanguage("zh_CN");
OapiV2DepartmentListsubResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
/**
* 功能:创建人员
*
* @param access_token
* @param deptId
* @param userId
* @param personName
* @param tel
* @param zhiWei
* @throws ApiException
*/
public static void createPerson(String access_token, String deptId, String userId, String personName, String tel, String zhiWei) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create");
OapiV2UserCreateRequest req = new OapiV2UserCreateRequest();
req.setUserid(userId);
req.setName(personName);
req.setMobile(tel);
req.setTitle(zhiWei);
req.setDeptIdList(deptId);
OapiV2UserCreateResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
/**
* 功能:删除人员
*
* @param access_token
* @param userId
* @throws ApiException
*/
public static void delPerson(String access_token, String userId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete");
OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest();
req.setUserid(userId);
OapiV2UserDeleteResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
}