钉钉审批流的调用(官方推荐审批流)

1.定义审批模板

参考https://ding-doc.dingtalk.com/doc#/serverapi2/tvu5f4
使管理员在后台的管理界面中创建一个模板,在浏览器url中查找process_code参数记录下来后面发送钉钉审批时需要用到

2.发送审批

Postman测试钉钉审批流发送

1.获取access_token
https://oapi.dingtalk.com/gettoken?appkey=appkey
&appsecret=appsecret";//在钉钉管理员后台中获取appkey和appsecret
2.发送审批申请
地址:https://oapi.dingtalk.com/topapi/processinstance/create?access_token=access_token

{
	"form_component_values": 
		{
			"name": "详细信息",//传入为控件定义的名称,控件详细信息可看官方文档
			"value": "所在部门:什么部 \n 所要发布职位名称:java \n 职级:p8 \n 职位要求:会java \n 发布理由:缺人 "
		},//内容
	"dept_id": "-1",//所在部门根部门为-1
	"process_code": "process_code",//url中的process_code
	"originator_user_id": "userid"//发起人userid可以通过手机或unionid获取
}

钉钉发送请求帮助类
3.需要在maven中导入httpClient坐标

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.10</version>
</dependency>

4.帮助类代码

public class DDAuthUntil {

    public static JSONObject httpGet(String url){
        JSONObject jsonrResult = null;
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonrResult = JSON.parseObject(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonrResult;
    }

    public static JSONObject httpPost(String url,JSONObject json){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonrResult = null;
        HttpPost httpPost = new HttpPost(url);
        try {
            if (null != json){
                StringEntity entity = new StringEntity(json.toString(),"utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
                CloseableHttpResponse response = httpClient.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    String result =EntityUtils.toString(response.getEntity(), "utf-8");
                    System.out.println("result:"+result);
                    jsonrResult = JSONObject.parseObject(result);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonrResult;
    }


    //钉钉工作消息发送帮助类
    public static String sendMessage(String content,String mobile){
        try {
            String access_tokenUrl = "https://oapi.dingtalk.com/gettoken?appkey=dingq8p7srlitsx0xjey&appsecret=S1SzCjx3VAAAzrhCehl1UhgCu3WEygyvVWZsyl_mcpIa3R52sZz6WqvrMDBlQmcI";
            JSONObject access_token = DDAuthUntil.httpGet(access_tokenUrl);
            String accessToken = access_token.getString("access_token");
            String getUseridurl = "https://oapi.dingtalk.com/user/get_by_mobile?access_token="+accessToken+"&mobile="+mobile;
            JSONObject useridJson = DDAuthUntil.httpGet(getUseridurl);
            String userid = useridJson.getString("userid");
            //发送通知
            String asyncsendurl = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token="+accessToken;

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("agent_id","696435982");
            jsonObject.put("userid_list",userid);

            JSONObject msgtype = new JSONObject();
            msgtype.put("msgtype","text");

            JSONObject contents = new JSONObject();
            contents.put("content",content);

            msgtype.put("text",contents);
            jsonObject.put("msg",msgtype);

            System.out.println(jsonObject);
            DDAuthUntil.httpPost(asyncsendurl,jsonObject);
            return "发送消息成功";
        }catch (Exception e){
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}

4.审批流发送代码
我们采用的是获取用户的钉钉手机号来获取userid
*第一个参数 mobile 为手机号,第二个参数processCode为process_code,第三个参数deptId为所在部门

//钉钉审批工作流
    public static void ddApproval(String mobile,String processCode,String deptId,String content){
        //获取accessToken
        String access_tokenUrl = "https://oapi.dingtalk.com/gettoken?appkey=appkey
&appsecret=appsecret";//在钉钉管理员后台中获取appkey和appsecret
        JSONObject access_token = DDAuthUntil.httpGet(access_tokenUrl);
        String accessToken = access_token.getString("access_token");
        //获取userid
        String getUseridurl = "https://oapi.dingtalk.com/user/get_by_mobile?access_token="+accessToken+"&mobile="+mobile;
        JSONObject useridJson = DDAuthUntil.httpGet(getUseridurl);
        String userid = useridJson.getString("userid");
        //发起审批
        String createApproval = "https://oapi.dingtalk.com/topapi/processinstance/create?access_token="+accessToken;
        //用JSONObject封装参数
        JSONObject jsonObject = new JSONObject();
        //封装userid
        jsonObject.put("originator_user_id",userid);
        //url中的process_code
        jsonObject.put("process_code",processCode);
        //所在部门根部门为-1
        jsonObject.put("dept_id",deptId);
        //参数拼接
        JSONObject form_component_values = new JSONObject();
        //组件内容
        form_component_values.put("name","详细信息");
        form_component_values.put("value",content);
        jsonObject.put("form_component_values",form_component_values);
        System.out.println(jsonObject);
        DDAuthUntil.httpPost(createApproval,jsonObject);
    }

该方式采用的是钉钉创建默认的审批流程,如需指定审批人将approvers加入jsonObject 中
参数参考地址:https://ding-doc.dingtalk.com/doc#/serverapi2/cmct1a

6.返回结果

{
    "errcode": 0,
    "process_instance_id": "3f2e9752-ca89-4c-b7-9,//审批流唯一代码
    "request_id": "6inrrnit9" 
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值