本文共 4310 字,大约阅读时间需要 14 分钟。
在钉钉后台管理界面中,管理员需要创建审批模板。通过分析审批模板的 process_code 参数,后续在发送审批时可以直接使用该参数。为实现这一功能,可参考钉钉提供的API文档。
通过以下URL获取访问令牌:
https://oapi.dingtalk.com/gettoken?appkey=appkey&appsecret=appsecret
请注意:在钉钉管理员后台中获取具体的 appkey 和 appsecret。
发送审批请求的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"} 在 Maven 项目中,导入以下依赖以支持 HTTP 客户端通信:
org.apache.httpcomponents httpclient 4.5.10
提供一个完整的 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 的逻辑。主要步骤如下:
调用以下URL获取访问令牌:
https://oapi.dingtalk.com/gettoken?appkey=appkey&appsecret=appsecret
通过钉钉提供的接口,根据手机号获取用户ID:
https://oapi.dingtalk.com/user/get_by_mobile?access_token=access_token&mobile=mobile
调用以下URL发起审批流程:
https://oapi.dingtalk.com/topapi/processinstance/create?access_token=access_token
请求参数需包含以下信息:
originator_user_id:发起人的钉钉用户 IDprocess_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/