Skip to content

Commit 5525c4f

Browse files
aimilaimil
authored andcommitted
添加批量查询用户基本信息功能,没有测试,没有添加超过100验证
1 parent af9bfa2 commit 5525c4f

File tree

4 files changed

+456
-192
lines changed

4 files changed

+456
-192
lines changed
Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package me.chanjar.weixin.mp.api;
22

3+
import java.util.List;
4+
35
import me.chanjar.weixin.common.exception.WxErrorException;
6+
import me.chanjar.weixin.mp.bean.WxMpUserQuery;
47
import me.chanjar.weixin.mp.bean.result.WxMpUser;
58
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
69

@@ -11,35 +14,55 @@
1114
*/
1215
public interface WxMpUserService {
1316

14-
/**
15-
* <pre>
16-
* 设置用户备注名接口
17-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=设置用户备注名接口
18-
* </pre>
19-
*
20-
* @param openid 用户openid
21-
* @param remark 备注名
22-
*/
23-
void userUpdateRemark(String openid, String remark) throws WxErrorException;
24-
25-
/**
26-
* <pre>
27-
* 获取用户基本信息
28-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取用户基本信息
29-
* </pre>
30-
*
31-
* @param openid 用户openid
32-
* @param lang 语言,zh_CN 简体(默认),zh_TW 繁体,en 英语
33-
*/
34-
WxMpUser userInfo(String openid, String lang) throws WxErrorException;
35-
36-
/**
37-
* <pre>
38-
* 获取关注者列表
39-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表
40-
* </pre>
41-
*
42-
* @param next_openid 可选,第一个拉取的OPENID,null为从头开始拉取
43-
*/
44-
WxMpUserList userList(String next_openid) throws WxErrorException;
17+
/**
18+
* <pre>
19+
* 设置用户备注名接口
20+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=设置用户备注名接口
21+
* </pre>
22+
*
23+
* @param openid 用户openid
24+
* @param remark 备注名
25+
*/
26+
void userUpdateRemark(String openid, String remark) throws WxErrorException;
27+
28+
/**
29+
* <pre>
30+
* 获取用户基本信息
31+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取用户基本信息
32+
* </pre>
33+
*
34+
* @param openid 用户openid
35+
* @param lang 语言,zh_CN 简体(默认),zh_TW 繁体,en 英语
36+
*/
37+
WxMpUser userInfo(String openid, String lang) throws WxErrorException;
38+
39+
/**
40+
* <pre>
41+
* 获取用户基本信息列表
42+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=批量获取用户基本信息
43+
* </pre>
44+
*
45+
* @param openid 用户openid, lang 使用默认(zh_CN 简体)
46+
*/
47+
List<WxMpUser> userInfoList(List<String> openidList) throws WxErrorException;
48+
49+
/**
50+
* <pre>
51+
* 获取用户基本信息列表
52+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=批量获取用户基本信息
53+
* </pre>
54+
*
55+
* @param userQuery 详细查询参数
56+
*/
57+
List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException;
58+
59+
/**
60+
* <pre>
61+
* 获取关注者列表
62+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表
63+
* </pre>
64+
*
65+
* @param nextOpenid 可选,第一个拉取的OPENID,null为从头开始拉取
66+
*/
67+
WxMpUserList userList(String nextOpenid) throws WxErrorException;
4568
}
Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,63 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.util.List;
4+
35
import com.google.gson.JsonObject;
46

57
import me.chanjar.weixin.common.exception.WxErrorException;
68
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
79
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
810
import me.chanjar.weixin.mp.api.WxMpService;
911
import me.chanjar.weixin.mp.api.WxMpUserService;
12+
import me.chanjar.weixin.mp.bean.WxMpUserQuery;
1013
import me.chanjar.weixin.mp.bean.result.WxMpUser;
1114
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
1215

