Skip to content

Commit bda5ab0

Browse files
committed
添加门店查询列表的接口,并修复创建门店的接口, for issue binarywang#17
1 parent 435eb12 commit bda5ab0

File tree

6 files changed

+286
-9
lines changed

6 files changed

+286
-9
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.chanjar.weixin.mp.api;
22

3+
import java.util.List;
4+
35
import me.chanjar.weixin.common.exception.WxErrorException;
4-
import me.chanjar.weixin.mp.bean.WxMpStoreBaseInfo;
6+
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
7+
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
58

69
/**
710
* 门店管理的相关接口代码
@@ -22,4 +25,26 @@ public interface WxMpStoreService {
2225
*
2326
*/
2427
void add(WxMpStoreBaseInfo request) throws WxErrorException;
28+
29+
/**
30+
* <pre>
31+
* 查询门店列表(指定查询起始位置和个数)
32+
* 商户可以通过该接口,批量查询自己名下的门店list,并获取已审核通过的poi_id(所有状态均会返回poi_id,但该poi_id不一定为最终id)、商户自身sid 用于对应、商户名、分店名、地址字段。
33+
* 详情请见: <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN">微信门店接口</a>
34+
* </pre>
35+
* @param begin 开始位置,0 即为从第一条开始查询
36+
* @param limit 返回数据条数,最大允许50,默认为20
37+
* @throws WxErrorException
38+
*/
39+
List<WxMpStoreInfo> list(int begin, int limit) throws WxErrorException;
40+
41+
/**
42+
* <pre>
43+
* 查询门店列表(所有)
44+
* 商户可以通过该接口,批量查询自己名下的门店list,并获取已审核通过的poi_id(所有状态均会返回poi_id,但该poi_id不一定为最终id)、商户自身sid 用于对应、商户名、分店名、地址字段。
45+
* 详情请见: <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN">微信门店接口</a>
46+
* </pre>
47+
* @throws WxErrorException
48+
*/
49+
List<WxMpStoreInfo> listAll() throws WxErrorException;
2550
}

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
import org.joor.Reflect;
88

99
import com.google.common.collect.Lists;
10+
import com.google.gson.JsonObject;
1011

1112
import me.chanjar.weixin.common.annotation.Required;
1213
import me.chanjar.weixin.common.bean.result.WxError;
1314
import me.chanjar.weixin.common.exception.WxErrorException;
1415
import me.chanjar.weixin.mp.api.WxMpService;
1516
import me.chanjar.weixin.mp.api.WxMpStoreService;
16-
import me.chanjar.weixin.mp.bean.WxMpStoreBaseInfo;
17+
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo;
18+
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo;
19+
import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult;
1720

