采用定时任务,企业微信限制是每分钟不超过600次,因此我们可以设置定时任务,每分钟获取一次
HttpsClient http = new HttpsClient();
HashMap<String, Object> json = new HashMap<>();
HashMap<String, String> headers = new HashMap<>(3);
String abc=http.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=“+企业微信id+”&corpsecret="+corpsecret);//获取方法查看企业微信api
HashMap hashMap1 = JSON.parseObject(abc, HashMap.class);
String access_token = (String) hashMap1.get("access_token");
String url1="https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo?access_token="+access_token;
long timestamp1 = (System.currentTimeMillis() / 1000)-60;
long timestamp2 = System.currentTimeMillis() / 1000;
json.put("starttime", timestamp1);
json.put("endtime", timestamp2);
json.put("cursor", 0);
json.put("size", 100);
JSONObject json1 = new JSONObject(json);
String s = json1.toString();
headers.put("content-type", "application/json");
String def = http.sendPostWithJson(url1, s, headers);
HashMap hashMap2 = JSON.parseObject(def, HashMap.class);
List<String> result = (List<String>) hashMap2.get("sp_no_list");
//循环从单号获取信息
for(int i=0;i<result.size();i++) {
HashMap<String, Object> json2 = new HashMap<>();
json2.put("sp_no",result.get(i));
JSONObject json3 = new JSONObject(json2);
String s2=json3.toString();
String url2="https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token="+access_token;
String temp = http.sendPostWithJson(url2, s2, headers);
HashMap temp1 = JSON.parseObject(temp, HashMap.class);
String id =(String) (((Map) ((List) ((Map) ((Map) ((Map) ((List) ((Map) ((Map) temp1.get("info")).get("apply_data")).get("contents")).get(0)).get("value")).get("selector")).get("options")).get(0)).get("key"));
//获取图片
List fileId = (List) ((Map) ((Map) ((List) ((Map) ((Map) temp1.get("info")).get("apply_data")).get("contents")).get(4)).get("value")).get("files");
for(int j=0;j<fileId.size();j++) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
String purl="https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token="+access_token+"&media_id="+((Map) fileId.get(j)).get("file_id");
HttpGet httpGet = new HttpGet(purl);
CloseableHttpResponse response = httpclient.execute(httpGet);
try {
HttpEntity entity = response.getEntity();
InputStream inStream = entity.getContent();
long name = System.currentTimeMillis() / 1000;
String tt=文件夹路径;
FileOutputStream fw = new FileOutputStream(tt, false);
int b = inStream.read();
while (b != -1) {
fw.write(b);
b = inStream.read();
}
fw.close();
EntityUtils.consume(entity);
} finally {
response.close();
}
}finally {
httpclient.close();
}
}
}