Skip to content

Commit 94b42ee

Browse files
committed
优化门店查询列表的接口, for issue binarywang#17
1 parent c7a04d5 commit 94b42ee

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.chanjar.weixin.common.exception.WxErrorException;
66
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
77
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
8+
import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult;
89

910
/**
1011
* 门店管理的相关接口代码
@@ -36,7 +37,7 @@ public interface WxMpStoreService {
3637
* @param limit 返回数据条数,最大允许50,默认为20
3738
* @throws WxErrorException
3839
*/
39-
List<WxMpStoreInfo> list(int begin, int limit) throws WxErrorException;
40+
WxMpStoreListResult list(int begin, int limit) throws WxErrorException;
4041

4142
/**
4243
* <pre>

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImpl.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void checkParameters(WxMpStoreBaseInfo request) {
6767
}
6868

6969
@Override
70-
public List<WxMpStoreInfo> list(int begin, int limit)
70+
public WxMpStoreListResult list(int begin, int limit)
7171
throws WxErrorException {
7272
String url = API_BASE_URL + "/getpoilist";
7373
JsonObject params = new JsonObject();
@@ -80,33 +80,25 @@ public List<WxMpStoreInfo> list(int begin, int limit)
8080
throw new WxErrorException(wxError);
8181
}
8282

83-
return WxMpStoreListResult.fromJson(response).getBusinessList();
83+
return WxMpStoreListResult.fromJson(response);
8484
}
8585

8686
@Override
8787
public List<WxMpStoreInfo> listAll() throws WxErrorException {
88-
int limit = 10;
89-
String url = API_BASE_URL + "/getpoilist";
90-
JsonObject params = new JsonObject();
91-
params.addProperty("begin", 0);
92-
params.addProperty("limit", limit);//返回数据条数,最大允许50,默认为20
93-
String response = this.wxMpService.post(url, params.toString());
94-
95-
WxError wxError = WxError.fromJson(response);
96-
if (wxError.getErrorCode() != 0) {
97-
throw new WxErrorException(wxError);
98-
}
99-
100-
WxMpStoreListResult listResult = WxMpStoreListResult.fromJson(response);
101-
List<WxMpStoreInfo> stores = Lists
102-
.newArrayList(listResult.getBusinessList());
103-
if (listResult.getTotalCount() > limit) {
104-
params = new JsonObject();
105-
params.addProperty("begin", limit);
106-
params.addProperty("limit", listResult.getTotalCount() - limit);
107-
stores.addAll(WxMpStoreListResult
108-
.fromJson(this.wxMpService.post(url, params.toString()))
109-
.getBusinessList());
88+
int limit = 50;
89+
WxMpStoreListResult list = this.list(0, limit);
90+
List<WxMpStoreInfo> stores = list.getBusinessList();
91+
if (list.getTotalCount() > limit) {
92+
int begin = limit;
93+
WxMpStoreListResult followingList = this.list(begin, limit);
94+
while (followingList.getBusinessList().size() > 0) {
95+
stores.addAll(followingList.getBusinessList());
96+
begin += limit;
97+
if (begin >= list.getTotalCount()) {
98+
break;
99+
}
100+
followingList = this.list(begin, limit);
101+
}
110102
}
111103

112104
return stores;

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import me.chanjar.weixin.mp.api.ApiTestModule;
1919
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
2020
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
21+
import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult;
2122

2223
/**
2324
* @author 王彬 (Binary Wang)
@@ -37,17 +38,16 @@ public void testAdd() throws WxErrorException {
3738
this.wxMpService.getStoreService()
3839
.add(WxMpStoreBaseInfo.builder().businessName("haha").branchName("abc")
3940
.province("aaa").district("aaa").telephone("122").address("abc")
40-
.categories(new String[] { "美食,江浙菜" })
41+
.categories(new String[] { "美食,江浙菜" })
4142
.longitude(new BigDecimal("115.32375"))
4243
.latitude(new BigDecimal("25.097486")).city("aaa").offsetType(1)
4344
.build());
4445
}
4546

4647
public void testList() throws WxErrorException {
47-
List<WxMpStoreInfo> list = this.wxMpService.getStoreService().list(0, 10);
48-
assertNotNull(list);
49-
System.err.println(list.size());
50-
System.err.println(list);
48+
WxMpStoreListResult result = this.wxMpService.getStoreService().list(0, 10);
49+
assertNotNull(result);
50+
System.err.println(result);
5151
}
5252

5353
public void testListAll() throws WxErrorException {

0 commit comments

Comments
 (0)