package com.fr.func; import java.util.ArrayList; import java.util.List; import com.fr.json.JSONObject; import com.fr.script.AbstractFunction; import com.fr.third.org.apache.http.HttpEntity; import com.fr.third.org.apache.http.HttpResponse; import com.fr.third.org.apache.http.client.methods.HttpGet; import com.fr.third.org.apache.http.client.methods.HttpPost; import com.fr.third.org.apache.http.entity.StringEntity; import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; import com.fr.third.org.apache.http.impl.client.HttpClientBuilder; import com.fr.third.org.apache.http.impl.client.HttpClients; import com.fr.third.org.apache.http.protocol.HTTP; import com.fr.third.org.apache.http.util.EntityUtils; public class QywxScheduleFunc extends AbstractFunction { /** * 企业微信日程接口 */ private static final long serialVersionUID = -1L; private static String corpid = "ww31234e3a6ea6af30"; private static String corpsecret = "nhpoa1wBnx5JVeFJrz5gC4HXWyUieK3lpxj1LHswZLc"; public static String getToken(String corpid, String corpsecret) { try { CloseableHttpClient client = HttpClients.createDefault(); String urlget = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret; HttpGet httpGet = new HttpGet(urlget); HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); String jsonstr = EntityUtils.toString(entity, "utf-8"); JSONObject jo = new JSONObject(jsonstr); return jo.getString("access_token"); } catch (Exception e) { e.printStackTrace(); } return null; } @Override public Object run(Object[] args) { // 企业微信日程创建POST try { JSONObject jo = new JSONObject();// 主json JSONObject jSchedule = new JSONObject(); // 日程信息 JSONObject jReminders = new JSONObject(); // 提醒相关信息 List jAttendees = new ArrayList(); // 日程参与者列表 // 日程参与者,可以建立多个,必填 JSONObject jUserid1 = new JSONObject(); jUserid1.put("userid", "userid1"); jAttendees.add(jUserid1); JSONObject jUserid2 = new JSONObject(); jUserid2.put("userid", "userid2"); jAttendees.add(jUserid2); // 提醒相关信息,属性非必填,具体内容参考文档 jReminders.put("is_remind", 1); jReminders.put("remind_before_event_secs", 3600); jReminders.put("is_repeat",1); jReminders.put("repeat_type", 7); jReminders.put("repeat_until", 1606976813); jReminders.put("is_custom_repeat", 1); jReminders.put("repeat_interval", 1); jReminders.put("timezone",8); // 日程信息 jSchedule.put("organizer", "userid1"); // 组织者 必填 jSchedule.put("start_time", 1571274600); // 日程开始时间,Unix时间戳 jSchedule.put("end_time", 1571320210); // 日程结束时间,Unix时间戳 jSchedule.put("summary", "需求评审会议"); // 日程标题 jSchedule.put("description", "2.0版本需求初步评审"); // 日程描述 jSchedule.put("location", "广州国际媒体港10楼1005会议室"); // 日程地址 jSchedule.put("attendees", jAttendees); // 日程参与者,必填 jSchedule.put("reminders", jReminders); // 提醒相关信息 // 主json jo.put("schedule",jSchedule); // 必填 jo.put("agentid", 3010084); // 换成自己的agentid // 接口调用 String access_token = getToken(corpid,corpsecret); String urlString = "https://qyapi.weixin.qq.com/cgi-bin/oa/schedule/add?access_token="+access_token; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(urlString); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); StringEntity entity = new StringEntity(jo.toString(), "UTF-8"); //System.out.println(jo.toString()); httpPost.setEntity(entity); HttpResponse response; response = httpClient.execute(httpPost); // 获取返回值 String reStr = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject reJson = new JSONObject(reStr); if (reJson.getInt("errcode")==0){ return "成功,日程ID:"+reJson.getString("schedule_id"); } else { return "失败!"+reJson.getString("errmsg"); } } catch (Exception e) { e.printStackTrace(); return e.toString(); } } }