fixed #42
This commit is contained in:
parent
02436ff2df
commit
7a870343b9
File diff suppressed because it is too large
Load Diff
@ -58,16 +58,17 @@ public class UserApi extends MpApi {
|
|||||||
* 国家地区语言版本
|
* 国家地区语言版本
|
||||||
* @return 用户对象
|
* @return 用户对象
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">获取用户信息</a>
|
* "http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">
|
||||||
|
* 获取用户信息</a>
|
||||||
* @see com.foxinmy.weixin4j.mp.type.Lang
|
* @see com.foxinmy.weixin4j.mp.type.Lang
|
||||||
* @see com.foxinmy.weixin4j.mp.model.User
|
* @see com.foxinmy.weixin4j.mp.model.User
|
||||||
*/
|
*/
|
||||||
public User getUser(String openId, Lang lang) throws WeixinException {
|
public User getUser(String openId, Lang lang) throws WeixinException {
|
||||||
String user_info_uri = getRequestUri("api_user_info_uri");
|
String user_info_uri = getRequestUri("api_user_info_uri");
|
||||||
Token token = tokenHolder.getToken();
|
Token token = tokenHolder.getToken();
|
||||||
WeixinResponse response = weixinExecutor.get(String.format(
|
WeixinResponse response = weixinExecutor
|
||||||
user_info_uri, token.getAccessToken(), openId, lang.name()));
|
.get(String.format(user_info_uri, token.getAccessToken(), openId, lang.name()));
|
||||||
|
|
||||||
return response.getAsObject(new TypeReference<User>() {
|
return response.getAsObject(new TypeReference<User>() {
|
||||||
});
|
});
|
||||||
@ -79,8 +80,9 @@ public class UserApi extends MpApi {
|
|||||||
* @param openIds
|
* @param openIds
|
||||||
* 用户ID
|
* 用户ID
|
||||||
* @return 用户列表
|
* @return 用户列表
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">获取用户信息</a>
|
* "http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">
|
||||||
|
* 获取用户信息</a>
|
||||||
* @see com.foxinmy.weixin4j.mp.model.User
|
* @see com.foxinmy.weixin4j.mp.model.User
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see {@link #getUsers(Lang,String[])}
|
* @see {@link #getUsers(Lang,String[])}
|
||||||
@ -95,71 +97,89 @@ public class UserApi extends MpApi {
|
|||||||
* @param lang
|
* @param lang
|
||||||
* 国家地区语言版本
|
* 国家地区语言版本
|
||||||
* @param openIds
|
* @param openIds
|
||||||
* 用户ID
|
* 用户ID 最多100个
|
||||||
* @return 用户列表
|
* @return 用户列表
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">获取用户信息</a>
|
* "http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">
|
||||||
|
* 获取用户信息</a>
|
||||||
* @see com.foxinmy.weixin4j.mp.type.Lang
|
* @see com.foxinmy.weixin4j.mp.type.Lang
|
||||||
* @see com.foxinmy.weixin4j.mp.model.User
|
* @see com.foxinmy.weixin4j.mp.model.User
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public List<User> getUsers(Lang lang, String... openIds)
|
public List<User> getUsers(Lang lang, String... openIds) throws WeixinException {
|
||||||
throws WeixinException {
|
|
||||||
String api_users_info_uri = getRequestUri("api_users_info_uri");
|
String api_users_info_uri = getRequestUri("api_users_info_uri");
|
||||||
StringBuilder parameter = new StringBuilder();
|
StringBuilder parameter = new StringBuilder();
|
||||||
parameter.append("{\"user_list\": [");
|
parameter.append("{\"user_list\": [");
|
||||||
for (String openId : openIds) {
|
for (String openId : openIds) {
|
||||||
parameter.append("{\"openid\": \"").append(openId).append("\"");
|
parameter.append("{\"openid\": \"").append(openId).append("\"");
|
||||||
parameter.append(",\"lang\": \"").append(lang.name()).append("\"")
|
parameter.append(",\"lang\": \"").append(lang.name()).append("\"").append("},");
|
||||||
.append("},");
|
|
||||||
}
|
}
|
||||||
parameter.delete(parameter.length() - 1, parameter.length());
|
parameter.delete(parameter.length() - 1, parameter.length());
|
||||||
parameter.append("]}");
|
parameter.append("]}");
|
||||||
Token token = tokenHolder.getToken();
|
Token token = tokenHolder.getToken();
|
||||||
WeixinResponse response = weixinExecutor.post(
|
WeixinResponse response = weixinExecutor.post(String.format(api_users_info_uri, token.getAccessToken()),
|
||||||
String.format(api_users_info_uri, token.getAccessToken()),
|
|
||||||
parameter.toString());
|
parameter.toString());
|
||||||
|
|
||||||
return JSON.parseArray(
|
return JSON.parseArray(response.getAsJson().getString("user_info_list"), User.class);
|
||||||
response.getAsJson().getString("user_info_list"), User.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户一定数量(10000)的关注者列表
|
* 获取公众号一定数量(10000)的关注者列表 <font corlor="red">请慎重使用</font>
|
||||||
*
|
*
|
||||||
* @param nextOpenId
|
* @param nextOpenId
|
||||||
* 下一次拉取数据的openid
|
* 下一次拉取数据的openid 不填写则默认从头开始拉取
|
||||||
* @return 关注信息
|
* @return 关注者信息 <font color="red">包含用户的详细信息</font>
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://mp.weixin.qq.com/wiki/3/17e6919a39c1c53555185907acf70093.html">获取关注者列表</a>
|
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN">
|
||||||
|
* 获取关注者列表</a>
|
||||||
|
* @see <a href=
|
||||||
|
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140840&token=&lang=zh_CN">
|
||||||
|
* 批量获取用户信息</a>
|
||||||
* @see com.foxinmy.weixin4j.mp.model.Following
|
* @see com.foxinmy.weixin4j.mp.model.Following
|
||||||
|
* @see com.foxinmy.weixin4j.mp.model.User
|
||||||
*/
|
*/
|
||||||
public Following getFollowing(String nextOpenId) throws WeixinException {
|
public Following getFollowing(String nextOpenId) throws WeixinException {
|
||||||
String following_uri = getRequestUri("following_uri");
|
Following following = getFollowingOpenIds(nextOpenId);
|
||||||
Token token = tokenHolder.getToken();
|
|
||||||
WeixinResponse response = weixinExecutor.get(String.format(
|
|
||||||
following_uri, token.getAccessToken(), nextOpenId == null ? ""
|
|
||||||
: nextOpenId));
|
|
||||||
|
|
||||||
Following following = response
|
|
||||||
.getAsObject(new TypeReference<Following>() {
|
|
||||||
});
|
|
||||||
|
|
||||||
if (following.getCount() > 0) {
|
if (following.getCount() > 0) {
|
||||||
List<String> openIds = JSON.parseArray(following.getDataJson()
|
List<User> users = new ArrayList<User>(following.getCount());
|
||||||
.getString("openid"), String.class);
|
for (int i = 1; i <= (int) Math.ceil(following.getCount() / 100d); i++) {
|
||||||
List<User> userList = new ArrayList<User>();
|
users.addAll(getUsers(following.getOpenIds().subList((i - 1) * 100, i * 100).toArray(new String[] {})));
|
||||||
for (String openId : openIds) {
|
|
||||||
userList.add(getUser(openId));
|
|
||||||
}
|
}
|
||||||
following.setUserList(userList);
|
following.setUserList(users);
|
||||||
}
|
}
|
||||||
return following;
|
return following;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户全部的关注者列表
|
* 获取公众号一定数量(10000)的关注者列表
|
||||||
|
*
|
||||||
|
* @param nextOpenId
|
||||||
|
* 下一次拉取数据的openid 不填写则默认从头开始拉取
|
||||||
|
* @return 关注者信息 <font color="red">不包含用户的详细信息</font>
|
||||||
|
* @throws WeixinException
|
||||||
|
* @see <a href=
|
||||||
|
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN">
|
||||||
|
* 获取关注者列表</a>
|
||||||
|
* @see com.foxinmy.weixin4j.mp.model.Following
|
||||||
|
*/
|
||||||
|
public Following getFollowingOpenIds(String nextOpenId) throws WeixinException {
|
||||||
|
String following_uri = getRequestUri("following_uri");
|
||||||
|
Token token = tokenHolder.getToken();
|
||||||
|
WeixinResponse response = weixinExecutor
|
||||||
|
.get(String.format(following_uri, token.getAccessToken(), nextOpenId == null ? "" : nextOpenId));
|
||||||
|
|
||||||
|
JSONObject result = response.getAsJson();
|
||||||
|
Following following = JSON.toJavaObject(result, Following.class);
|
||||||
|
|
||||||
|
if (following.getCount() > 0) {
|
||||||
|
following.setOpenIds(JSON.parseArray(result.getJSONObject("data").getString("openid"), String.class));
|
||||||
|
}
|
||||||
|
return following;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取公众号全部的关注者列表 <font corlor="red">请慎重使用</font>
|
||||||
* <p>
|
* <p>
|
||||||
* 当公众号关注者数量超过10000时,可通过填写next_openid的值,从而多次拉取列表的方式来满足需求,
|
* 当公众号关注者数量超过10000时,可通过填写next_openid的值,从而多次拉取列表的方式来满足需求,
|
||||||
* 将上一次调用得到的返回中的next_openid值,作为下一次调用中的next_openid值
|
* 将上一次调用得到的返回中的next_openid值,作为下一次调用中的next_openid值
|
||||||
@ -167,9 +187,13 @@ public class UserApi extends MpApi {
|
|||||||
*
|
*
|
||||||
* @return 用户对象集合
|
* @return 用户对象集合
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://mp.weixin.qq.com/wiki/3/17e6919a39c1c53555185907acf70093.html">获取关注者列表</a>
|
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN">
|
||||||
* @see com.foxinmy.weixin4j.mp.model.Following
|
* 获取关注者列表</a>
|
||||||
|
* @see <a href=
|
||||||
|
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140840&token=&lang=zh_CN">
|
||||||
|
* 批量获取用户信息</a>
|
||||||
|
* @see com.foxinmy.weixin4j.mp.model.User
|
||||||
* @see com.foxinmy.weixin4j.mp.api.UserApi#getFollowing(String)
|
* @see com.foxinmy.weixin4j.mp.api.UserApi#getFollowing(String)
|
||||||
*/
|
*/
|
||||||
public List<User> getAllFollowing() throws WeixinException {
|
public List<User> getAllFollowing() throws WeixinException {
|
||||||
@ -187,6 +211,35 @@ public class UserApi extends MpApi {
|
|||||||
return userList;
|
return userList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取公众号全部的关注者列表 <font corlor="red">请慎重使用</font>
|
||||||
|
* <p>
|
||||||
|
* 当公众号关注者数量超过10000时,可通过填写next_openid的值,从而多次拉取列表的方式来满足需求,
|
||||||
|
* 将上一次调用得到的返回中的next_openid值,作为下一次调用中的next_openid值
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return 用户openid集合
|
||||||
|
* @throws WeixinException
|
||||||
|
* @see <a href=
|
||||||
|
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN">
|
||||||
|
* 获取关注者列表</a>
|
||||||
|
* @see com.foxinmy.weixin4j.mp.api.UserApi#getFollowingOpenIds(String)
|
||||||
|
*/
|
||||||
|
public List<String> getAllFollowingOpenIds() throws WeixinException {
|
||||||
|
List<String> openIds = new ArrayList<String>();
|
||||||
|
String nextOpenId = null;
|
||||||
|
Following f = null;
|
||||||
|
for (;;) {
|
||||||
|
f = getFollowingOpenIds(nextOpenId);
|
||||||
|
if (f.getCount() == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
openIds.addAll(f.getOpenIds());
|
||||||
|
nextOpenId = f.getNextOpenId();
|
||||||
|
}
|
||||||
|
return openIds;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置用户备注名
|
* 设置用户备注名
|
||||||
*
|
*
|
||||||
@ -195,18 +248,17 @@ public class UserApi extends MpApi {
|
|||||||
* @param remark
|
* @param remark
|
||||||
* 备注名
|
* 备注名
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://mp.weixin.qq.com/wiki/10/bf8f4e3074e1cf91eb6518b6d08d223e.html">设置用户备注名</a>
|
* "http://mp.weixin.qq.com/wiki/10/bf8f4e3074e1cf91eb6518b6d08d223e.html">
|
||||||
|
* 设置用户备注名</a>
|
||||||
*/
|
*/
|
||||||
public JsonResult remarkUserName(String openId, String remark)
|
public JsonResult remarkUserName(String openId, String remark) throws WeixinException {
|
||||||
throws WeixinException {
|
|
||||||
String updateremark_uri = getRequestUri("updateremark_uri");
|
String updateremark_uri = getRequestUri("updateremark_uri");
|
||||||
Token token = tokenHolder.getToken();
|
Token token = tokenHolder.getToken();
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
obj.put("openid", openId);
|
obj.put("openid", openId);
|
||||||
obj.put("remark", remark);
|
obj.put("remark", remark);
|
||||||
WeixinResponse response = weixinExecutor.post(
|
WeixinResponse response = weixinExecutor.post(String.format(updateremark_uri, token.getAccessToken()),
|
||||||
String.format(updateremark_uri, token.getAccessToken()),
|
|
||||||
obj.toJSONString());
|
obj.toJSONString());
|
||||||
|
|
||||||
return response.getAsJsonResult();
|
return response.getAsJsonResult();
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package com.foxinmy.weixin4j.mp.model;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.alibaba.fastjson.annotation.JSONField;
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,8 +27,8 @@ public class Following implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 列表数据,OPENID的列表
|
* 列表数据,OPENID的列表
|
||||||
*/
|
*/
|
||||||
@JSONField(name = "data")
|
@JSONField(deserialize = false)
|
||||||
private JSONObject dataJson;
|
private List<String> openIds;
|
||||||
/**
|
/**
|
||||||
* 拉取列表的后一个用户的OPENID
|
* 拉取列表的后一个用户的OPENID
|
||||||
*/
|
*/
|
||||||
@ -40,6 +39,7 @@ public class Following implements Serializable {
|
|||||||
*
|
*
|
||||||
* @see com.foxinmy.weixin4j.mp.model.User
|
* @see com.foxinmy.weixin4j.mp.model.User
|
||||||
*/
|
*/
|
||||||
|
@JSONField(deserialize = false)
|
||||||
private List<User> userList;
|
private List<User> userList;
|
||||||
|
|
||||||
public int getTotal() {
|
public int getTotal() {
|
||||||
@ -66,12 +66,12 @@ public class Following implements Serializable {
|
|||||||
this.userList = userList;
|
this.userList = userList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject getDataJson() {
|
public List<String> getOpenIds() {
|
||||||
return dataJson;
|
return openIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDataJson(JSONObject dataJson) {
|
public void setOpenIds(List<String> openIds) {
|
||||||
this.dataJson = dataJson;
|
this.openIds = openIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNextOpenId() {
|
public String getNextOpenId() {
|
||||||
@ -84,17 +84,7 @@ public class Following implements Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
return "Following [total=" + total + ", count=" + count + ", openIds=" + openIds + ", nextOpenId=" + nextOpenId
|
||||||
sb.append("[Following total=").append(total);
|
+ ", userList=" + userList + "]";
|
||||||
sb.append(", count=").append(count);
|
|
||||||
if (userList != null && !userList.isEmpty()) {
|
|
||||||
sb.append(", users={");
|
|
||||||
for (User u : userList) {
|
|
||||||
sb.append(u.toString());
|
|
||||||
}
|
|
||||||
sb.append("}");
|
|
||||||
}
|
|
||||||
sb.append(", nextOpenId=").append(nextOpenId).append("]");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user