参考:官方文档
企业可通过打卡应用Secret调用本接口,获取指定员工指定时间段内的打卡记录数据。
第三方应用可获取应用可见范围内指定员工指定日期的打卡记录数据。
请求方式:POST(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=ACCESS_TOKEN
JAVA程序
/**
* opencheckindatatype 是 打卡类型。1:上下班打卡;2:外出打卡;3:全部打卡
* starttime 是 获取打卡记录的开始时间。Unix时间戳
* endtime 是 获取打卡记录的结束时间。Unix时间戳
* useridlist 是 需要获取打卡记录的用户列表
*
* @return
*/
@Override
public Integer countsUpadte() {
int n = 0;
// 1.查询出所有用户
List<WxUserlist> userlists = wxUserlistService.list(null);
//请求体数据,使用hashmap封装
HashMap<String, Object> hashMap = MapUtils.newHashMap();
//参数:开始时间戳(秒)//7天前0:0:0的时间戳(秒)
Long starttime = TjDateUtils.chooseDay(TjDateUtils.getTodayBegin(), -7).getTime() / 1000;
//参数:结束时间戳,当前时间的时间戳(秒)
Long endtime = System.currentTimeMillis() / 1000;
// 2. 用户列表不超过100个。若用户超过100个,请分批获取
for (int i = 0; i < userlists.size(); i += 100) {
hashMap.clear(); //清空缓存,是用来复用的。
hashMap.put("opencheckindatatype", 3);
hashMap.put("starttime", starttime);
hashMap.put("endtime", endtime);
ArrayList<String> useridlist = ListUtils.newArrayList();
//循环获取用户的id,加入list中
for (int j = 0; j < 100; j++) {
if (j + i < userlists.size()) {
useridlist.add(userlists.get(j + i).getUserid());
}
}
hashMap.put("useridlist", useridlist);
log.info("{}:结果:{}", i, hashMap);
String token = baseNumvarService.getAccessTokenBySecret(baseNumvarService.getSecretCheckin());
String url = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=" + token;
//执行请求
Map map = restTemplate.postForObject(url, hashMap, Map.class);
if (0 == (Integer) map.get("errcode")) {
//map数据结构转为java对象结构
List<WxCheckin> wxCheckinList = JSON.parseArray(JSON.toJSONString(map.get("checkindata")), WxCheckin.class);
log.info("返回的数据信息:*****************{}", wxCheckinList);
if (wxCheckinList != null && wxCheckinList.size() > 0) {
//更新打卡的数据信息
for (WxCheckin wxCheckin : wxCheckinList) {
wxCheckin.setId_time(wxCheckin.getUserid() + wxCheckin.getCheckin_time());
saveOrUpdate(wxCheckin);
n++;//记录更新了多少个记录
}
}
}
}
log.info("一共更新了多少条记录:{}", n);
return n;
}