1316
/**
1417
* Created by Binary Wang on 2016/7/21.
1518
*/
1619
public class WxMpUserServiceImpl implements WxMpUserService {
17-
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/user";
18-
private WxMpService wxMpService;
19-
20-
public WxMpUserServiceImpl(WxMpService wxMpService) {
21-
this.wxMpService = wxMpService;
22-
}
23-
24-
@Override
25-
public void userUpdateRemark(String openid, String remark) throws WxErrorException {
26-
String url = API_URL_PREFIX + "/info/updateremark";
27-
JsonObject json = new JsonObject();
28-
json.addProperty("openid", openid);
29-
json.addProperty("remark", remark);
30-
this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString());
31-
}
32-
33-
@Override
34-
public WxMpUser userInfo(String openid, String lang) throws WxErrorException {
35-
String url = API_URL_PREFIX + "/info";
36-
lang = lang == null ? "zh_CN" : lang;
37-
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang);
38-
return WxMpUser.fromJson(responseContent);
39-
}
40-
41-
@Override
42-
public WxMpUserList userList(String next_openid) throws WxErrorException {
43-
String url = API_URL_PREFIX + "/get";
44-
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid);
45-
return WxMpUserList.fromJson(responseContent);
46-
}
20+
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/user";
21+
private WxMpService wxMpService;
22+
23+
public WxMpUserServiceImpl(WxMpService wxMpService) {
24+
this.wxMpService = wxMpService;
25+
}
26+
27+
@Override
28+
public void userUpdateRemark(String openid, String remark) throws WxErrorException {
29+
String url = API_URL_PREFIX + "/info/updateremark";
30+
JsonObject json = new JsonObject();
31+
json.addProperty("openid", openid);
32+
json.addProperty("remark", remark);
33+
this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString());
34+
}
35+
36+
@Override
37+
public WxMpUser userInfo(String openid, String lang) throws WxErrorException {
38+
String url = API_URL_PREFIX + "/info";
39+
lang = lang == null ? "zh_CN" : lang;
40+
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang);
41+
return WxMpUser.fromJson(responseContent);
42+
}
43+
44+
@Override
45+
public WxMpUserList userList(String next_openid) throws WxErrorException {
46+
String url = API_URL_PREFIX + "/get";
47+
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid);
48+
return WxMpUserList.fromJson(responseContent);
49+
}
50+
51+
@Override
52+
public List<WxMpUser> userInfoList(List<String> openidList) throws WxErrorException {
53+
return userInfoList(new WxMpUserQuery(openidList));
54+
}
55+
56+
@Override
57+
public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException {
58+
String url = API_URL_PREFIX + "/info/batchget";
59+
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, userQuery.toJsonString());
60+
return WxMpUser.fromJsonList(responseContent);
61+
}
4762

