1、创建钉钉应用
1.1、 登录钉钉开放平台
https://open-dev.dingtalk.com/
1.2、创建钉钉应用
点击开发者后台
点击应用开发
点击创建应用
创建成功之后需要开通对应的权限
2、java集成钉钉开发
2.1、导入依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
2.2、在若依字典管理中添加相关配置
字典数据
图中APP_KEY 、APP_SECERT从钉钉开放平台获取
图中ACCESS_TOKEN_URL是获取token值
图中DEPT_API是获取部门信息的URL值
图中USER_ID_API是获取部门下有哪些用户id集合
图中USER_DETAIL_API是根据用户id查询到用户详细信息
2.3、同步部门代码开发
示例实现同步父节点下的所有部门信息
2.3.1、controller层
/**
* 同步部门数据 全量同步
*/
@GetMapping("/syncListDept")
@Operation(summary = "同步部门数据")
public void syncListDept() throws ApiException {
deptService.syncListDept();
}
2.3.2、service层
接口层
/**
* 同步部门数据
*/
void syncListDept() throws ApiException;
实现层
@Override
public void syncListDept() throws ApiException {
//获取accessToken
String token = accessTokenUtils.getToken();
this.insertListDept(token,1L);
}
/**
* 根据部门id获取子部门列表
* @param token
* @param deptId
*/
private void insertListDept(String token,Long deptId) throws ApiException {
DeptDO deptDO = deptMapper.selectById(deptId);
OapiV2DepartmentListsubResponse rootObject = this.getListDept(token, deptId);
//判断是否成功
checkIsSuccess(rootObject);
List<OapiV2DepartmentListsubResponse.DeptBaseResponse> result = rootObject.getResult();
if(result.size() == 0){
return;
}
int num = 1;
for (OapiV2DepartmentListsubResponse.DeptBaseResponse deptBaseResponse : result) {
DeptDO sd = new DeptDO();
// 部门ID
sd.setId(deptBaseResponse.getDeptId());
// 部门名称
sd.setName(deptBaseResponse.getName());
// 父级部门名称
sd.setParentId(deptDO.getId());
sd.setStatus(0);
sd.setSort(num++);
DeptDO dept = deptMapper.selectById(deptBaseResponse.getDeptId());
if(null==dept){
deptMapper.insert(sd);
}else {
deptMapper.updateById(sd);
}
insertListDept(token,sd.getId());
}
}
/**
* 获取子部门详情
* @param token
* @param deptId
* @return
*/
private OapiV2DepartmentListsubResponse getListDept(String token,Long deptId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient(DictFrameworkUtils.parseDictDataValue(DictTypeConstants.DINGDING_INTEGRATION, DingDingEnum.DEPT_API.getStr()));
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
req.setDeptId(deptId);
return client.execute(req, token);
}
public void checkIsSuccess(OapiV2DepartmentListsubResponse response){
if (0L!=response.getErrcode()) {
throw new ServiceException(new ErrorCode(response.getErrcode().intValue(),response.getErrmsg()));
}
}
2.3.3、获取token工具类
/***
* @title yudao-cloud
* @description 获取access_token工具类
* @author wangxiaoqi
* @version 1.0.0
* @create 2023-08-30 10:42
**/
@Component
public class AccessTokenUtils {
@Cacheable(value = RedisKeyConstants.ACCESS_TOKEN + "#180")
public String getToken() throws RuntimeException {
try {
DefaultDingTalkClient client = new DefaultDingTalkClient(DictFrameworkUtils.parseDictDataValue(DictTypeConstants.DINGDING_INTEGRATION, DingDingEnum.ACCESS_TOKEN_URL.getStr()));
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(DictFrameworkUtils.parseDictDataValue(DictTypeConstants.DINGDING_INTEGRATION, DingDingEnum.APP_KEY.getStr()));
request.setAppsecret(DictFrameworkUtils.parseDictDataValue(DictTypeConstants.DINGDING_INTEGRATION, DingDingEnum.APP_SECRET.getStr()));
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
return response.getAccessToken();
} catch (ApiException e) {
throw new RuntimeException();
}
}
}
2.4、最终得到的数据
2.5、同步用户代码开发
示例实现获取一个部门下所有用户
2.5.1、controller层
@PostMapping("/syncUser")
@Operation(summary = "同步用户")
public void syncUser(@RequestParam("deptId") String deptId) throws Exception {
userService.syncUser(deptId);
}
2.5.2、service层
接口层
/**
* 同步可部门下的用户信息
* @param deptId 部门id
*/
void syncUser(String deptId) throws ApiException;
实现层
@Override
public void syncUser(String deptId) throws ApiException {
String token = accessTokenUtils.getToken();
//获取部门下的用户Id集合
OapiUserListidResponse response = getUserIdList(token, Long.parseLong(deptId));
if(response.isSuccess()){
List<String> useridList = response.getResult().getUseridList();
for (String userId : useridList) {
//获取用户的详情信息
OapiV2UserGetResponse userDetailResponse = getUserDetail(userId, token);
if(userDetailResponse.isSuccess()){
OapiV2UserGetResponse.UserGetResponse result = userDetailResponse.getResult();
AdminUserDO adminUserDO = new AdminUserDO();
adminUserDO.setUsername(result.getMobile());
adminUserDO.setNickname(result.getName());
adminUserDO.setDeptId(Long.parseLong(deptId));
adminUserDO.setMobile(result.getMobile());
// 校验正确性
validateUserForCreateOrUpdate(null, adminUserDO.getUsername(), adminUserDO.getMobile(), adminUserDO.getEmail(),
adminUserDO.getDeptId(), adminUserDO.getPostIds());
// 默认开启
adminUserDO.setStatus(CommonStatusEnum.ENABLE.getStatus());
// 加密密码
adminUserDO.setPassword(encodePassword("123456"));
userMapper.insert(adminUserDO);
}
}
}
}
/**
* 通过部门id获取部门下的所有用户Ids
* @param token
* @param deptId
* @return
* @throws ApiException
*/
private OapiUserListidResponse getUserIdList(String token,Long deptId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient(DictFrameworkUtils.parseDictDataValue(DictTypeConstants.DINGDING_INTEGRATION, DingDingEnum.USER_ID_API.getStr()));
OapiUserListidRequest req = new OapiUserListidRequest();
req.setDeptId(deptId);
return client.execute(req, token);
}
/**
* 通过用户id获取用户详情信息
* @param userId
* @param token
* @return
* @throws ApiException
*/
private OapiV2UserGetResponse getUserDetail(String userId,String token) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient(DictFrameworkUtils.parseDictDataValue(DictTypeConstants.DINGDING_INTEGRATION, DingDingEnum.USER_DETAIL_API.getStr()));
OapiV2UserGetRequest req = new OapiV2UserGetRequest();
req.setUserid(userId);
return client.execute(req, token);
}