1821
/**
1922
* Created by Binary Wang on 2016/9/26.
2023
* @author binarywang (https://github.com/binarywang)
2124
*
2225
*/
2326
public class WxMpStoreServiceImpl implements WxMpStoreService {
27+
private static final String API_BASE_URL = "http://api.weixin.qq.com/cgi-bin/poi";
2428

2529
private WxMpService wxMpService;
2630

@@ -32,10 +36,8 @@ public WxMpStoreServiceImpl(WxMpService wxMpService) {
3236
public void add(WxMpStoreBaseInfo request) throws WxErrorException {
3337
checkParameters(request);
3438

35-
String url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi";
36-
// 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\"}}}";
39+
String url = API_BASE_URL + "/addpoi";
3740
String response = this.wxMpService.post(url, request.toJson());
38-
// String response = this.wxMpService.post(url, data);
3941
WxError wxError = WxError.fromJson(response);
4042
if (wxError.getErrorCode() != 0) {
4143
throw new WxErrorException(wxError);
@@ -64,4 +66,50 @@ private void checkParameters(WxMpStoreBaseInfo request) {
6466

6567
}
6668

69+
@Override
70+
public List<WxMpStoreInfo> list(int begin, int limit)
71+
throws WxErrorException {
72+
String url = API_BASE_URL + "/getpoilist";
73+
JsonObject params = new JsonObject();
74+
params.addProperty("begin", begin);
75+
params.addProperty("limit", limit);
76+
String response = this.wxMpService.post(url, params.toString());
77+
78+
WxError wxError = WxError.fromJson(response);
79+
if (wxError.getErrorCode() != 0) {
80+
throw new WxErrorException(wxError);
81+
}
82+
83+
return WxMpStoreListResult.fromJson(response).getBusinessList();
84+
}
85+
86+
@Override
87+
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());
110+
}
111+
112+
return stores;
113+
}
114+
67115
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpStoreBaseInfo.java renamed to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/store/WxMpStoreBaseInfo.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
package me.chanjar.weixin.mp.bean;
1+
package me.chanjar.weixin.mp.bean.store;
22

33
import java.math.BigDecimal;
44
import java.util.List;
55

6+
import org.apache.commons.lang3.builder.ToStringBuilder;
7+
import org.apache.commons.lang3.builder.ToStringStyle;
8+
69
import com.google.gson.JsonElement;
710
import com.google.gson.JsonObject;
811
import com.google.gson.annotations.SerializedName;
@@ -16,6 +19,10 @@
1619
* Created by Binary Wang on 2016-09-23.
1720
*/
1821
public class WxMpStoreBaseInfo {
22+
@Override
23+
public String toString() {
24+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
25+
}
1926

2027
public String toJson() {
2128
JsonElement base_info = WxMpGsonBuilder.create().toJsonTree(this);
@@ -180,6 +187,24 @@ public static class WxMpStorePhoto {
180187
@SerializedName("avg_price")
181188
private Integer avgPrice;
182189

190+
/**
191+
* 门店是否可用状态。1 表示系统错误、2 表示审核中、3 审核通过、4 审核驳回。当该字段为1、2、4 状态时,poi_id 为空
192+
*/
193+
@SerializedName("available_state")
194+
private Integer availableState;
195+
196+
/**
197+
* 扩展字段是否正在更新中。1 表示扩展字段正在更新中,尚未生效,不允许再次更新; 0 表示扩展字段没有在更新中或更新已生效,可以再次更新
198+
*/
199+
@SerializedName("update_status")
200+
private Integer updateStatus;
201+
202+
/**
203+
* 门店poi id
204+
*/
205+
@SerializedName("poi_id")
206+
private String poiId;
207+
183208
public String getSid() {
184209
return this.sid;
185210
}
@@ -324,6 +349,30 @@ public void setAvgPrice(Integer avgPrice) {
324349
this.avgPrice = avgPrice;
325350
}
326351

352+
public Integer getAvailableState() {
353+
return this.availableState;
354+
}
355+
356+
public void setAvailableState(Integer availableState) {
357+
this.availableState = availableState;
358+
}
359+
360+
public Integer getUpdateStatus() {
361+
return this.updateStatus;
362+
}
363+
364+
public void setUpdateStatus(Integer updateStatus) {
365+
this.updateStatus = updateStatus;
366+
}
367+
368+
public String getPoiId() {
369+
return this.poiId;
370+
}
371+
372+
public void setPoiId(String poiId) {
373+
this.poiId = poiId;
374+
}
375+
327376
public static WxMpStoreBaseInfoBuilder builder() {
328377
return new WxMpStoreBaseInfoBuilder();
329378
}
@@ -347,6 +396,9 @@ public static class WxMpStoreBaseInfoBuilder {
347396
private String introduction;
348397
private String openTime;
349398
private Integer avgPrice;
399+
private Integer availableState;
400+
private Integer updateStatus;
401+
private String poiId;
350402

351403
public WxMpStoreBaseInfoBuilder sid(String sid) {
352404
this.sid = sid;
@@ -438,6 +490,21 @@ public WxMpStoreBaseInfoBuilder avgPrice(Integer avgPrice) {
438490
return this;
439491
}
440492

493+
public WxMpStoreBaseInfoBuilder availableState(Integer availableState) {
494+
this.availableState = availableState;
495+
return this;
496+
}
497+
498+
public WxMpStoreBaseInfoBuilder updateStatus(Integer updateStatus) {
499+
this.updateStatus = updateStatus;
500+
return this;
501+
}
502+
503+
public WxMpStoreBaseInfoBuilder poiId(String poiId) {
504+
this.poiId = poiId;
505+
return this;
506+
}
507+
441508
public WxMpStoreBaseInfoBuilder from(WxMpStoreBaseInfo origin) {
442509
this.sid(origin.sid);
443510
this.businessName(origin.businessName);
@@ -457,6 +524,9 @@ public WxMpStoreBaseInfoBuilder from(WxMpStoreBaseInfo origin) {
457524
this.introduction(origin.introduction);
458525
this.openTime(origin.openTime);
459526
this.avgPrice(origin.avgPrice);
527+
this.availableState(origin.availableState);
528+
this.updateStatus(origin.updateStatus);
529+
this.poiId(origin.poiId);
460530
return this;
461531
}
462532

@@ -480,6 +550,9 @@ public WxMpStoreBaseInfo build() {
480550
m.introduction = this.introduction;
481551
m.openTime = this.openTime;
482552
m.avgPrice = this.avgPrice;
553+
m.availableState = this.availableState;
554+
m.updateStatus = this.updateStatus;
555+
m.poiId = this.poiId;
483556
return m;
484557
}
485558
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright(c) 2011-2016 by UCredit Inc.
3+
* All Rights Reserved
4+
*/
5+
package me.chanjar.weixin.mp.bean.store;
6+
7+
import org.apache.commons.lang3.builder.ToStringBuilder;
8+
import org.apache.commons.lang3.builder.ToStringStyle;
9+
10+
import com.google.gson.annotations.SerializedName;
11+
12+
public class WxMpStoreInfo {
13+
@Override
14+
public String toString() {
15+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
16+
}
17+
18+
@SerializedName("base_info")
19+
private WxMpStoreBaseInfo baseInfo;
20+
21+
public WxMpStoreBaseInfo getBaseInfo() {
22+
return this.baseInfo;
23+
}
24+
25+
public void setBaseInfo(WxMpStoreBaseInfo baseInfo) {
26+
this.baseInfo = baseInfo;
27+
}
28+
29+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package me.chanjar.weixin.mp.bean.store;
2+
3+
import java.util.List;
4+
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
import org.apache.commons.lang3.builder.ToStringStyle;
7+
8+
import com.google.gson.annotations.SerializedName;
9+
10+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
11+
12+
/**
13+
* 门店列表结果类
14+
* @author binarywang(https://github.com/binarywang)
15+
* Created by Binary Wang on 2016-09-27.
16+
*
17+
*/
18+
public class WxMpStoreListResult {
19+
@Override
20+
public String toString() {
21+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
22+
}
23+
24+
public static WxMpStoreListResult fromJson(String json) {
25+
return WxMpGsonBuilder.create().fromJson(json, WxMpStoreListResult.class);
26+
}
27+
28+
/**
29+
* 错误码,0为正常
30+
*/
31+
@SerializedName("errcode")
32+
private Integer errCode;
33+
34+
/**
35+
* 错误信息
36+
*/
37+
@SerializedName("errmsg")
38+
private String errMsg;
39+
40+
/**
41+
* 门店信息列表
42+
*/
43+
@SerializedName("business_list")
44+
private List<WxMpStoreInfo> businessList;
45+
46+
/**
47+
* 门店信息总数
48+
*/
49+
@SerializedName("total_count")
50+
private Integer totalCount;
51+
52+
public Integer getTotalCount() {
53+
return this.totalCount;
54+
}
55+
56+
public void setTotalCount(Integer totalCount) {
57+
this.totalCount = totalCount;
58+
}
59+
60+
public Integer getErrCode() {
61+
return this.errCode;
62+
}
63+
64+
public void setErrCode(Integer errCode) {
65+
this.errCode = errCode;
66+
}
67+
68+
public String getErrMsg() {
69+
return this.errMsg;
70+
}
71+
72+
public void setErrMsg(String errMsg) {
73+
this.errMsg = errMsg;
74+
}
75+
76+
public List<WxMpStoreInfo> getBusinessList() {
77+
return this.businessList;
78+
}
79+
80+
public void setBusinessList(List<WxMpStoreInfo> businessList) {
81+
this.businessList = businessList;
82+
}
83+
84+
}

0 commit comments

Comments
 (0)