最近的一个项目中,用到企业微信的审批流程,整理出来分享给大家。在企业微信中实现审批流程可以通过调用企业微信的开放api完成,企业微信提供了审批应用接口,用于创建审批模板、发起审批流程以及获取审批实例详情。下面 v 哥用一个java示例代码,来展示如何在企业微信中实现审批流程。
实现步骤
- 获取企业微信access token:每次访问企业微信api接口前需要先获取access token。
- 创建审批模板(如已有模板则跳过此步骤)。
- 发起审批流程:通过指定的模板id发起审批请求。
- 查询审批结果:获取审批的状态和详细信息。
以下代码使用httpclient发起http请求来调用企业微信api接口。
代码示例
import org.apache.http.client.methods.closeablehttpresponse;
import org.apache.http.client.methods.httppost;
import org.apache.http.client.methods.httpget;
import org.apache.http.entity.stringentity;
import org.apache.http.impl.client.closeablehttpclient;
import org.apache.http.impl.client.httpclients;
import org.apache.http.util.entityutils;
import com.fasterxml.jackson.databind.objectmapper;
import java.io.ioexception;
import java.util.hashmap;
import java.util.map;
public class wechatapproval {
//下面三个常量定义,需要用你自己的(企业微信开放平台)
private static final string corp_id = "你的corp_id";
private static final string corp_secret = "你的corp_secret";
private static final string approval_template_id = "你的template_id"; // 审批模板id
// 获取 access token
public static string getaccesstoken() throws ioexception {
string url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corp_id + "&corpsecret=" + corp_secret;
try (closeablehttpclient client = httpclients.createdefault()) {
httpget request = new httpget(url);
try (closeablehttpresponse response = client.execute(request)) {
string responsebody = entityutils.tostring(response.getentity());
map<string, object> map = new objectmapper().readvalue(responsebody, map.class);
return map.get("access_token").tostring();
}
}
}
// 发起审批流程
public static string initiateapproval(string accesstoken, map<string, object> approvaldata) throws ioexception {
string url = "https://qyapi.weixin.qq.com/cgi-bin/oa/applyevent?access_token=" + accesstoken;
try (closeablehttpclient client = httpclients.createdefault()) {
httppost post = new httppost(url);
post.setheader("content-type", "application/json");
map<string, object> requestmap = new hashmap<>();
requestmap.put("template_id", approval_template_id);
requestmap.put("use_template_approver", 1); // 使用模板中的审批人
requestmap.put("approver", approvaldata.get("approver"));
requestmap.put("apply_data", approvaldata.get("apply_data"));
requestmap.put("summary_list", approvaldata.get("summary_list"));
string json = new objectmapper().writevalueasstring(requestmap);
post.setentity(new stringentity(json, "utf-8"));
try (closeablehttpresponse response = client.execute(post)) {
string responsebody = entityutils.tostring(response.getentity());
map<string, object> map = new objectmapper().readvalue(responsebody, map.class);
return map.get("sp_no").tostring(); // 返回审批单编号
}
}
}
// 查询审批流程状态
public static map<string, object> getapprovaldetail(string accesstoken, string spno) throws ioexception {
string url = "https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token=" + accesstoken;
try (closeablehttpclient client = httpclients.createdefault()) {
httppost post = new httppost(url);
post.setheader("content-type", "application/json");
map<string, object> requestmap = new hashmap<>();
requestmap.put("sp_no", spno);
string json = new objectmapper().writevalueasstring(requestmap);
post.setentity(new stringentity(json, "utf-8"));
try (closeablehttpresponse response = client.execute(post)) {
string responsebody = entityutils.tostring(response.getentity());
return new objectmapper().readvalue(responsebody, map.class);
}
}
}
public static void main(string[] args) {
try {
// 1. 获取access token
string accesstoken = getaccesstoken();
system.out.println("access token: " + accesstoken);
// 2. 发起审批流程
map<string, object> approvaldata = new hashmap<>();
approvaldata.put("approver", new object[] {
map.of("attr", 1, "userid", new string[] { "approver_userid" })
});
approvaldata.put("apply_data", map.of(
"contents", new object[] {
map.of("control", "text", "id", "text-1", "value", map.of("text", "请假事由")),
map.of("control", "date", "id", "date-1", "value", map.of("date", "2024-11-01"))
}
));
approvaldata.put("summary_list", new object[] {
map.of("summary_info", map.of("text", "请假申请"))
});
string spno = initiateapproval(accesstoken, approvaldata);
system.out.println("审批单号: " + spno);
// 3. 查询审批状态
map<string, object> approvaldetail = getapprovaldetail(accesstoken, spno);
system.out.println("审批详情: " + approvaldetail);
} catch (ioexception e) {
e.printstacktrace();
}
}
}
代码说明
- 获取access token:通过
getaccesstoken方法获取企业微信的access_token,用于后续接口调用。 - 发起审批流程:
initiateapproval方法通过oa/applyevent接口发起审批流程,传入审批模板id和审批表单数据(如审批人、申请数据和摘要等)。 - 查询审批流程状态:
getapprovaldetail方法通过oa/getapprovaldetail接口查询审批详情,包括审批状态和各环节的处理结果。
核心参数解释
template_id:审批模板id,由企业微信审批应用中创建。
approver:审批人信息,可以指定具体审批人或审批人角色。
apply_data:审批申请数据,包含表单控件的数据内容。
summary_list:摘要信息,用于在审批列表显示申请概要信息。
sp_no:审批单编号,用于查询审批状态。
注意事项
权限问题:确保调用接口的应用具有审批权限,且已配置了企业微信api调用权限。
审批模板id:模板id需要在企业微信管理后台中创建审批模板时获取。
审批人配置:审批人需要是企业微信用户,并确保在审批模板中有相关配置。
到此这篇关于详解java如何实现企业微信审批流程的文章就介绍到这了,更多相关java实现企业微信审批内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论