W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
大家好,我是 V 哥。最近的一個(gè)項(xiàng)目中,用到企業(yè)微信的審批流程,整理出來(lái)分享給大家。在企業(yè)微信中實(shí)現(xiàn)審批流程可以通過(guò)調(diào)用企業(yè)微信的開(kāi)放API完成,企業(yè)微信提供了審批應(yīng)用接口,用于創(chuàng)建審批模板、發(fā)起審批流程以及獲取審批實(shí)例詳情。下面 V 哥用一個(gè)Java示例代碼,來(lái)展示如何在企業(yè)微信中實(shí)現(xiàn)審批流程。
以下代碼使用HttpClient
發(fā)起HTTP請(qǐng)求來(lái)調(diào)用企業(yè)微信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 {
//下面三個(gè)常量定義,需要用你自己的(企業(yè)微信開(kāi)放平臺(tái))
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();
}
}
}
// 發(fā)起審批流程
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(); // 返回審批單編號(hào)
}
}
}
// 查詢審批流程狀態(tài)
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. 發(fā)起審批流程
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", "請(qǐng)假事由")),
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", "請(qǐng)假申請(qǐng)"))
});
String spNo = initiateApproval(accessToken, approvalData);
System.out.println("審批單號(hào): " + spNo);
// 3. 查詢審批狀態(tài)
Map<String, Object> approvalDetail = getApprovalDetail(accessToken, spNo);
System.out.println("審批詳情: " + approvalDetail);
} catch (IOException e) {
e.printStackTrace();
}
}
}
getAccessToken
方法獲取企業(yè)微信的access_token
,用于后續(xù)接口調(diào)用。initiateApproval
方法通過(guò)oa/applyevent
接口發(fā)起審批流程,傳入審批模板ID和審批表單數(shù)據(jù)(如審批人、申請(qǐng)數(shù)據(jù)和摘要等)。getApprovalDetail
方法通過(guò)oa/getapprovaldetail
接口查詢審批詳情,包括審批狀態(tài)和各環(huán)節(jié)的處理結(jié)果。template_id
:審批模板ID,由企業(yè)微信審批應(yīng)用中創(chuàng)建。approver
:審批人信息,可以指定具體審批人或?qū)徟私巧?/li>
apply_data
:審批申請(qǐng)數(shù)據(jù),包含表單控件的數(shù)據(jù)內(nèi)容。summary_list
:摘要信息,用于在審批列表顯示申請(qǐng)概要信息。sp_no
:審批單編號(hào),用于查詢審批狀態(tài)。以上是實(shí)現(xiàn)企業(yè)微信中的審批流程,如何你也在開(kāi)發(fā)類似的功能,希望可以幫助到你。關(guān)注威哥愛(ài)編程,編碼路上作個(gè)同行人。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: