调用企业微信api,获取打卡记录
1、获取企业微信token
获取access_token - 接口文档 - 企业微信开发者中心
请求方式:POST(HTTPS)
请求地址:/cgi-bin/checkin/getcheckindata?access_token=ACCESS_TOKEN
2、获取所有人员数据
获取部门成员 - 接口文档 - 企业微信开发者中心
请求方式:GET(HTTPS)
请求地址:/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=1&fetch_child=1
3、获取打卡记录数据
获取打卡记录数据 - 接口文档 - 企业微信开发者中心
请求方式:POST(HTTPS)
请求地址:/cgi-bin/checkin/getcheckindata?access_token=ACCESS_TOKEN
4、最后是源代码
import ;
import ;
import .*;
import ;
import ;
import ;
import .*;
public class HttpUtil {
public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final String Corpid = "企业ID";//替换成自己的企业ID
public static final String Corpsecret = "应用Secret";//替换成打卡应用的secret
public static final String GetToken = "/cgi-bin/gettoken";
public static final String GetCheckindata = "/cgi-bin/checkin/getcheckindata";
public static final String GetUserlist = "/cgi-bin/user/simplelist";
public static final Integer MAX_NUM = 100;//企业微信打卡每次最多取100条记录
public static void main(String[] args) throws Exception {
String rq = GetToken + "?Corp&Corpsecret=" + Corpsecret;
JSONObject gettokenRs = (doGet(rq));
String token = ("access_token");//获取token
JSONObject body = new JSONObject();
List<String> idList = new ArrayList<>();
//获取所有人员信息
JSONObject getuserlistRs = (doGet(GetUserlist+ "?access_token=" + token+"&department_id=1&fetch_child=1"));
JSONArray userlist = ("userlist");
final int ulsize = ();
for (int i = 0; i < ulsize; i++) {
JSONObject udata = (i);
String depuserId = ("userid");
(depuserId);
}
//每100人为1组取数据
final int idlsize = ();
for (int j = 0 ; j < idlsize; j+= MAX_NUM) {
List<String> useridlist = (j, j+MAX_NUM>()?():j+MAX_NUM);
("opencheckindatatype", "3");
Calendar calendar = ();
(Calendar.HOUR_OF_DAY, 0);
(, 0);
(, 0);
(, -1);
("starttime", () / 1000);
(, 2);
("endtime", () / 1000);
("useridlist", useridlist);
JSONObject checkobj = (doPost(GetCheckindata + "?access_token=" + token, body));//取打卡数据
JSONArray checkindata = ("checkindata");
final int ckdsize = ();
for (int i = 0; i < ckdsize; i++) {
JSONObject data = (i);
String userId = ("userid");
String type = ("checkin_type");
String location = ("location_detail");//打卡地点
String longitude = ("lng");
String latitude = ("lat");
Long ckTime = ("checkin_time");//打卡时间毫秒数
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = (ckTime * 1000);//毫秒数转换成日期格式
String sql = "insert into HrCardData (userId,cardType,cardDate,cardTime,addr,longitude,latitude) values ('" + userId + "','" + type + "','" + (" ")[0] + "','" + (" ")[1] + "','" + location + "','" + longitude + "','" + latitude + "')";
(sql);
}
}
}
/**
* get请求
*
* @param getUrl
* @return
*/
public static String doGet(String getUrl) {
try {
URL url = new URL(getUrl);
//设置连接方式
HttpURLConnection conn = (HttpURLConnection) ();
("GET");
//设置主机连接时间超时时间3000毫秒
(3000);
//设置读取远程返回数据的时间3000毫秒
(3000);
//发送请求
();
//获取输入流
InputStream is = ();
//封装输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
//接收读取数据
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = ()) != null) {
(line);
("\r\n");
}
if (null != br) {
();
}
if (null != is) {
();
}
//关闭连接
();
return ();
} catch (Exception e) {
();
}
return "";
}
/**
* 原生http请求
*
* @param sendUrl 请求的Url
* @param body 传入的参数
* @return
*/
public static String doPost(String sendUrl, JSONObject body) {
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
HttpURLConnection conn = null;
try {
URL url = new URL(sendUrl);
conn = (HttpURLConnection) ();
("POST");
//发送POST请求必须设置为true
(true);
(true);
//设置连接超时时间和读取超时时间
(30000);
(10000);
("Content-Type", "application/json");
("Accept", "application/json");
//获取输出流
out = new OutputStreamWriter(());
(());
();
();
//取得输入流,并使用Reader读取
if (200 == ()) {
in = new BufferedReader(new InputStreamReader((), "UTF-8"));
String line;
while ((line = ()) != null) {
(line);
(line);
}
} else {
("ResponseCode is an error code:" + ());
}
} catch (Exception e) {
();
} finally {
try {
if (out != null) {
();
}
if (in != null) {
();
}
} catch (IOException ioe) {
();
}
}
return ();
}
/**
* 将map转String
*
* @param map
* @return
*/
private static String mapToString(Map<String, String> map) {
StringBuilder sb = new StringBuilder();
int index = 0;
for (String i : ()) {
index++;
if (() == index) {
(i + "=" + (i));
} else {
(i + "=" + (i) + "&");
}
}
return ();
}
}