All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tmsps.ne4weixin.api.UserAPI Maven / Gradle / Ivy

There is a newer version: 3.2.2.3
Show newest version
package com.tmsps.ne4weixin.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tmsps.ne4weixin.bean.BaseResultBean;
import com.tmsps.ne4weixin.bean.UserInfo;
import com.tmsps.ne4weixin.config.WxConfig;
import com.tmsps.ne4weixin.utils.WXHttpUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @Title: UserAPI.java
 * @Package: com.tmsps.ne4weixin.api
 * @Description: 公众号用户管理API
 */
@Slf4j
public class UserAPI extends BaseAPI {
	public UserAPI(WxConfig wxConfig) {
		super(wxConfig);
	}

	/**
	 * 获取用户基本信息
	 * 
	 * @param openId
	 * @return
	 */
	public UserInfo getUserInfo(String openId) {
		String url = String.format(USER_INFO, wxConfig.getAccessToken(), openId, "zh_CN");
		String result = WXHttpUtil.get(url);
		return JSONObject.parseObject(result, UserInfo.class);
	}

	/**
	 * 批量获取用户基本信息
	 * 
	 * @param openids
	 * @return
	 */
	public List getUserInfos(List openids) {
		String url = String.format(GET_USER_INFOS, wxConfig.getAccessToken());
		JSONObject param = new JSONObject();
		List> params = new ArrayList>();
		for (String openid : openids) {
			Map map = new HashMap();
			map.put("openid", openid);
			map.put("lang", "zh_CN");
			params.add(map);
		}
		param.put("user_list", params);
		String result = WXHttpUtil.postJson(url, param.toJSONString());
		log.info("批量获取用户基本信息返回信息:{}", result);
		JSONObject obj = JSONObject.parseObject(result);
		return JSON.parseArray(obj.getString("user_info_list"), UserInfo.class);
	}

	/**
	 * 获取用户列表 从头开始拉取
	 * 
	 * @return
	 */
	public JSONObject getUserList() {
		return getUserList("");
	}

	/**
	 * 获取用户列表
	 * 
	 * @param next_openid 第一个拉取的OPENID
	 * @return
	 */
	public JSONObject getUserList(String next_openid) {
		String url = String.format(GET_USERS, wxConfig.getAccessToken(), next_openid);
		String result = WXHttpUtil.get(url);
		return JSONObject.parseObject(result);
	}

	/**
	 * 拉黑用户
	 * 
	 * @param openids 拉入黑名单的用户的openid,一次拉黑最多允许20个
	 * @return
	 */
	public BaseResultBean addUserToBlack(List openids) {
		if (openids != null && openids.size() > 20) {
			throw new IllegalArgumentException("一次拉黑最多允许20个用户");
		}
		String url = String.format(ADD_BLACK_USERS, wxConfig.getAccessToken());
		Map paramMap = new HashMap();
		paramMap.put("openid_list", openids);
		String result = WXHttpUtil.postJson(url, JSONObject.toJSONString(paramMap));
		log.info("拉黑用户返回信息:{}", result);
		return JSONObject.parseObject(result, BaseResultBean.class);
	}

	/**
	 * 获取公众号的黑名单列表 从头开始拉取
	 * 
	 * @return
	 */
	public String getUserBlackList() {
		return this.getUserBlackList("");
	}

	/**
	 * 获取公众号的黑名单列表
	 * 
	 * @param begin_openid 开始openid
	 * @return
	 */
	public String getUserBlackList(String begin_openid) {
		String url = String.format(GET_BLACK_USERS, wxConfig.getAccessToken());
		Map paramMap = new HashMap();
		paramMap.put("begin_openid", begin_openid);
		return WXHttpUtil.postJson(url, JSONObject.toJSONString(paramMap));
	}

	/**
	 * 取消拉黑用户
	 * @param openids 拉入黑名单的用户的openid,一次拉黑最多允许20个
	 */
	public BaseResultBean removeUserFromBlack(List openids) {
		if (openids != null && openids.size() > 20) {
			throw new IllegalArgumentException("每次最多允许操作20个用户");
		}
		String url = String.format(CANCLE_BLACK_USERS, wxConfig.getAccessToken());
		Map paramMap = new HashMap();
		paramMap.put("openid_list", openids);
		String result = WXHttpUtil.postJson(url, JSONObject.toJSONString(paramMap));
		log.info("取消拉黑用户返回信息:{}", result);
		return JSONObject.parseObject(result, BaseResultBean.class);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy