/**
* 获取部门》用户信息
* 该接口已失效,企业微信于2022.8月份对接口进行了调整,原使用该接口仍可以使用;新的IP调用该接口将被拒绝。
*
* @return
*/
public void getAllwxUser() {
log.info("begin-----执行---获取员工信息-----getUser");
List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();
HashMap<String, Object> user = null;
// 1、查询出access_Token的值
String access_token = getAccessToken(CORPID, APP_SECRET);
log.info("access_token=" + access_token);
log.info("=====2、获取部门集合数据 start=====");
// 2、获取部门列表信息(不填则是查询出所有的部门列表)
List<String> depts = getDepartmentList(access_token, "");
log.info("=====2、获取部门集合数据 end 数量:" + depts.size() + "=====");
// 3、根据部门信息去获取成员的详细信息(查询到的数据)
log.info("=====3、获取用户集合数据 start=====");
List<SystemUserFu> users = getDepartmentUserDetails(depts, access_token);
log.info("=====3、获取用户集合数据 end 数量:" + users.size() + "=====");
//4、对查询到的信息数据进行拼接插入
log.info("=====4、insertUserDetailsNew start=====");
int insertCount = insertUserDetailsNew(users);
log.info("=====4、insertUserDetailsNew end 数量:" + insertCount + "=====");
}
2、获取token
/**
* 获取扫码token
*
* @param corpId
* @param corpSecret
* @return
*/
public String getAccessToken(String corpId, String corpSecret) {
String url = String.format(ACCESSTOKE_URL, corpId, corpSecret);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
URI uri = builder.build().encode().toUri();
String resp = restTemplate.getForObject(uri, String.class);
JSONObject jsonObject = JSONObject.parseObject(resp);
String access_token = jsonObject.getString("access_token");
return access_token;
}
3、获取部门列表
/**
* 获取部门列表
*
* @param accessToken
* @param departmentId
* @return
*/
public List<String> getDepartmentList(String accessToken, String departmentId) {
List<String> departments = new ArrayList<String>();
String url = String.format(departmentList_url, accessToken, departmentId);
String resp = restTemplate.getForObject(url, String.class);
JSONObject jsonObject = JSONObject.parseObject(resp);
// log.info("jsonObject:" + jsonObject.toString());
// 3.错误消息处理
if (null != jsonObject) {
if (0 != jsonObject.getIntValue("errcode")) {
log.info("获取部门列表 errcode:{" + jsonObject.getIntValue("errcode") + "} errmsg:{" + jsonObject.get("errmsg") + "}");
} else {// 查询成功
List<Map<String, Object>> mapListJson = (List) jsonObject.getJSONArray("department");
if (null != mapListJson) {
for (int i = 0; i < mapListJson.size(); i++) {
Map<String, Object> dept = mapListJson.get(i);
//部门id不为空,并且是最上级企业,避免重复获取数据
if (null != dept.get("id") && "0".equals(dept.get("parentid").toString())) {
departments.add(dept.get("id").toString());
}
}
}
}
}
return departments;
}
4、获取用户信息
/**
* 获取部门成员详情
*
* @param depts
* @param accessToken //是否遍历子部门的成员,一般不要遍历,除非你就只获取父级部门或者子部门为空,不然会导致数据重复
* @return
*/
public List<SystemUserFu> getDepartmentUserDetails(List<String> depts, String accessToken) {
List<SystemUserFu> users = new ArrayList<SystemUserFu>();
for (String deptId : depts) {
log.info("=====deptId:" + deptId);
// 1.获取请求的url
String url = String.format(departmentUserList_url, accessToken, deptId, 1);
String resp = restTemplate.getForObject(url, String.class);
JSONObject jsonObject = JSONObject.parseObject(resp);
log.info("=====jsonObject:" + jsonObject.toString().substring(0, 1000));
// log.info("Contact_service----jsonObject:" + jsonObject.toString());
// 3.错误消息处理
if (null != jsonObject) {
if (0 != jsonObject.getIntValue("errcode")) {
log.info("获取部门成员详情失败 errcode:{" + jsonObject.getIntValue("errcode") + "} errmsg:{" + jsonObject.get("errmsg") + "}");
} else {// 查询成功的话
log.info("=====jsonObject不为空=====");
// JSONArray array=jsonObject.getJSONArray("userlist");
List<Map<String, Object>> mapListJson = (List) jsonObject.getJSONArray("userlist");
log.info("=====mapListJson:长度:" + mapListJson.size() + "=====");
if (null != mapListJson) {
log.info("=====mapListJson不为空=====");
for (int i = 0; i < mapListJson.size(); i++) {
Map<String, Object> de = mapListJson.get(i);
// if (i < 2) {
// log.info("=====mapListJson " + i + " start=====");
// for (String key : de.keySet()) {
// log.info("key:" + key + ";val:" + de.get(key) + ";");
// }
// log.info("=====mapListJson " + i + " end=====");
//
// }
// 具体字段看自己业务需求
SystemUserFu user = new SystemUserFu();
user.setUserid(String.valueOf(de.get("userid")));
user.setDepartment(String.valueOf(de.get("department")));
user.setName(String.valueOf(de.get("name")));
user.setPosition(String.valueOf(de.get("position")));
user.setMobile(String.valueOf(de.get("mobile")));
user.setGender(String.valueOf(de.get("gender")));
user.setEmail(String.valueOf(de.get("email")));
user.setStatus(String.valueOf(de.get("status")));
// user.setOpenuserid(String.valueOf(de.get("open_userid")));
//某些企业企业微信存在自建字段,可使用该方法获取自建字段
List<Map<String, Object>> attrsJson = (List) JSONObject.parseObject(de.get("extattr").toString()).get("attrs");
for (Map<String, Object> userDetailMap : attrsJson) {
String name = userDetailMap.get("name").toString();
if ("XXX工号".equals(name)) {
String gy_code = userDetailMap.get("value").toString();
// log.info("XXX工号:"+gy_code);
// 具体字段看自己业务需求
user.setUser_code(gy_code);
}
}
users.add(user);
}
}
}
}
}
return users;
}
4,拼接批量插入语句(批量插入语句调用数据库连接少,效率高,此处使用的是mysql数据库,别的数据库根据语法自行拼接)
public int insertUserDetailsNew(List<SystemUserFu> systemUserFuList) {
StringBuffer insertSql = new StringBuffer();
insertSql.append("insert into system_user_fu (name,department,userid,mobile,email,status,user_code,openuserid ) ");
insertSql.append(" values ");
for (SystemUserFu suf : systemUserFuList) {
if (null != suf) {
String name = suf.getName();
String department = suf.getDepartment();
String mobile = suf.getMobile();
String email = suf.getEmail();
String status = suf.getStatus();
String user_code = suf.getUser_code();
String userid = suf.getUserid();
insertSql.append("('").append(name).append("',");
insertSql.append("'").append(department).append("',");
insertSql.append("'").append(userid).append("',");
insertSql.append("'").append(mobile).append("',");
insertSql.append("'").append(email).append("',");
insertSql.append("'").append(status).append("',");
insertSql.append("'").append(user_code).append("',");
insertSql.append("'").append("'),");
}
}
//循环结束,删除最后面的一个“,”号
insertSql.deleteCharAt(insertSql.length() - 1);
int insertCount = layoutService.getSqlMapper().insert(insertSql.toString());
return insertCount;
}
导入的公共包
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.io.IOException;
import java.net.URI;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
其他平台自建包,此处不放出代码,根据自己项目情况使用自己的包,放出来仅给开发者参考是我项目的包非开源公共包
import com.xx.config.DynamicDataSource;
import com.xx.core.BaseService;
import com.xx.core.vo.Result;
import com.xx.modules.api.web.WeChatMsgSend;
import com.xx.modules.common.service.LayoutService;
import com.xx.modules.eam.entity.SystemUserFu;
import com.xx.modules.eam.mapper.SystemUserFuMapper;
import com.xx.modules.system.entity.SystemUser;
import com.xx.modules.system.mapper.SystemUserMapper;
相关类解释:SystemUserFu :自定义实体类,根据自己的实体类属性设置参数
layoutService:数据库操作类,根据自己的项目情况,调用dao层操作类
String url = String.format(ACCESSTOKE_URL, corpId, corpSecret);此处的参数为企业微信的管理员界面进行查询获得