From bda5ab01e5220d94f78693d2041eb5ff4d0468e3 Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Tue, 27 Sep 2016 19:58:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=97=A8=E5=BA=97=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=88=97=E8=A1=A8=E7=9A=84=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=88=9B=E5=BB=BA=E9=97=A8=E5=BA=97?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3=EF=BC=8C=20for=20issue=20#17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/mp/api/WxMpStoreService.java | 27 +++++- .../mp/api/impl/WxMpStoreServiceImpl.java | 56 ++++++++++++- .../bean/{ => store}/WxMpStoreBaseInfo.java | 75 ++++++++++++++++- .../weixin/mp/bean/store/WxMpStoreInfo.java | 29 +++++++ .../mp/bean/store/WxMpStoreListResult.java | 84 +++++++++++++++++++ .../mp/api/impl/WxMpStoreServiceImplTest.java | 24 +++++- 6 files changed, 286 insertions(+), 9 deletions(-) rename weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/{ => store}/WxMpStoreBaseInfo.java (84%) create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreInfo.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreListResult.java diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java index 3b445791..421204e6 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java @@ -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; + + /** + *
+   * 查询门店列表(指定查询起始位置和个数)
+   * 商户可以通过该接口,批量查询自己名下的门店list,并获取已审核通过的poi_id(所有状态均会返回poi_id,但该poi_id不一定为最终id)、商户自身sid 用于对应、商户名、分店名、地址字段。
+   * 详情请见: 微信门店接口
+   * 
+ * @param begin 开始位置,0 即为从第一条开始查询 + * @param limit 返回数据条数,最大允许50,默认为20 + * @throws WxErrorException + */ + List list(int begin, int limit) throws WxErrorException; + + /** + *
+   * 查询门店列表(所有)
+   * 商户可以通过该接口,批量查询自己名下的门店list,并获取已审核通过的poi_id(所有状态均会返回poi_id,但该poi_id不一定为最终id)、商户自身sid 用于对应、商户名、分店名、地址字段。
+   * 详情请见: 微信门店接口
+   * 
+ * @throws WxErrorException + */ + List listAll() throws WxErrorException; } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImpl.java index 9ed28df5..83d6a0f0 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImpl.java @@ -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 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 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 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; + } + } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpStoreBaseInfo.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreBaseInfo.java similarity index 84% rename from weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpStoreBaseInfo.java rename to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreBaseInfo.java index bb097a69..06a5b178 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpStoreBaseInfo.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreBaseInfo.java @@ -1,8 +1,11 @@ -package me.chanjar.weixin.mp.bean; +package me.chanjar.weixin.mp.bean.store; import java.math.BigDecimal; import java.util.List; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.annotations.SerializedName; @@ -16,6 +19,10 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; * Created by Binary Wang on 2016-09-23. */ public class WxMpStoreBaseInfo { + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); + } public String toJson() { JsonElement base_info = WxMpGsonBuilder.create().toJsonTree(this); @@ -180,6 +187,24 @@ public class WxMpStoreBaseInfo { @SerializedName("avg_price") private Integer avgPrice; + /** + * 门店是否可用状态。1 表示系统错误、2 表示审核中、3 审核通过、4 审核驳回。当该字段为1、2、4 状态时,poi_id 为空 + */ + @SerializedName("available_state") + private Integer availableState; + + /** + * 扩展字段是否正在更新中。1 表示扩展字段正在更新中,尚未生效,不允许再次更新; 0 表示扩展字段没有在更新中或更新已生效,可以再次更新 + */ + @SerializedName("update_status") + private Integer updateStatus; + + /** + * 门店poi id + */ + @SerializedName("poi_id") + private String poiId; + public String getSid() { return this.sid; } @@ -324,6 +349,30 @@ public class WxMpStoreBaseInfo { this.avgPrice = avgPrice; } + public Integer getAvailableState() { + return this.availableState; + } + + public void setAvailableState(Integer availableState) { + this.availableState = availableState; + } + + public Integer getUpdateStatus() { + return this.updateStatus; + } + + public void setUpdateStatus(Integer updateStatus) { + this.updateStatus = updateStatus; + } + + public String getPoiId() { + return this.poiId; + } + + public void setPoiId(String poiId) { + this.poiId = poiId; + } + public static WxMpStoreBaseInfoBuilder builder() { return new WxMpStoreBaseInfoBuilder(); } @@ -347,6 +396,9 @@ public class WxMpStoreBaseInfo { private String introduction; private String openTime; private Integer avgPrice; + private Integer availableState; + private Integer updateStatus; + private String poiId; public WxMpStoreBaseInfoBuilder sid(String sid) { this.sid = sid; @@ -438,6 +490,21 @@ public class WxMpStoreBaseInfo { return this; } + public WxMpStoreBaseInfoBuilder availableState(Integer availableState) { + this.availableState = availableState; + return this; + } + + public WxMpStoreBaseInfoBuilder updateStatus(Integer updateStatus) { + this.updateStatus = updateStatus; + return this; + } + + public WxMpStoreBaseInfoBuilder poiId(String poiId) { + this.poiId = poiId; + return this; + } + public WxMpStoreBaseInfoBuilder from(WxMpStoreBaseInfo origin) { this.sid(origin.sid); this.businessName(origin.businessName); @@ -457,6 +524,9 @@ public class WxMpStoreBaseInfo { this.introduction(origin.introduction); this.openTime(origin.openTime); this.avgPrice(origin.avgPrice); + this.availableState(origin.availableState); + this.updateStatus(origin.updateStatus); + this.poiId(origin.poiId); return this; } @@ -480,6 +550,9 @@ public class WxMpStoreBaseInfo { m.introduction = this.introduction; m.openTime = this.openTime; m.avgPrice = this.avgPrice; + m.availableState = this.availableState; + m.updateStatus = this.updateStatus; + m.poiId = this.poiId; return m; } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreInfo.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreInfo.java new file mode 100644 index 00000000..eae6dc6e --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreInfo.java @@ -0,0 +1,29 @@ +/** + * Copyright(c) 2011-2016 by UCredit Inc. + * All Rights Reserved + */ +package me.chanjar.weixin.mp.bean.store; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +import com.google.gson.annotations.SerializedName; + +public class WxMpStoreInfo { + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); + } + + @SerializedName("base_info") + private WxMpStoreBaseInfo baseInfo; + + public WxMpStoreBaseInfo getBaseInfo() { + return this.baseInfo; + } + + public void setBaseInfo(WxMpStoreBaseInfo baseInfo) { + this.baseInfo = baseInfo; + } + +} \ No newline at end of file diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreListResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreListResult.java new file mode 100644 index 00000000..2bc514ed --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreListResult.java @@ -0,0 +1,84 @@ +package me.chanjar.weixin.mp.bean.store; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +import com.google.gson.annotations.SerializedName; + +import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; + +/** + * 门店列表结果类 + * @author binarywang(https://github.com/binarywang) + * Created by Binary Wang on 2016-09-27. + * + */ +public class WxMpStoreListResult { + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); + } + + public static WxMpStoreListResult fromJson(String json) { + return WxMpGsonBuilder.create().fromJson(json, WxMpStoreListResult.class); + } + + /** + * 错误码,0为正常 + */ + @SerializedName("errcode") + private Integer errCode; + + /** + * 错误信息 + */ + @SerializedName("errmsg") + private String errMsg; + + /** + * 门店信息列表 + */ + @SerializedName("business_list") + private List businessList; + + /** + * 门店信息总数 + */ + @SerializedName("total_count") + private Integer totalCount; + + public Integer getTotalCount() { + return this.totalCount; + } + + public void setTotalCount(Integer totalCount) { + this.totalCount = totalCount; + } + + public Integer getErrCode() { + return this.errCode; + } + + public void setErrCode(Integer errCode) { + this.errCode = errCode; + } + + public String getErrMsg() { + return this.errMsg; + } + + public void setErrMsg(String errMsg) { + this.errMsg = errMsg; + } + + public List getBusinessList() { + return this.businessList; + } + + public void setBusinessList(List businessList) { + this.businessList = businessList; + } + +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java index 1e1ed49c..6f655232 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java @@ -4,7 +4,10 @@ */ package me.chanjar.weixin.mp.api.impl; +import static org.junit.Assert.assertNotNull; + import java.math.BigDecimal; +import java.util.List; import org.testng.annotations.Guice; import org.testng.annotations.Test; @@ -13,7 +16,8 @@ import com.google.inject.Inject; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.ApiTestModule; -import me.chanjar.weixin.mp.bean.WxMpStoreBaseInfo; +import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo; +import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo; /** * @author 王彬 (Binary Wang) @@ -26,17 +30,31 @@ public class WxMpStoreServiceImplTest { private WxMpServiceImpl wxMpService; /** - * Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpStoreServiceImpl#add(me.chanjar.weixin.mp.bean.WxMpStoreBaseInfo)}. + * Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpStoreServiceImpl#add(me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo)}. * @throws WxErrorException */ public void testAdd() throws WxErrorException { 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 list = this.wxMpService.getStoreService().list(0, 10); + assertNotNull(list); + System.err.println(list.size()); + System.err.println(list); + } + + public void testListAll() throws WxErrorException { + List list = this.wxMpService.getStoreService().listAll(); + assertNotNull(list); + System.err.println(list.size()); + System.err.println(list); + } + }