com.foxinmy.weixin4j.mp.api.UserApi Maven / Gradle / Ivy
package com.foxinmy.weixin4j.mp.api;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.model.Following;
import com.foxinmy.weixin4j.mp.model.User;
import com.foxinmy.weixin4j.mp.type.Lang;
import com.foxinmy.weixin4j.token.TokenHolder;
/**
* 用户相关API
*
* @className UserApi
* @author jy.hu
* @date 2014年9月25日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.mp.model.User
*/
public class UserApi extends MpApi {
private final TokenHolder tokenHolder;
public UserApi(TokenHolder tokenHolder) {
this.tokenHolder = tokenHolder;
}
/**
* 获取用户信息
*
* @param openId
* 用户对应的ID
* @return 用户对象
* @throws WeixinException
* @see 获取用户信息
* @see com.foxinmy.weixin4j.mp.model.User
* @see {@link #getUser(String,Lang)}
*/
public User getUser(String openId) throws WeixinException {
return getUser(openId, Lang.zh_CN);
}
/**
* 获取用户信息
*
* 在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的,对于不同公众号,
* 同一用户的openid不同),公众号可通过本接口来根据OpenID获取用户基本信息,包括昵称、头像、性别、所在城市、语言和关注时间
*
*
* @param openId
* 用户对应的ID
* @param lang
* 国家地区语言版本
* @return 用户对象
* @throws WeixinException
* @see 获取用户信息
* @see com.foxinmy.weixin4j.mp.type.Lang
* @see com.foxinmy.weixin4j.mp.model.User
*/
public User getUser(String openId, Lang lang) throws WeixinException {
String user_info_uri = getRequestUri("api_user_info_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinClient.get(String.format(user_info_uri,
token.getAccessToken(), openId, lang.name()));
return response.getAsObject(new TypeReference() {
});
}
/**
* 获取用户一定数量(10000)的关注者列表
*
* @param nextOpenId
* 下一次拉取数据的openid
* @return 关注信息
* @throws WeixinException
* @see 获取关注者列表
* @see com.foxinmy.weixin4j.mp.model.Following
*/
public Following getFollowing(String nextOpenId) throws WeixinException {
String following_uri = getRequestUri("following_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinClient.get(String.format(following_uri,
token.getAccessToken(), nextOpenId == null ? "" : nextOpenId));
Following following = response
.getAsObject(new TypeReference() {
});
if (following.getCount() > 0) {
List openIds = JSON.parseArray(following.getDataJson()
.getString("openid"), String.class);
List userList = new ArrayList();
for (String openId : openIds) {
userList.add(getUser(openId));
}
following.setUserList(userList);
}
return following;
}
/**
* 获取用户全部的关注者列表
*
* 当公众号关注者数量超过10000时,可通过填写next_openid的值,从而多次拉取列表的方式来满足需求,
* 将上一次调用得到的返回中的next_openid值,作为下一次调用中的next_openid值
*
*
* @return 用户对象集合
* @throws WeixinException
* @see 获取关注者列表
* @see com.foxinmy.weixin4j.mp.model.Following
* @see com.foxinmy.weixin4j.mp.api.UserApi#getFollowing(String)
*/
public List getAllFollowing() throws WeixinException {
List userList = new ArrayList();
String nextOpenId = null;
Following f = null;
for (;;) {
f = getFollowing(nextOpenId);
if (f.getCount() == 0) {
break;
}
userList.addAll(f.getUserList());
nextOpenId = f.getNextOpenId();
}
return userList;
}
/**
* 设置用户备注名
*
* @param openId
* 用户ID
* @param remark
* 备注名
* @throws WeixinException
* @see 设置用户备注名
*/
public JsonResult remarkUserName(String openId, String remark)
throws WeixinException {
String updateremark_uri = getRequestUri("updateremark_uri");
Token token = tokenHolder.getToken();
JSONObject obj = new JSONObject();
obj.put("openid", openId);
obj.put("remark", remark);
WeixinResponse response = weixinClient.post(
String.format(updateremark_uri, token.getAccessToken()),
obj.toJSONString());
return response.getAsJsonResult();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy