添加门店查询列表的接口,并修复创建门店的接口, for issue #17

This commit is contained in:
BinaryWang
2016-09-27 19:58:58 +08:00
parent 435eb1250a
commit bda5ab01e5
6 changed files with 286 additions and 9 deletions

View File

@@ -1,7 +1,10 @@
package me.chanjar.weixin.mp.api;
import java.util.List;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpStoreBaseInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
/**
* 门店管理的相关接口代码
@@ -22,4 +25,26 @@ public interface WxMpStoreService {
*
*/
void add(WxMpStoreBaseInfo request) throws WxErrorException;
/**
* <pre>
* 查询门店列表(指定查询起始位置和个数)
* 商户可以通过该接口批量查询自己名下的门店list并获取已审核通过的poi_id所有状态均会返回poi_id但该poi_id不一定为最终id、商户自身sid 用于对应、商户名、分店名、地址字段。
* 详情请见: <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN">微信门店接口</a>
* </pre>
* @param begin 开始位置0 即为从第一条开始查询
* @param limit 返回数据条数最大允许50默认为20
* @throws WxErrorException
*/
List<WxMpStoreInfo> list(int begin, int limit) throws WxErrorException;
/**
* <pre>
* 查询门店列表(所有)
* 商户可以通过该接口批量查询自己名下的门店list并获取已审核通过的poi_id所有状态均会返回poi_id但该poi_id不一定为最终id、商户自身sid 用于对应、商户名、分店名、地址字段。
* 详情请见: <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN">微信门店接口</a>
* </pre>
* @throws WxErrorException
*/
List<WxMpStoreInfo> listAll() throws WxErrorException;
}

View File

@@ -7,13 +7,16 @@ import java.util.Map.Entry;
import org.joor.Reflect;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.annotation.Required;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpStoreService;
import me.chanjar.weixin.mp.bean.WxMpStoreBaseInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult;
/**
* Created by Binary Wang on 2016/9/26.
@@ -21,6 +24,7 @@ import me.chanjar.weixin.mp.bean.WxMpStoreBaseInfo;
*
*/
public class WxMpStoreServiceImpl implements WxMpStoreService {
private static final String API_BASE_URL = "http://api.weixin.qq.com/cgi-bin/poi";
private WxMpService wxMpService;
@@ -32,10 +36,8 @@ public class WxMpStoreServiceImpl implements WxMpStoreService {
public void add(WxMpStoreBaseInfo request) throws WxErrorException {
checkParameters(request);
String url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi";
// String data = "{\"business\":{\"base_info\":{\"business_name\":\"haha\",\"branch_name\":\"abc\",\"province\":\"aaa\",\"city\":\"aaa\",\"district\":\"aaa\",\"telephone\":\"122\",\"categories\":\"adsdas\",\"offset_type\":\"1\",\"longitude\":\"115.32375\",\"latitude\":\"25.097486\"}}}";
String url = API_BASE_URL + "/addpoi";
String response = this.wxMpService.post(url, request.toJson());
// String response = this.wxMpService.post(url, data);
WxError wxError = WxError.fromJson(response);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
@@ -64,4 +66,50 @@ public class WxMpStoreServiceImpl implements WxMpStoreService {
}
@Override
public List<WxMpStoreInfo> list(int begin, int limit)
throws WxErrorException {
String url = API_BASE_URL + "/getpoilist";
JsonObject params = new JsonObject();
params.addProperty("begin", begin);
params.addProperty("limit", limit);
String response = this.wxMpService.post(url, params.toString());
WxError wxError = WxError.fromJson(response);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
return WxMpStoreListResult.fromJson(response).getBusinessList();
}
@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());
}
return stores;
}
}