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();        }    }}
评论