大家好,欢迎来到IT知识分享网。
目录
近期一个项目中需要获取企业微信的打卡记录并同步到本地来进行考勤相关业务的处理,其中涉及了如何获取企业微信的部门组织,如何通过组织架构获取每个部门的人员Userid,然后再获取考勤数据等等。其中必要的条件有 企业ID,通讯录 AppSecre 值,打卡 AppSecre 值。
- 企业ID位置在企业微信管理后台,上方最右侧我的企业>>>企业信息>>最下方就能查找到
- 通讯录Secret值查看位置:在后台管理>>>管理工具>>>通讯录同步
- 打卡Secret值查看位置:应用管理>>>打卡
前期准备工作准备好后,接下来开始处理业务:
先获取AppToken:
public class GetUsersAttenDancereCord {
static DBSession dbSession = DBSessionFactory.createSession();
private static String AppKey = "AppKey";//单位企业微信id
private static String AppSecre = "AppSecre";//通讯录 AppSecre 值
private static String Corpsecret ="Corpsecret";//打卡 AppSecre 值
String starttime = "2022-02-15 00:44:08";
String endtime = "2022-02-16 00:44:08";
public static String getAccToken() throws IOException{// TODO: handle exception 程序在下方}
}
public static String getAccToken() throws IOException{
//当前时间戳
String generalUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; //Token地址
URL url = new URL(generalUrl);
String strRead = null;
StringBuffer sbf = new StringBuffer();
// 连接接口路径
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// 设置请求属性
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 建立连接
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
// 插入Body里面的json数据
JSONObject parm = new JSONObject();
parm.put("corpid", AppKey); //AppKey 单位id
parm.put("corpsecret", AppSecre);//AppSecre 应用corpsecret值
writer.write(parm.toString());
writer.flush();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close(); //关流
connection.disconnect();
String ss =JSONObject.parseObject(sbf.toString()).getString("access_token"); //获取data对象
if("".equals(ss)||ss==null){
System.out.println("获取token失败>>>>>ss的值是:"+ss);
}
return ss; //返回token字符串
}
有了Token再加上企业id就可获取到企业微信的组织架构:
/**
* 获取组织架构id和人员Userid
* @return
*/
private void getAllQiYeUser(String starttime,String endtime,String token) throws Exception {
List<Object> users = new ArrayList<>();
//--拉取企业微信所有部门信息
String generalUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token="+token+"&department_id=1&fetch_child=1"; //Token地址
URL url = new URL(generalUrl);
String strRead = null;
StringBuffer sbf = new StringBuffer();
// 连接接口路径
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// 设置请求属性
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 建立连接
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
// 插入Body里面的json数据
JSONObject parm = new JSONObject();
writer.write(parm.toString());
writer.flush();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close(); //关流
connection.disconnect();
JSONObject object = JSONObject.parseObject(sbf.toString());
//System.out.print(object);
JSONArray jsonArray = new JSONArray(object.get("userlist").toString());
int iSize = jsonArray.length();
List<String> userlist = new ArrayList<String>();//将员工Userid放到List里面
for (int i = 0; i < iSize; i++) {
org.json.JSONObject jsonObject = jsonArray.getJSONObject(i);
String userid = CUtil.convert2String(jsonObject.get("userid"));//转换String的方法 CUtil.convert2String();
userlist.add(userid);
}
List<List<String>> list = fixedGrouping2(userlist, 100); //因为企业微信API一次性只能获取固定人员和30天的数据因此将人员进行拆分
for (List<String> list2 : list) {
getDKData(starttime, endtime, list2,token);
}
}
人员拆分分组
/**
* 将一组数据固定分组,每组n个元素
*
* @param source 要分组的数据源
* @param n 每组n个元素
* @param <T>
* @return
*/
public static <T> List<List<T>> fixedGrouping2(List<T> source, int n) {
if (null == source || source.size() == 0 || n <= 0)
return new ArrayList<List<T>>();
List<List<T>> result = new ArrayList<List<T>>();
int remainder = source.size() % n;
int size = (source.size() / n);
for (int i = 0; i < size; i++) {
List<T> subset = null;
subset = source.subList(i * n, (i + 1) * n);
result.add(subset);
}
if (remainder > 0) {
List<T> subset = null;
subset = source.subList(size * n, size * n + remainder);
result.add(subset);
}
return result;
}
进行考勤数据读取:
public static void getDKData(String starttime,String endtime,List userlist,String token) throws Exception{
System.out.print(userlist);
long kssj = CUtil.convert2Date(starttime).getTime()/1000; //开始时间
long jssj = CUtil.convert2Date(endtime).getTime()/1000; //结束时间
String generalUrl = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token="+getAccToken1()+""; //Token地址
URL url = new URL(generalUrl);
String strRead = null;
StringBuffer sbf = new StringBuffer();
// 连接接口路径
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// 设置请求属性
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 建立连接
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
// 插入Body里面的json数据
JSONObject parm = new JSONObject();
parm.put("opencheckindatatype","3"); //打卡类型。1:上下班打卡;2:外出打卡;3:全部打卡
parm.put("starttime", kssj);
parm.put("endtime", jssj);
parm.put("useridlist", userlist);
writer.write(parm.toString());
writer.flush();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close(); //关流
connection.disconnect();
JSONObject object = JSONObject.parseObject(sbf.toString());
System.out.print(object);
JSONArray jsonArray = new JSONArray(object.get("checkindata").toString());//获取打卡数据
int iSize = jsonArray.length();
for (int i = 0; i < iSize; i++) {//循环验证状态
org.json.JSONObject jsonObject = jsonArray.getJSONObject(i);
String deviceid = CUtil.convert2String(jsonObject.get("deviceid"));//获取打卡设备id。如果有值则证明打卡了。如果无则证明没有
String exception_type = CUtil.convert2String(jsonObject.get("exception_type"));//打卡类型 未打卡状态为:未打卡,此处为空的情况为走了补考勤申请因此需要进行两个条件来验证
if (deviceid.length() > 0 || exception_type.length() == 0) {
insterIorecordSync(jsonObject);//进行数据同步操作
}
}
}
参考地址:如何拉取企业微信打卡数据_Soup’s Blog-CSDN博客前言:拉取企业微信打卡数据这个功能我很早就做了,对于我来说我感觉挺简单的,今天我想把他分享出来了,帮助更多有需要的人。本文涉及到阿里巴巴的JsonObject的解析,可参考我的另一篇blog。1)了解企业微信相关文档本文中涉及的一些专业术语可以参考:https://work.weixin.qq.com/api/doc/90000/90135/90665#corpid拉取企业微信打卡数据在OA数据接口这块,这块的功能逻辑主要是这样的,a.企业微信会提供一个接口…https://blog.csdn.net/tangthh123/article/details/107080333?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-107080333.pc_agg_new_rank&utm_term=%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1+%E6%89%93%E5%8D%A1%E6%95%B0%E6%8D%AE%E8%8E%B7%E5%8F%96&spm=1000.2123.3001.4430
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/14445.html