In a recent project, the approval process of enterprise WeChat is used and sorted out and shared with everyone. Implementing the approval process in Enterprise WeChat can be completed by calling the open API of Enterprise WeChat. Enterprise WeChat provides an approval application interface for creating approval templates, initiating approval process, and obtaining approval instance details. Below, Brother V uses a Java sample code to show how to implement the approval process in corporate WeChat.
Implementation steps
- Get Enterprise WeChat Access Token: You need to obtain Access Token before accessing the Enterprise WeChat API interface.
- Create an approval template(Skip this step if there is already a template).
- Initiate approval process: Initiate an approval request through the specified template ID.
- Query the approval results: Obtain the status and details of the approval.
The following code usesHttpClient
Initiate an HTTP request to call the enterprise WeChat API interface.
Code Example
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class WeChatApproval { //The following three constant definitions need to be used with your own (Enterprise WeChat Open Platform) private static final String CORP_ID = "Your corp_id"; private static final String CORP_SECRET = "Your corp_secret"; private static final String APPROVAL_TEMPLATE_ID = "Your template_id"; // Approval template ID // Get Access Token public static String getAccessToken() throws IOException { String url = "/cgi-bin/gettoken?corp&corpsecret=" + CORP_SECRET; try (CloseableHttpClient client = ()) { HttpGet request = new HttpGet(url); try (CloseableHttpResponse response = (request)) { String responseBody = (()); Map<String, Object> map = new ObjectMapper().readValue(responseBody, ); return ("access_token").toString(); } } } // Initiate the approval process public static String initiateApproval(String accessToken, Map<String, Object> approvalData) throws IOException { String url = "/cgi-bin/oa/applyevent?access_token=" + accessToken; try (CloseableHttpClient client = ()) { HttpPost post = new HttpPost(url); ("Content-Type", "application/json"); Map<String, Object> requestMap = new HashMap<>(); ("template_id", APPROVAL_TEMPLATE_ID); ("use_template_approver", 1); // Use the approver in the template ("approver", ("approver")); ("apply_data", ("apply_data")); ("summary_list", ("summary_list")); String json = new ObjectMapper().writeValueAsString(requestMap); (new StringEntity(json, "UTF-8")); try (CloseableHttpResponse response = (post)) { String responseBody = (()); Map<String, Object> map = new ObjectMapper().readValue(responseBody, ); return ("sp_no").toString(); // Return to the approval form number } } } // Check the status of the approval process public static Map<String, Object> getApprovalDetail(String accessToken, String spNo) throws IOException { String url = "/cgi-bin/oa/getapprovaldetail?access_token=" + accessToken; try (CloseableHttpClient client = ()) { HttpPost post = new HttpPost(url); ("Content-Type", "application/json"); Map<String, Object> requestMap = new HashMap<>(); ("sp_no", spNo); String json = new ObjectMapper().writeValueAsString(requestMap); (new StringEntity(json, "UTF-8")); try (CloseableHttpResponse response = (post)) { String responseBody = (()); return new ObjectMapper().readValue(responseBody, ); } } } public static void main(String[] args) { try { // 1. Obtain Access Token String accessToken = getAccessToken(); ("Access Token: " + accessToken); // 2. Initiate the approval process Map<String, Object> approvalData = new HashMap<>(); ("approver", new Object[] { ("attr", 1, "userid", new String[] { "approver_userid" }) }); ("apply_data", ( "contents", new Object[] { ("control", "Text", "id", "Text-1", "value", ("text", "Reason for leave")), ("control", "Date", "id", "Date-1", "value", ("date", "2024-11-01")) } )); ("summary_list", new Object[] { ("summary_info", ("text", "Apply for leave")) }); String spNo = initiateApproval(accessToken, approvalData); ("Approval No.: " + spNo); // 3. Check the approval status Map<String, Object> approvalDetail = getApprovalDetail(accessToken, spNo); ("Approval details: " + approvalDetail); } catch (IOException e) { (); } } }
Code description
-
Get Access Token:pass
getAccessToken
Method to obtain the WeChat of the enterpriseaccess_token
, used for subsequent interface calls. -
Initiate approval process:
initiateApproval
Method passedoa/applyevent
Interface initiates the approval process, and pass in the approval template ID and approval form data (such as approver, application data and summary, etc.). -
Check the status of the approval process:
getApprovalDetail
Method passedoa/getapprovaldetail
Interface query approval details, including approval status and processing results of each link.
Core parameter explanation
template_id
: Approval template ID, created in the enterprise WeChat approval application.
approver
: Approver information, you can specify the specific approver or approver role.
apply_data
: Approval application data, including data content of form controls.
summary_list
: Summary information, used to display application summary information in the approval list.
sp_no
: Approval form number, used to check the approval status.
Things to note
Permissions issues: Ensure that the application calling the interface has approval permissions and has configured the enterprise WeChat API call permissions.
Approval template ID: The template ID needs to be obtained when creating an approval template in the enterprise WeChat management background.
Approver configuration: The approver needs to be an enterprise WeChat user and ensure that there are relevant configurations in the approval template.
This is the article about how Java implements the enterprise WeChat approval process. For more relevant Java implementation of enterprise WeChat approval content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!