public class WechatUtil {
public static final String CHAR_SET = "utf-8";
/**
* 获取token
*/
public static final String TOKEN_API = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
/**
* 创建用户
*/
public static final String CREATE_USER_API = "https://qyapi.weixin.qq.com/cgi-bin/user/create";
/**
* 修改用户
*/
public static final String UPDATE_USER_API = "https://qyapi.weixin.qq.com/cgi-bin/user/update";
/**
* 删除用户
*/
public static final String DELETE_USER_API = "https://qyapi.weixin.qq.com/cgi-bin/user/delete";
/**
* 创建部门
*/
public static final String CREATE_ORG_API = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
/**
* 修改部门
*/
public static final String UPDATE_ORG_API = "https://qyapi.weixin.qq.com/cgi-bin/department/update";
/**
* 删除部门
*/
public static final String DELETE_ORG_API = "https://qyapi.weixin.qq.com/cgi-bin/department/delete";
/**
* 获取部门下的用户列表
*/
public static final String FIND_ORG_USER_API = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
/**
* 用户批量删除
*/
public static final String USER_BATCH_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete";
private CloseableHttpClient httpClient;
private HttpPost httpPost;
private HttpGet httpGet;
private static final Logger log = LoggerFactory.getLogger(WechatUtil.class);
/**
* 获取access_token
*
* @param corpid 企业ID
* @param corpsecret 应用的凭证密钥
*/
public String getToken(String corpid, String corpsecret) {
httpClient = HttpClients.createDefault();
httpGet = new HttpGet(TOKEN_API + "?corpid=" + corpid + "&corpsecret=" + corpsecret);
CloseableHttpResponse res = null;
try {
res = httpClient.execute(httpGet);
} catch (IOException e) {
throw new RuntimeException(e);
}
String resString = "";
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
JSONObject jo = JSONObject.parseObject(resString);
return jo.getString("access_token");
} catch (Exception e) {
log.error(e.getMessage());
} finally {
try {
res.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
log.debug("resp:{}", resString);
return resString;
}
/**
* 用户创建
*
* @param data 数据
* @param token token
* @return String
* @throws IOException IOException
*/
public String createUser(String data, String token) throws IOException {
CloseableHttpResponse res = getCloseableHttpResponse(CREATE_USER_API, data, token);
String resString;
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, CHAR_SET);
EntityUtils.consume(entity);
} finally {
res.close();
}
log.debug("call [{}], param:{}, res:{}", CREATE_USER_API, data, resString);
return resString;
}
/**
* 用户修改
*
* @param data 数据
* @param token token
* @return resString
* @throws IOException IOException
*/
public String updateUser(String data, String token) throws IOException {
CloseableHttpResponse res = getCloseableHttpResponse(UPDATE_USER_API, data, token);
String resString;
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, CHAR_SET);
EntityUtils.consume(entity);
} finally {
res.close();
}
log.debug("call [{}], param:{}, res:{}", UPDATE_USER_API, data, resString);
return resString;
}
/**
* 用户删除
*
* @param userId 用户id
* @param token token
* @return token
* @throws IOException IOException
*/
public String deleteUser(String userId, String token) throws IOException {
return deleteOrgOrUserById(userId, token, DELETE_USER_API,"userid=");
}
/**
* 创建部门
*
* @param data 组织机构data
* @param token token
* @return String
* @throws IOException IOException
*/
public String creteOrg(String data, String token) throws IOException {
CloseableHttpResponse res = getCloseableHttpResponse(CREATE_ORG_API, data, token);
String resString;
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, CHAR_SET);
EntityUtils.consume(entity);
} finally {
res.close();
}
log.debug("call [{}], param:{}, res:{}", CREATE_ORG_API, data, resString);
return resString;
}
/**
* 修改部门
*
* @param data 组织机构data
* @param token token
* @return String
* @throws IOException IOException
*/
public String updateOrg(String data, String token) throws IOException {
CloseableHttpResponse res = getCloseableHttpResponse(UPDATE_ORG_API, data, token);
String resString;
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, CHAR_SET);
EntityUtils.consume(entity);
} finally {
res.close();
}
log.debug("call [{}], param:{}, res:{}", UPDATE_ORG_API, data, resString);
return resString;
}
/**
* 部门删除
*
* @param orgId 用户id
* @param token token
* @return token
* @throws IOException IOException
*/
public String deleteOrg(String orgId, String token) throws IOException {
return deleteOrgOrUserById(orgId, token, DELETE_ORG_API,"id=");
}
/**
* 通过id删除逻辑(用户和部门)
*
* @param id id
* @param token token
* @param deleteOrgApi deleteOrgApi
* @return String
*/
private String deleteOrgOrUserById(String id, String token, String deleteOrgApi,String type){
httpClient = HttpClients.createDefault();
httpGet = new HttpGet(deleteOrgApi + "?access_token=" + token + "&" + type + id);
CloseableHttpResponse res = null;
try {
res = httpClient.execute(httpGet);
} catch (IOException e) {
throw new RuntimeException(e);
}
String resString = "";
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, "utf-8");
return resString;
} catch (Exception e) {
log.error(e.getMessage());
}
log.debug("resp:{}", resString);
return resString;
}
/**
* 通过部门Id获取用户列表
*
* @param orgId 组织机构id
* @param token token
* @return String
*/
public String getUserListByOrgId(String orgId, String token){
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(FIND_ORG_USER_API + "?access_token=" + token + "&" + "department_id=" + orgId);
try {
httpPost.setEntity(new StringEntity(CHAR_SET));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
CloseableHttpResponse res = null;
try {
res = httpClient.execute(httpPost);
} catch (IOException e) {
throw new RuntimeException(e);
}
String resString;
try {
HttpEntity entity = res.getEntity();
try {
resString = EntityUtils.toString(entity, CHAR_SET);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
EntityUtils.consume(entity);
} catch (IOException e) {
throw new RuntimeException(e);
}
} finally {
try {
res.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
log.debug("call [{}], param:{}, res:{}", FIND_ORG_USER_API, orgId, resString);
return resString;
}
/**
* 用户批量删除
* @param data 数据
* @param token token
* @return String
* @throws IOException IOException
*/
public String batchDelete(String data, String token) throws IOException {
CloseableHttpResponse res = getCloseableHttpResponse(USER_BATCH_DELETE, data, token);
String resString;
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, CHAR_SET);
EntityUtils.consume(entity);
} finally {
res.close();
}
log.debug("call [{}], param:{}, res:{}", USER_BATCH_DELETE, data, resString);
return resString;
}
/**
* 创建修改用户data
*
* @param name 姓名
* @param email 邮箱
* @param mobile 手机
* @param department 部门
* @return json
*/
public String createOrUpdateUserData(String name, String email, String mobile, String department, String userId) {
Map<String, Object> data = new HashMap<String, Object>();
if(!StringUtils.isEmpty(name)){
data.put(WeComConstant.ID_SOURCE_WE_COM_USER_NAME, name);
}
if(!StringUtils.isEmpty(email)){
data.put(WeComConstant.ID_SOURCE_WE_COM_USER_MOBILE, mobile);
}
if(!StringUtils.isEmpty(mobile)){
data.put(WeComConstant.ID_SOURCE_WE_COM_USER_DEPARTMENT, department);
}
if(!StringUtils.isEmpty(department)){
data.put(WeComConstant.ID_SOURCE_WE_COM_USER_EMAIL, email);
}
if(!StringUtils.isEmpty(userId)){
data.put(WeComConstant.ID_SOURCE_WE_COM_USER_USERID, userId);
}
return JSONObject.toJSONString(data);
}
/**
* 批量用户删除data
* @param userId 用户id
* @return String
*/
public String createDataByUserId(List<String> userId) {
Map<String, Object> data = new HashMap<String, Object>();
data.put("useridlist", userId);
return JSONObject.toJSONString(data);
}
/**
* 创建或者修改部门data
*
* @param name 名称
* @param parentId 父级id
* @return String
*/
public String createOrUpdateOrgData(String name, String parentId, String orgId) {
Map<String, Object> data = new HashMap<String, Object>();
data.put(WeComConstant.ID_SOURCE_WE_COM_ORG_NAME, name);
data.put(WeComConstant.ID_SOURCE_WE_COM_ORG_PARENT_ID, parentId);
data.put(WeComConstant.ORDER, "1");
if (!StringUtils.isEmpty(orgId)) {
data.put(WeComConstant.ID_SOURCE_WE_COM_ID, orgId);
}
return JSONObject.toJSONString(data);
}
/**
* 公共逻辑
*
* @param url url
* @param data 数据
* @param token token
* @return CloseableHttpResponse
* @throws IOException CloseableHttpResponse
*/
private CloseableHttpResponse getCloseableHttpResponse(String url, String data, String token) throws IOException {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url + "?access_token=" + token);
httpPost.setEntity(new StringEntity(data, CHAR_SET));
return httpClient.execute(httpPost);
}
/**
* 示例
*/
public void test() {
String token = getToken("xxxxxxx", "xxxxxxxxxxxxxx");
String data = createOrUpdateUserData("测试用户新增", "www.xxxxxxxxx.com", "xxxxxxx", "xxxxx", "xxxxxx");
try {
String s = createUser(data, token);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
【企业微信API】(数据源同步)
于 2023-06-08 14:00:45 首次发布