获取钉钉考勤机的打卡记录并且解析

时间:2022-02-15 11:15:57
有个特别恶心的地方就是打卡信息只能最多一次取七天的。所以要注意一下; 时间格式:2018-05-01
accessTokenUrl : https://oapi.dingtalk.com/gettoken
public String getAccessToken() {
   Map<String,Object> map = new HashMap<>();
   map.put("corpid",corpId);
   map.put("corpsecret",corpSecret);
   return HttpClientUtil.httpGetStringResult(accessTokenUrl, map);//获取access_token }
public static String httpGetStringResult(String url,Map<String,Object> param){
   String content = null;
   CloseableHttpClient httpClient = HttpClients.createDefault();
   if(param != null && !param.isEmpty()){
      StringBuffer strparams = new StringBuffer();
      for (Map.Entry<String, Object> map : param.entrySet()) {
         strparams.append(map.getKey()).append("=").append(map.getValue().toString()).append("&");
      }
      strparams = strparams.deleteCharAt(strparams.length()-1);
      url = url + "?" + strparams;
   }
   HttpGet httpGet = new HttpGet(url);
   CloseableHttpResponse response = null;

   try {

      response = httpClient.execute(httpGet);
      HttpEntity entity = response.getEntity();
      content = EntityUtils.toString(entity,"UTF-8");
   } catch (ClientProtocolException e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
   } catch (IOException e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
   }finally {
         try {
            if(null!=response){
               response.close();
            }
         } catch (IOException e) {
            LOG.error(e.getMessage());
            e.printStackTrace();
         }
      }
      LOG.info("httpget----url:"+url +" Json:"+content);
   return content;
}

public void getAttendances(List<AttendanceQvo> users, String workDateFrom, String workDateTo,String access_Token) {
   Map<String,Object> map = new HashMap<>();
   map.put("workDateFrom", workDateFrom);
   map.put("workDateTo", workDateTo);
   map.put("userIdList", "");
   map.put("offset","");
   map.put("limit","");
   String attendanceStr = getAttendance(map, access_Token);
   getAttendanceList(users, attendanceStr);//setAttendanceList 

}

public String getAttendance(Map<String, Object> map ,String access_token_str) {
   String dingDingAttendance = "https://oapi.dingtalk.com/attendance/list?access_token="+access_token_str;
   JSONObject jsonObject = new JSONObject();
   jsonObject.put("workDateFrom",map.get("workDateFrom"));
   jsonObject.put("workDateTo",map.get("workDateTo"));
   jsonObject.put("limit",map.get("limit"));
   jsonObject.put("offset",map.get("offset"));
   jsonObject.put("userIdList",map.get("userIdList"));
   return HttpClientUtil.doPost(dingDingAttendance,jsonObject);//获取考勤记录 }
/**  * post请求  * @param requestUrl  * @param json  * @return  */ public static String doPost(String requestUrl,JSONObject json){
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(requestUrl);
    post.setHeader("Content-Type", "application/json");
    post.addHeader("Authorization", "Basic YWRtaW46");
    String result = "";
    try {
        StringEntity s = new StringEntity(json.toString(), "utf-8");
        s.setContentEncoding(new BasicHeader("contentType",
                   "application/json"));
        post.setEntity(s);
        // 发送请求  HttpResponse httpResponse = client.execute(post);
        // 获取响应输入流  InputStream inStream = httpResponse.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
        StringBuilder strber = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
             strber.append(line + "\n");
        inStream.close();
        result = strber.toString();
        System.out.println(result);
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

           System.out.println("请求服务器成功,做相应处理");

        } else {
          System.out.println("请求服务端失败");
        }
    } catch (Exception e) {
        System.out.println("请求异常");
        throw new RuntimeException(e);
    }
    return result;
}
解析方法:

public void getAttendanceList(List<AttendanceQvo> users, String json) {
       JSONObject firstJson = JSONObject.parseObject(json);
       JSONArray  recordFirst = firstJson.getJSONArray("recordresult");//当前部门下的userList  for(int j=0;j<recordFirst.size();j++) {
           JSONObject record = recordFirst.getJSONObject(j);
           AttendanceQvo attendanceQvo = new AttendanceQvo();
           attendanceQvo.setCheckType(record.getString("checkType"));
           attendanceQvo.setUserId(record.getString("userId"));
           attendanceQvo.setUserCheckTime(record.getLong("userCheckTime"));
           attendanceQvo.setWorkDate(record.getLong("workDate"));
           users.add(attendanceQvo);
       }
   }
 
@Data
public class AttendanceQvo implements Serializable{
    private String checkType;     //上下午  private String userId;        //员工ID  private long userCheckTime;//实际打卡时间  private long workDate;      //工作日  }