public static String getAccessToken(String appKey,String appSecret) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(appKey);
request.setAppsecret(appSecret);
request.setHttpMethod("GET");
OapiGettokenResponse response = null;
try {
response = client.execute(request);
} catch (ApiException e) {
e.printStackTrace();
}
// token有效期是7200s
// 可以将token放入缓存
return response.getAccessToken();
}
//获得部门list id
public static List<OapiDepartmentListResponse.Department> getDeptId(String accessToken) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
OapiDepartmentListRequest request = new OapiDepartmentListRequest();
request.setId("");
request.setHttpMethod("GET");
OapiDepartmentListResponse response_deptId=null;
try {
response_deptId = client.execute(request, accessToken);
} catch (ApiException e) {
e.printStackTrace();
}
List<OapiDepartmentListResponse.Department> deptId_list=response_deptId.getDepartment();
return deptId_list;
}
//获得人员信息
public static List<OapiUserSimplelistResponse.Userlist> getUser(String accessToken){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/simplelist");
OapiUserSimplelistRequest request = new OapiUserSimplelistRequest();
OapiUserSimplelistResponse execute=null;
request.setHttpMethod("GET");
//循环部门编号
List<OapiUserSimplelistResponse.Userlist> allUserlist=new ArrayList<>();
for (OapiDepartmentListResponse.Department dept_id : getDeptId(accessToken)) {
request.setDepartmentId(dept_id.getId());
try {
execute = client.execute(request,accessToken);
//循环每一个部门下的user
allUserlist.addAll(execute.getUserlist());
} catch (ApiException e) {
e.printStackTrace();
}
}
Map<String , OapiUserSimplelistResponse.Userlist> map =new HashMap<>();
for (OapiUserSimplelistResponse.Userlist userlist : allUserlist) {
if (map.containsKey(userlist.getUserid())) {
continue;
}else {
map.put(userlist.getUserid(), userlist);
}
}
List<OapiUserSimplelistResponse.Userlist> person_list=new ArrayList<>();
person_list.addAll(map.values());
return person_list;
}
/**
* 通过员工手机号获取用户的钉钉id
* @param phone 用户手机号
* @param accessToken token getAccessToken()方法获取
* @return
*/
public static String getUseridByPhone(String phone,String accessToken){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
req.setMobile(phone);
OapiV2UserGetbymobileResponse rsp = null;
try {
rsp = client.execute(req, accessToken);
if (rsp.getCode() == null || "60020".equals(rsp.getSubCode())){
return "没有权限";
}
return rsp.getResult().getUserid();
} catch (Exception e) {
System.out.println("号码为"+phone+"的用户未加入钉钉考勤组!");
}
return null;
}
/**
* 获取指定时间内的考勤记录
* @param userid 钉钉id
* @param startTime 开始时间 日期格式 yyyy-MM-dd HH:mm:ss
* @param endTime 结束时间 日期格式 yyyy-MM-dd HH:mm:ss
* @param accessToken token getAccessToken()方法获取
* @return
*/
public static List<OapiAttendanceListResponse.Recordresult> getAtteRecord(String userid,String startTime,String endTime,String accessToken){
// 通过调用接口获取考勤打卡结果
DingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/list");
OapiAttendanceListRequest requestAttendanceListRequest = new OapiAttendanceListRequest();
// 查询考勤打卡记录的起始工作日 "2022-04-27 00:00:00"
requestAttendanceListRequest.setWorkDateFrom(startTime);
// 查询考勤打卡记录的结束工作日 "2022-04-28 00:00:00"
requestAttendanceListRequest.setWorkDateTo(endTime);
// 员工在企业内的userid列表,最多不能超过50个。
requestAttendanceListRequest.setUserIdList(Arrays.asList(userid));
// 表示获取考勤数据的起始点
requestAttendanceListRequest.setOffset(0L);
// 表示获取考勤数据的条数,最大不能超过50条。
requestAttendanceListRequest.setLimit(50L);
OapiAttendanceListResponse response = null;
try {
response = clientDingTalkClient.execute(requestAttendanceListRequest,accessToken);
} catch (ApiException e) {
e.printStackTrace();
}
return response.getRecordresult();
}
/**
* 获取指定时间内的考勤记录
* @param userids 钉钉id
* @param startTime 开始时间 日期格式 yyyy-MM-dd HH:mm:ss
* @param endTime 结束时间 日期格式 yyyy-MM-dd HH:mm:ss
* @param accessToken token getAccessToken()方法获取
* @return
*/
public static List<OapiAttendanceListResponse.Recordresult> getAtteRecord(List<String> userids,String startTime,String endTime,String accessToken){
// 通过调用接口获取考勤打卡结果
DingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/list");
OapiAttendanceListRequest requestAttendanceListRequest = new OapiAttendanceListRequest();
// 查询考勤打卡记录的起始工作日 "2022-04-27 00:00:00"
requestAttendanceListRequest.setWorkDateFrom(startTime);
// 查询考勤打卡记录的结束工作日 "2022-04-28 00:00:00"
requestAttendanceListRequest.setWorkDateTo(endTime);
// 员工在企业内的userid列表,最多不能超过50个。
requestAttendanceListRequest.setUserIdList(userids);
// 表示获取考勤数据的起始点
requestAttendanceListRequest.setOffset(0L);
// 表示获取考勤数据的条数,最大不能超过50条。
requestAttendanceListRequest.setLimit(50L);
OapiAttendanceListResponse response = null;
try {
response = clientDingTalkClient.execute(requestAttendanceListRequest,accessToken);
} catch (ApiException e) {
e.printStackTrace();
}
return response.getRecordresult();
}
public static List<OapiAttendanceListRecordResponse.Recordresult> getAtteRecordDetailList(String userid,String startTime,String endTime,String accessToken){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/listRecord");
OapiAttendanceListRecordRequest req = new OapiAttendanceListRecordRequest();
req.setUserIds(Arrays.asList(userid));
req.setCheckDateFrom(startTime);
req.setCheckDateTo(endTime);
req.setIsI18n(false);
OapiAttendanceListRecordResponse rsp = null;
try {
rsp = client.execute(req, accessToken);
} catch (ApiException e) {
e.printStackTrace();
}
return rsp.getRecordresult();
}