4863
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import com.google.gson.Gson;
10+
11+
/**
12+
* 批量查询用户信息查询参数 <br>
13+
* Created by LiuJunGuang on 2016/8/31.
14+
*
15+
* @author LiuJunGuang
16+
*/
17+
public class WxMpUserQuery {
18+
private List<WxMpUserQueryParam> queryParamList = new ArrayList<>();
19+
20+
public WxMpUserQuery() {
21+
super();
22+
}
23+
24+
/**
25+
* 语言使用默认(zh_CN)
26+
*
27+
* @description
28+
* @param openIdList
29+
*/
30+
public WxMpUserQuery(List<String> openIdList) {
31+
super();
32+
add(openIdList);
33+
}
34+
35+
/**
36+
* 添加OpenId列表,语言使用默认(zh_CN)
37+
*
38+
* @param openIdList
39+
* @return {@link WxMpUserQuery}
40+
*/
41+
public WxMpUserQuery add(List<String> openIdList) {
42+
for (String openId : openIdList) {
43+
this.add(openId);
44+
}
45+
return this;
46+
}
47+
48+
/**
49+
* 添加一个OpenId
50+
*
51+
* @param openId
52+
* @param lang 国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
53+
* @return {@link WxMpUserQuery}
54+
*/
55+
public WxMpUserQuery add(String openId, String lang) {
56+
queryParamList.add(new WxMpUserQueryParam(openId, lang));
57+
return this;
58+
}
59+
60+
/**
61+
* 添加一个OpenId到列表中,并返回本对象
62+
*
63+
* <pre>
64+
* 该方法默认lang = zh_CN
65+
* </pre>
66+
*
67+
* @param openId
68+
* @return {@link WxMpUserQuery}
69+
*/
70+
public WxMpUserQuery add(String openId) {
71+
queryParamList.add(new WxMpUserQueryParam(openId));
72+
return this;
73+
}
74+
75+
/**
76+
* 删除指定的OpenId,语言使用默认(zh_CN)
77+
*
78+
* @param openId
79+
* @return {@link WxMpUserQuery}
80+
*/
81+
public WxMpUserQuery remove(String openId) {
82+
queryParamList.remove(new WxMpUserQueryParam(openId));
83+
return this;
84+
}
85+
86+
/**
87+
* 删除指定的OpenId
88+
*
89+
* @param openId
90+
* @param lang 国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
91+
* @return {@link WxMpUserQuery}
92+
*/
93+
public WxMpUserQuery remove(String openId, String lang) {
94+
queryParamList.remove(new WxMpUserQueryParam(openId, lang));
95+
return this;
96+
}
97+
98+
/**
99+
* 获取查询参数列表
100+
*
101+
* @return
102+
*/
103+
public List<WxMpUserQueryParam> getQueryParamList() {
104+
return queryParamList;
105+
}
106+
107+
public String toJsonString() {
108+
Map<String, Object> map = new HashMap<>();
109+
map.put("user_list", queryParamList);
110+
return new Gson().toJson(map);
111+
}
112+
113+
// 查询参数封装
114+
public class WxMpUserQueryParam implements Serializable {
115+
/**
116+
* @fields serialVersionUID
117+
*/
118+
private static final long serialVersionUID = -6863571795702385319L;
119+
private String openid;
120+
private String lang;
121+
122+
public WxMpUserQueryParam(String openid, String lang) {
123+
super();
124+
this.openid = openid;
125+
this.lang = lang;
126+
}
127+
128+
public WxMpUserQueryParam(String openid) {
129+
super();
130+
this.openid = openid;
131+
this.lang = "zh_CN";
132+
}
133+
134+
public WxMpUserQueryParam() {
135+
super();
136+
}
137+
138+
public String getOpenid() {
139+
return openid;
140+
}
141+
142+
public void setOpenid(String openid) {
143+
this.openid = openid;
144+
}
145+
146+
public String getLang() {
147+
return lang;
148+
}
149+
150+
public void setLang(String lang) {
151+
this.lang = lang;
152+
}
153+
154+
@Override
155+
public int hashCode() {
156+
final int prime = 31;
157+
int result = 1;
158+
result = prime * result + getOuterType().hashCode();
159+
result = prime * result + ((lang == null) ? 0 : lang.hashCode());
160+
result = prime * result + ((openid == null) ? 0 : openid.hashCode());
161+
return result;
162+
}
163+
164+
@Override
165+
public boolean equals(Object obj) {
166+
if (this == obj)
167+
return true;
168+
if (obj == null)
169+
return false;
170+
if (getClass() != obj.getClass())
171+
return false;
172+
WxMpUserQueryParam other = (WxMpUserQueryParam) obj;
173+
if (!getOuterType().equals(other.getOuterType()))
174+
return false;
175+
if (lang == null) {
176+
if (other.lang != null)
177+
return false;
178+
} else if (!lang.equals(other.lang))
179+
return false;
180+
if (openid == null) {
181+
if (other.openid != null)
182+
return false;
183+
} else if (!openid.equals(other.openid))
184+
return false;
185+
return true;
186+
}
187+
188+
private WxMpUserQuery getOuterType() {
189+
return WxMpUserQuery.this;
190+
}
191+
192+
}
193+
194+
}

0 commit comments

Comments
 (0)