读取企微通讯录成员

官方文档地址读取成员 - 接口文档 - 企业微信开发者中心

废话不多说,直接上代码。

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格式是否正确.");
		}
	}

自此完结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值