瀏覽代碼

优化门店查询列表的接口, for issue #17

master
Binary Wang 8 年之前
父節點
當前提交
94b42ee9ae
共有 3 個文件被更改,包括 23 次插入30 次删除
  1. +2
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java
  2. +16
    -24
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImpl.java
  3. +5
    -5
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java

+ 2
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java 查看文件

@@ -5,6 +5,7 @@ import java.util.List;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult;

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

/**
* <pre>


+ 16
- 24
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImpl.java 查看文件

@@ -67,7 +67,7 @@ public class WxMpStoreServiceImpl implements WxMpStoreService {
}

@Override
public List<WxMpStoreInfo> list(int begin, int limit)
public WxMpStoreListResult list(int begin, int limit)
throws WxErrorException {
String url = API_BASE_URL + "/getpoilist";
JsonObject params = new JsonObject();
@@ -80,33 +80,25 @@ public class WxMpStoreServiceImpl implements WxMpStoreService {
throw new WxErrorException(wxError);
}

return WxMpStoreListResult.fromJson(response).getBusinessList();
return WxMpStoreListResult.fromJson(response);
}

@Override
public List<WxMpStoreInfo> listAll() throws WxErrorException {
int limit = 10;
String url = API_BASE_URL + "/getpoilist";
JsonObject params = new JsonObject();
params.addProperty("begin", 0);
params.addProperty("limit", limit);//返回数据条数,最大允许50,默认为20
String response = this.wxMpService.post(url, params.toString());

WxError wxError = WxError.fromJson(response);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}

WxMpStoreListResult listResult = WxMpStoreListResult.fromJson(response);
List<WxMpStoreInfo> stores = Lists
.newArrayList(listResult.getBusinessList());
if (listResult.getTotalCount() > limit) {
params = new JsonObject();
params.addProperty("begin", limit);
params.addProperty("limit", listResult.getTotalCount() - limit);
stores.addAll(WxMpStoreListResult
.fromJson(this.wxMpService.post(url, params.toString()))
.getBusinessList());
int limit = 50;
WxMpStoreListResult list = this.list(0, limit);
List<WxMpStoreInfo> stores = list.getBusinessList();
if (list.getTotalCount() > limit) {
int begin = limit;
WxMpStoreListResult followingList = this.list(begin, limit);
while (followingList.getBusinessList().size() > 0) {
stores.addAll(followingList.getBusinessList());
begin += limit;
if (begin >= list.getTotalCount()) {
break;
}
followingList = this.list(begin, limit);
}
}

return stores;


+ 5
- 5
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java 查看文件

@@ -18,6 +18,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.ApiTestModule;
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult;

/**
* @author 王彬 (Binary Wang)
@@ -37,17 +38,16 @@ public class WxMpStoreServiceImplTest {
this.wxMpService.getStoreService()
.add(WxMpStoreBaseInfo.builder().businessName("haha").branchName("abc")
.province("aaa").district("aaa").telephone("122").address("abc")
.categories(new String[] { "美食,江浙菜" })
.categories(new String[] { "美食,江浙菜" })
.longitude(new BigDecimal("115.32375"))
.latitude(new BigDecimal("25.097486")).city("aaa").offsetType(1)
.build());
}

public void testList() throws WxErrorException {
List<WxMpStoreInfo> list = this.wxMpService.getStoreService().list(0, 10);
assertNotNull(list);
System.err.println(list.size());
System.err.println(list);
WxMpStoreListResult result = this.wxMpService.getStoreService().list(0, 10);
assertNotNull(result);
System.err.println(result);
}

public void testListAll() throws WxErrorException {


Loading…
取消
儲存