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.
152 lines
5.5 KiB
152 lines
5.5 KiB
/* |
|
* Copyright (C), 2018-2021 |
|
* Project: starter |
|
* FileName: DingAPI |
|
* Author: Louis |
|
* Date: 2021/5/13 16:16 |
|
*/ |
|
package com.fr.plugin.mqh.dingtalksyn.utils; |
|
|
|
import com.dingtalk.api.DefaultDingTalkClient; |
|
import com.dingtalk.api.DingTalkClient; |
|
import com.dingtalk.api.request.*; |
|
import com.dingtalk.api.response.*; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fr.plugin.mqh.dingtalksyn.config.DingSynConfig; |
|
import com.taobao.api.ApiException; |
|
|
|
import java.util.List; |
|
|
|
/** |
|
* <Function Description><br> |
|
* <DingAPI> |
|
* |
|
* @author Louis |
|
* @since 1.0.0 |
|
*/ |
|
public class DingAPI { |
|
public static final int TOKEN_EXPIRE_TIME = 3600000; |
|
/** |
|
* 获取企业内部应用的access_token |
|
*/ |
|
public static final String GETTOKEN = "https://oapi.dingtalk.com/gettoken"; |
|
/** |
|
* 通过免登码获取用户信息 |
|
*/ |
|
public static final String GET_USER_INFO = "https://oapi.dingtalk.com/user/getuserinfo"; |
|
public static final String USER_GET = "https://oapi.dingtalk.com/topapi/v2/user/get"; |
|
public static final String DEPARTMENT_LIST = "https://oapi.dingtalk.com/topapi/v2/department/listsub"; |
|
public static final String GET_DEPT_MEMBER = "https://oapi.dingtalk.com/topapi/user/listid"; |
|
public static final String GET_DEPT = "https://oapi.dingtalk.com/topapi/v2/department/get"; |
|
public static final String ROLE_LIST = "https://oapi.dingtalk.com/topapi/role/list"; |
|
public static final String GETROLE = "https://oapi.dingtalk.com/topapi/role/getrole"; |
|
|
|
/** |
|
* 根据deptId获取用户详情 |
|
* |
|
* @param deptId |
|
* @return |
|
* @throws ApiException |
|
*/ |
|
public static OapiV2DepartmentGetResponse.DeptGetResponse getDeptResponse(long deptId) throws ApiException { |
|
DingTalkClient client = new DefaultDingTalkClient(GET_DEPT); |
|
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest(); |
|
req.setDeptId(deptId); |
|
req.setLanguage("zh_CN"); |
|
OapiV2DepartmentGetResponse rsp = client.execute(req, DingTokenUtils.getAccessToken()); |
|
return rsp.getResult(); |
|
} |
|
|
|
/** |
|
* 获取部门用户userid列表 |
|
* |
|
* @param deptId |
|
* @return |
|
* @throws ApiException |
|
*/ |
|
public static List<String> getDeptMember(long deptId) throws ApiException { |
|
DingTalkClient client = new DefaultDingTalkClient(GET_DEPT_MEMBER); |
|
OapiUserListidRequest req = new OapiUserListidRequest(); |
|
req.setDeptId(deptId); |
|
OapiUserListidResponse rsp = client.execute(req, DingTokenUtils.getAccessToken()); |
|
return rsp.getResult().getUseridList(); |
|
} |
|
|
|
/** |
|
* 获取部门列表 |
|
* |
|
* @return |
|
* @throws ApiException |
|
*/ |
|
public static List<OapiV2DepartmentListsubResponse.DeptBaseResponse> getDepartmentList(long deptId) throws ApiException { |
|
DingTalkClient client = new DefaultDingTalkClient(DEPARTMENT_LIST); |
|
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest(); |
|
req.setDeptId(deptId); |
|
req.setLanguage("zh_CN"); |
|
OapiV2DepartmentListsubResponse rsp = client.execute(req, DingTokenUtils.getAccessToken()); |
|
return rsp.getResult(); |
|
} |
|
|
|
/** |
|
* 根据roleId获取角色详情 |
|
* |
|
* @param roleId |
|
* @return |
|
* @throws ApiException |
|
*/ |
|
public static OapiRoleGetroleResponse.OpenRole getRoleResponse(long roleId) throws ApiException { |
|
DingTalkClient client = new DefaultDingTalkClient(GETROLE); |
|
OapiRoleGetroleRequest req = new OapiRoleGetroleRequest(); |
|
req.setRoleId(roleId); |
|
OapiRoleGetroleResponse rsp = client.execute(req, DingTokenUtils.getAccessToken()); |
|
return rsp.getRole(); |
|
} |
|
|
|
/** |
|
* 获取角色列表 |
|
* |
|
* @return |
|
* @throws ApiException |
|
*/ |
|
public static List<OapiRoleListResponse.OpenRoleGroup> getOapiRoleList() throws ApiException { |
|
DingTalkClient client = new DefaultDingTalkClient(ROLE_LIST); |
|
OapiRoleListRequest req = new OapiRoleListRequest(); |
|
req.setSize(200L); |
|
req.setOffset(0L); |
|
OapiRoleListResponse rsp = client.execute(req, DingTokenUtils.getAccessToken()); |
|
return rsp.getResult().getList(); |
|
} |
|
|
|
/** |
|
* 根据userid获取用户详情 |
|
* |
|
* @param userId |
|
* @return |
|
* @throws ApiException |
|
*/ |
|
public static OapiV2UserGetResponse.UserGetResponse getUserResponse(String userId) throws ApiException { |
|
DingTalkClient client = new DefaultDingTalkClient(USER_GET); |
|
OapiV2UserGetRequest req = new OapiV2UserGetRequest(); |
|
req.setUserid(userId); |
|
req.setLanguage("zh_CN"); |
|
OapiV2UserGetResponse rsp = client.execute(req, DingTokenUtils.getAccessToken()); |
|
LogKit.info("dingtalksyn-DingAPI-getUserResponse-rsp:{}", rsp.getBody()); |
|
return rsp.getResult(); |
|
} |
|
|
|
/** |
|
* 获取企业内部应用的access_token |
|
* |
|
* @return |
|
* @throws ApiException |
|
*/ |
|
public static String getAccessToken() throws ApiException { |
|
DingTalkClient client = new DefaultDingTalkClient(GETTOKEN); |
|
OapiGettokenRequest request = new OapiGettokenRequest(); |
|
request.setAppkey(DingSynConfig.getInstance().getAppKey()); |
|
request.setAppsecret(DingSynConfig.getInstance().getAppSecret()); |
|
request.setHttpMethod("GET"); |
|
OapiGettokenResponse response = client.execute(request); |
|
return response.getAccessToken(); |
|
} |
|
} |