博客
关于我
钉钉审批流的调用(官方推荐审批流)
阅读量:178 次
发布时间:2019-02-28

本文共 4310 字,大约阅读时间需要 14 分钟。

钉钉审批流配置与使用指南

一、审批模板的定义

在钉钉后台管理界面中,管理员需要创建审批模板。通过分析审批模板的 process_code 参数,后续在发送审批时可以直接使用该参数。为实现这一功能,可参考钉钉提供的API文档。

二、审批流发送

1. 获取 access_token

通过以下URL获取访问令牌:

https://oapi.dingtalk.com/gettoken?appkey=appkey&appsecret=appsecret

请注意:在钉钉管理员后台中获取具体的 appkey 和 appsecret。

2. 发送审批申请

发送审批请求的URL地址为:

https://oapi.dingtalk.com/topapi/processinstance/create?access_token=access_token

请求参数示例(JSON格式):

{  "form_component_values": {    "name": "详细信息",    "value": "所在部门:什么部\n所要发布职位名称:java\n职级:p8\n职位要求:会java\n发布理由:缺人"  },  "dept_id": "-1",  "process_code": "process_code",  "originator_user_id": "userid"}

3. 导入相关依赖

在 Maven 项目中,导入以下依赖以支持 HTTP 客户端通信:

org.apache.httpcomponents
httpclient
4.5.10

4. 帮助类代码

提供一个完整的 HTTP 请求工具类,用于处理钉钉 API 调用:

public class DDAuthUntil {    public static JSONObject httpGet(String url) throws IOException {        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();        HttpGet httpGet = new HttpGet(url);        CloseableHttpResponse response = closeableHttpClient.execute(httpGet);        HttpEntity entity = response.getEntity();        String result = EntityUtils.toString(entity, "UTF-8");        JSONObject jsonrResult = JSON.parseObject(result);        closeableHttpClient.close();        return jsonrResult;    }    public static JSONObject httpPost(String url, JSONObject json) throws Exception {        CloseableHttpClient httpClient = HttpClients.createDefault();        JSONObject jsonrResult = null;        HttpPost httpPost = new HttpPost(url);        try {            if (json != null) {                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");                    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) throws Exception {        String access_tokenUrl = "https://oapi.dingtalk.com/gettoken?appkey=dingq8p7srlitsx0xjey&appsecret=S1SzCjx3VAAAzrhCehl1UhgCu3WEygyvVWZsyl_mcpIa3R52sZz6WqvrMDBlQmcI";        JSONObject access_token = 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 = 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);        httpPost(asyncsendurl, jsonObject);        return "发送消息成功";    }}

五、审批流发送代码

审批流的发送代码基于获取用户钉钉手机号获取 userid 的逻辑。主要步骤如下:

1. 获取 access_token

调用以下URL获取访问令牌:

https://oapi.dingtalk.com/gettoken?appkey=appkey&appsecret=appsecret

2. 获取 userid

通过钉钉提供的接口,根据手机号获取用户ID:

https://oapi.dingtalk.com/user/get_by_mobile?access_token=access_token&mobile=mobile

3. 发起审批流

调用以下URL发起审批流程:

https://oapi.dingtalk.com/topapi/processinstance/create?access_token=access_token

4. 参数配置

请求参数需包含以下信息:

  • originator_user_id:发起人的钉钉用户 ID
  • process_code:审批流的代码标识
  • dept_id:部门 ID(默认值为 -1)
  • form_component_values:审批模板的具体内容

六、返回结果

审批流发送成功后,系统返回以下 JSON 格式结果:

{  "errcode": 0,  "process_instance_id": "3f2e9752-ca89-4c-b7-9",  "request_id": "6inrrnit9"}

以上方法可帮助管理员顺利配置钉钉审批流,实现自动化审批需求。

转载地址:http://wstj.baihongyu.com/

你可能感兴趣的文章
python+requests+unittest执行自动化接口测试!
查看>>
Python+Requests编码识别Bug
查看>>
python函数编写_[零基础学python]传说中的函数编写条规
查看>>
Python+selenium —— UI自动化之鼠标操作
查看>>
python函数注释,参数后面加冒号:,函数后面的箭头→是什么?
查看>>
Python+Selenium+Threading进行兼容性测试
查看>>
Python+selenium+unittest的GUI自动化框架实现
查看>>
python函数拟合不规则曲线_Python计算&绘图——曲线拟合问题(转)
查看>>
python函数定义的基本格式_零基础学python-2.19 定义函数、调用函数与默认参数
查看>>
Python+Selenium之数据驱动测试的实现
查看>>
python函数定义与使用+返回值简解
查看>>
Python+Selenium登录
查看>>
Python日期时间模块
查看>>
Python函数合集:足足68个内置函数请收好!
查看>>
Python+Selenium自动化测试项目实战
查看>>
Python+Selenium自动化测试项目实战【建议收藏】
查看>>
Python+Selenium自动化测试:Page Object模式
查看>>
python+selenium进行cnblog的自动化登录测试
查看>>
Python函数只返回第一个值而不是数据框
查看>>
Python+SQL实战:京东用户行为数据分析案例解析(上)
查看>>