@@ -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; | |||
} |
@@ -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; | |||
} | |||
} |
@@ -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; | |||
} | |||
} |
@@ -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; | |||
} | |||
} |
@@ -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<WxMpStoreInfo> 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<WxMpStoreInfo> getBusinessList() { | |||
return this.businessList; | |||
} | |||
public void setBusinessList(List<WxMpStoreInfo> businessList) { | |||
this.businessList = businessList; | |||
} | |||
} |
@@ -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<WxMpStoreInfo> list = this.wxMpService.getStoreService().list(0, 10); | |||
assertNotNull(list); | |||
System.err.println(list.size()); | |||
System.err.println(list); | |||
} | |||
public void testListAll() throws WxErrorException { | |||
List<WxMpStoreInfo> list = this.wxMpService.getStoreService().listAll(); | |||
assertNotNull(list); | |||
System.err.println(list.size()); | |||
System.err.println(list); | |||
} | |||
} |