官方文档地址读取成员 - 接口文档 - 企业微信开发者中心
废话不多说,直接上代码。
getToken方法可以看我上一篇博文获取企微通讯录(应用)的access_token_wzn1054162229的博客-CSDN博客
getHttp方法HttpsUtil的getHttp方法_wzn1054162229的博客-CSDN博客
/**
* Description: 查询用户
*
* @interfaceName https://open.work.weixin.qq.com/api/doc/90000/90135/90196
* @param userMap
* @return
* @throws Exception
*/
public static Map<String, Object> getUser(Map<String, Object> userMap) throws Exception {
try {
// 校验必填
String userid = MapUtil.checkStringExistMap(userMap, "userid");
String accessToken = getToken("");
String result = HttpsUtil.getHttp(get_user_url, "access_token=" + accessToken + "&userid=" + userid);
Map<String, Object> resultMap = orderResult(result);
return resultMap;
} catch (Exception e) {
throw new Exception(e);
}
}
public static String checkStringExistMap(Map<String, Object> dataMap, String key) {
if (!dataMap.containsKey(key) || StringUtils.isEmpty(ObjectParser.toString(dataMap.get(key)))) {
throw new Exception(-1, "参数[" + key + "]不能为空或空字符串.");
}
return ObjectParser.toString(dataMap.get(key));
}
public static String toString(Object data)
{
if (data == null)
{
return null;
}
return String.valueOf(data.toString());
}
public static Map<String, Object> orderResult(String result) throws Exception {
Map<String, Object> resultMap = CheckJSONDataUtil.checkJSONData(result);
// 解析企业微信返回
if (resultMap == null || StringUtils.isEmpty(resultMap.get("errcode"))) {
throw new Exception(-1, "无法获得企业微信响应结果");
}
if (ObjectParser.toInteger(resultMap.get("errcode")) != 0) {
resultMap.put("code", resultMap.get("errcode"));
resultMap.put("msg", resultMap.get("errmsg"));
return resultMap;
}
// 封装data数据
Map<String, Object> returnMap = new HashMap<>();
// 将code转换
returnMap.put("code", resultMap.get("errcode"));
resultMap.remove("errcode");
resultMap.remove("errmsg");
returnMap.put("data", resultMap);
return returnMap;
}
public static Map<String, Object> checkJSONData(String data) {
if (data == null || "".equals(data.trim())) {
throw new Exception(-1, "传入的参数data不能为空.");
}
if ("null".equals(data.trim())) {
throw new Exception(-1, "null字符串不是JSON格式.");
}
try {
Map<String, Object> dataMap = JSONObject.parseObject(data.trim(), Feature.OrderedField, Feature.IgnoreAutoType);
return dataMap;
} catch (Exception e) {
ILogUtil.error("JSONObject.parseObject方法报错:" + e.getMessage());
ILogUtil.error("待转换成JSON的字符串,data:" + data);
throw new Exception(-1, "解析参数出错,请检查传入参数JSON格式是否正确.");
}
}
自此完结