SpringBoot钉钉通讯录信息同步

欢迎访问我的个人博客:www.ifueen.com

上一篇讲了SpringBoot集成钉钉小程序和H5微应用实现免密登录功能,这里再介绍一下从钉钉通讯录将人员信息同步到自身系统中的一个实际过程,注意,这里面的有些代码没有的在上一篇文章中

首先要清楚钉钉下面人员结构概念,大体是先选择部门然后再在下方查询人员,当然,部门也是有子部门等层级,我们可以先把部门信息同步到自身系统中,当然,这是根据业务来决定需不需要同步部门

部门同步

public void dingSyncDept() {
        String token = AccessTokenUtil.getToken();
        //获取所有部门id
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
            OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
            req.setDeptId(1L);
            OapiV2DepartmentListsubResponse rsp = null;
            rsp = client.execute(req, token);
            List<OapiV2DepartmentListsubResponse.DeptBaseResponse> result = rsp.getResult();
            if (CollectionUtil.isNotEmpty(result)){
                log.info("----------开始封装子部门");
                //封装子部门
                departmentDept(null,token,result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

private void departmentDept(Dept parentDept,String token, List<OapiV2DepartmentListsubResponse.DeptBaseResponse> result) {
        for (OapiV2DepartmentListsubResponse.DeptBaseResponse deptBaseResponse : result){
            try {
                log.info("------------开始查询"+deptBaseResponse.getName()+"的子部门");
                //查询子部门
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
                OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
                req.setDeptId(deptBaseResponse.getDeptId());
                OapiV2DepartmentListsubResponse rsp = null;
                rsp = client.execute(req, token);
                List<OapiV2DepartmentListsubResponse.DeptBaseResponse> subResult = rsp.getResult();
                //查询是否存在数据库中
                String name = deptBaseResponse.getName();
                //subResult即是当前部门的子部门,这里部门信息全部拿到,后面可以根据自身业务进行后续操作
                if (CollectionUtil.isNotEmpty(subResult)){
                    log.info("-----------"+deptBaseResponse.getName()+"下面一共有"+subResult.size()+"个子部门");
                    departmentDept(currentDept,token,subResult);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

部门同步完成,下面开始同步全部人员信息

/**
     * 同步钉钉下面员工信息到数据库
     * @return
     */
    public void dingSync() {
        String token = AccessTokenUtil.getToken();
        //获取所有部门id
        List<Long> deptIds = getdeptId(token);

        //获取部门下面所有人员信息并同步
        syncUserAll(deptIds,token);
    }


private List<Long> getdeptId(String token) {
        try {
            List<Long> deptIds = new ArrayList<>();
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
            OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
            req.setDeptId(1L);
            OapiV2DepartmentListsubidResponse rsp = null;
            rsp = client.execute(req, token);
            OapiV2DepartmentListsubidResponse.DeptListSubIdResponse result = rsp.getResult();
            List<Long> deptIdList = result.getDeptIdList();
            deptIds.add(1L);
            if (CollectionUtil.isNotEmpty(deptIdList)){
                //封装子部门
                department(deptIds,token,deptIdList);
            }
            return deptIds;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

private void department(List<Long> deptIds, String token, List<Long> deptIdList) {
        try {
            for (Long deptId : deptIdList){
                deptIds.add(deptId);
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
                OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
                req.setDeptId(deptId);
                OapiV2DepartmentListsubidResponse rsp = null;
                rsp = client.execute(req, token);
                OapiV2DepartmentListsubidResponse.DeptListSubIdResponse result = rsp.getResult();
                List<Long> subDeptIds = result.getDeptIdList();
                if (CollectionUtil.isNotEmpty(subDeptIds)){
                    //封装子部门
                    department(deptIds,token,subDeptIds);
                }
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

private void syncUserAll(List<Long> deptIds, String token) {
        log.info("---------一共"+deptIds.size()+"个部门");
        try {
            for (int i = 0; i < deptIds.size();i++){
                Long deptId = deptIds.get(i);
                log.info("------------第"+(i+1)+"个部门人员开始同步");
                String currentDeptId = null;
                //当前部门详情
                DingTalkClient clientDept = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
                OapiV2DepartmentGetRequest reqDept = new OapiV2DepartmentGetRequest();
                reqDept.setDeptId(deptId);
                reqDept.setLanguage("zh_CN");
                OapiV2DepartmentGetResponse rspDept = clientDept.execute(reqDept, token);
                OapiV2DepartmentGetResponse.DeptGetResponse resultDept = rspDept.getResult();
                String deptName = resultDept.getName();
                
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
                OapiV2UserListRequest req = new OapiV2UserListRequest();
                req.setDeptId(deptId);
                req.setCursor(0L);
                req.setSize(100L);
                req.setOrderField("modify_desc");
                req.setContainAccessLimit(false);
                req.setLanguage("zh_CN");
                OapiV2UserListResponse rsp = null;
                rsp = client.execute(req, token);
                OapiV2UserListResponse.PageResult result = rsp.getResult();
                List<OapiV2UserListResponse.ListUserResponse> userrespList = result.getList();
                log.info("-------------当前部门一共"+userrespList.size()+"人");
                if (CollectionUtil.isNotEmpty(userrespList)){
                    //这里拿到人员信息,然后根据自身业务进行操作
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

大概讲一下这里的逻辑,我的逻辑就是先遍历查询到所有的部门,然后把他们放到一个集合当中,再遍历这个集合,分别查询出每个部门下面的人员信息,然后拿到人员信息之后就可以根据自身的功能进行后续的操作,因为我是按照我们的一个需求来写的,所以代码写的有些粗糙,主要是提供一个思路方向

你可能感兴趣的:(spring,boot,钉钉,后端)