同步企业微信中的员工信息到本地数据库
package com.bh.module.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bh.common.utils.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class WeChatDevelopService {
//获取access_token url
private static String contactsTokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
//根据获取单个用户url
private static String doGetUserList = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
//企业id(唯一)
private static String corpid = "*********";
// 企业通讯录密钥
private static String corpsecret = "***********************";
// 获取部门列表url
private static final String getDeptListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
//获取token
private static String access_token;
static {
try {
access_token = WeChatDevelopService.doGetAccessToken();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取所有通讯录成员
* 因为我们要的是所有成员的列表
* 遍历获取
* @return
* @throws Exception
*/
public static JSONArray getUserLists() throws Exception {
JSONArray array = new JSONArray();
if (WeChatDevelopService.getDeptList().get("errcode").toString().equals("0")) {
JSONObject userList = (JSONObject) WeChatDevelopService.getDeptList();
JSONArray jsonArray = (JSONArray) userList.get("userlist");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject job = jsonArray.getJSONObject(i);
WeChatDevelopService.oneEmployee(job.get("userid").toString());
JSONObject contactList = (JSONObject) WeChatDevelopService.oneEmployee(job.get("userid").toString());
array.add(contactList);
}
}
return array;
}
/**
* 获取通讯录中单个成员的信息
*
* @return
* @throws Exception
*/
public static JSONObject oneEmployee(String name) throws Exception {
//创建一个httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个uri对象
URIBuilder uriBuilder = new URIBuilder(doGetUserList);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_token));
nameValuePairs.add(new BasicNameValuePair("userid", name));
uriBuilder.addParameters(nameValuePairs);
HttpGet get = new HttpGet(uriBuilder.build());
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
//取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String objectStr = EntityUtils.toString(entity, "utf-8");
JSONObject jsonObject = JSONObject.parseObject(objectStr);
//关闭httpclient
response.close();
httpClient.close();
return jsonObject;
}
/**
* 获取部门列表
* 参数:access_token
* department_id 部门id
*
* @return
* @throws Exception
*/
public static JSONObject getDeptList() throws Exception {
//创建一个httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个uri对象
URIBuilder uriBuilder = new URIBuilder(getDeptListURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", access_token));
nameValuePairs.add(new BasicNameValuePair("department_id", "1"));
nameValuePairs.add(new BasicNameValuePair("fetch_child", "1"));
uriBuilder.addParameters(nameValuePairs);
HttpGet get = new HttpGet(uriBuilder.build());
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
//取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String objectStr = EntityUtils.toString(entity, "utf-8");
JSONObject jsStr = JSONObject.parseObject(objectStr);
//关闭httpclient
response.close();
httpClient.close();
return jsStr;
}
/**
* 获取 access_token 信息
*
* @return
* @throws Exception
*/
public static String doGetAccessToken() throws Exception {
//创建一个httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个uri对象
URIBuilder uriBuilder = new URIBuilder(contactsTokenURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("corpid", corpid));
nameValuePairs.add(new BasicNameValuePair("corpsecret", corpsecret));
uriBuilder.addParameters(nameValuePairs);
HttpGet get = new HttpGet(uriBuilder.build());
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
//取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String jsonStr = EntityUtils.toString(entity, "utf-8");
JSONObject jsStr = JSONObject.parseObject(jsonStr);
String access_token = jsStr.getString("access_token");
//关闭httpclient
response.close();
httpClient.close();
return access_token;
}
}
欢迎补充~~~~