All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hn.dingtalk.DingRoleUtils Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.dingtalk;

import cn.hutool.json.JSONUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRoleAddrolesforempsRequest;
import com.dingtalk.api.request.OapiRoleListRequest;
import com.dingtalk.api.request.OapiRoleRemoverolesforempsRequest;
import com.dingtalk.api.response.OapiRoleAddrolesforempsResponse;
import com.dingtalk.api.response.OapiRoleListResponse;
import com.dingtalk.api.response.OapiRoleRemoverolesforempsResponse;
import com.hn.dingtalk.constant.DingConstant;
import com.hn.dingtalk.exception.DingTalkException;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * 角色管理
 *
 * @author fei
 * 2019-06-29 08:50
 */
@Slf4j
public class DingRoleUtils {

    /**
     * 获取角色列表
     * @param offset 当前页
     * @param size   每页记录数
     * @return {@link  OapiRoleListResponse.PageVo}
     */
    public static OapiRoleListResponse.PageVo list(Long offset, Long size) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/list");
        OapiRoleListRequest request = new OapiRoleListRequest();
        request.setOffset(offset);
        request.setSize(size);
        try {
            OapiRoleListResponse response = client.execute(request, AccessTokenUtil.getToken());
            if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
                log.debug("获取部门列表 success,{}", JSONUtil.toJsonStr(response.getResult()));
                return response.getResult();
            }
            throw new DingTalkException("获取部门列表异常 fail:," + response.getErrmsg());
        } catch (ApiException e) {
            throw new DingTalkException("获取角色列表error," + e.getErrMsg());
        }
    }

    public static List listRole() {
        List allRoles = new ArrayList<>();
        OapiRoleListResponse.PageVo pageVo = list(0L, 100L);
        List list = pageVo.getList();
        for (OapiRoleListResponse.OpenRoleGroup openRoleGroup : list) {
            List roles = openRoleGroup.getRoles();
            for (OapiRoleListResponse.OpenRole role : roles) {
                allRoles.add(role);
            }
        }
        return allRoles;
    }

    /**
     * 批量增加员工角色
     *
     * @param roleIds 角色id数组
     * @param userIds 用户id数组
     */
    public static void addRolesForemps(String roleIds, String userIds) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/addrolesforemps");
        OapiRoleAddrolesforempsRequest request = new OapiRoleAddrolesforempsRequest();
        request.setRoleIds(roleIds);
        request.setUserIds(userIds);

        try {
            OapiRoleAddrolesforempsResponse response = client.execute(request, AccessTokenUtil.getToken());
            if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
                log.debug("批量增加员工角色 success,{}", JSONUtil.toJsonStr(response.getErrmsg()));
                return;
            }
            throw new DingTalkException("批量增加员工角色失败 fail:," + response.getErrmsg());
        } catch (ApiException e) {
            throw new DingTalkException("批量增加员工角色失败 fail:," + e.getErrMsg());
        }
    }

    /**
     * 批量删除员工角色
     *
     * @param roleIds 角色id数组
     * @param userIds 用户id数组
     */
    public static void delRolesForemps(String roleIds, String userIds) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/removerolesforemps");
        OapiRoleRemoverolesforempsRequest request = new OapiRoleRemoverolesforempsRequest();
        request.setRoleIds(roleIds);
        request.setUserIds(userIds);

        try {
            OapiRoleRemoverolesforempsResponse response = client.execute(request, AccessTokenUtil.getToken());
            if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
                log.debug("批量删除员工角色 success,{}", JSONUtil.toJsonStr(response.getErrmsg()));
                return;
            }
            throw new DingTalkException("批量删除员工角色失败 fail:," + response.getErrmsg());
        } catch (ApiException e) {
            throw new DingTalkException("批量删除员工角色失败 fail:," + e.getErrMsg());
        }
    }


    public static void main(String[] args) {
        for (OapiRoleListResponse.OpenRole role : listRole()) {
            System.out.println(role.getName());
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy