diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpDataCubeService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpDataCubeService.java new file mode 100644 index 00000000..f42829e7 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpDataCubeService.java @@ -0,0 +1,37 @@ +package me.chanjar.weixin.mp.api; + +import java.util.Date; +import java.util.List; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; +import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; + +/** + * 统计分析相关接口 + * Created by Binary Wang on 2016/8/23. + * @author binarywang (https://github.com/binarywang) + */ +public interface WxMpDataCubeService { + /** + *
+   * 获取用户增减数据
+   * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
+   * 
+ * + * @param beginDate 最大时间跨度7天 + * @param endDate endDate不能早于begingDate + */ + List getUserSummary(Date beginDate, Date endDate) throws WxErrorException; + + /** + *
+   * 获取累计用户数据
+   * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
+   * 
+ * + * @param beginDate 最大时间跨度7天 + * @param endDate endDate不能早于begingDate + */ + List getUserCumulate(Date beginDate, Date endDate) throws WxErrorException; +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index 31c65e64..7acd2342 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -336,4 +336,11 @@ public interface WxMpService { * @return WxMpPayService */ WxMpPayService getPayService(); + + /** + * 返回数据分析统计相关接口的方法实现类,以方便调用个其各种接口 + * + * @return WxMpDataCubeService + */ + WxMpDataCubeService getDataCubeService(); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserService.java index 0705e990..a071b0a2 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserService.java @@ -2,12 +2,7 @@ package me.chanjar.weixin.mp.api; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.bean.result.WxMpUser; -import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; import me.chanjar.weixin.mp.bean.result.WxMpUserList; -import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; - -import java.util.Date; -import java.util.List; /** * 用户管理和统计相关操作接口 @@ -47,26 +42,4 @@ public interface WxMpUserService { * @param next_openid 可选,第一个拉取的OPENID,null为从头开始拉取 */ WxMpUserList userList(String next_openid) throws WxErrorException; - - /** - *
-   * 获取用户增减数据
-   * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
-   * 
- * - * @param beginDate 最大时间跨度7天 - * @param endDate endDate不能早于begingDate - */ - List dataCubeUserSummary(Date beginDate, Date endDate) throws WxErrorException; - - /** - *
-   * 获取累计用户数据
-   * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
-   * 
- * - * @param beginDate 最大时间跨度7天 - * @param endDate endDate不能早于begingDate - */ - List dataCubeUserCumulate(Date beginDate, Date endDate) throws WxErrorException; } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImpl.java new file mode 100644 index 00000000..3b577713 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImpl.java @@ -0,0 +1,52 @@ +package me.chanjar.weixin.mp.api.impl; + +import java.util.Date; +import java.util.List; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.api.WxMpDataCubeService; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; +import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; +import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; + +/** + * Created by Binary Wang on 2016/8/23. + * @author binarywang (https://github.com/binarywang) + */ +public class WxMpDataCubeServiceImpl implements WxMpDataCubeService { + private static final String API_URL_PREFIX = "https://api.weixin.qq.com/datacube"; + private WxMpService wxMpService; + + public WxMpDataCubeServiceImpl(WxMpService wxMpService) { + this.wxMpService = wxMpService; + } + + @Override + public List getUserSummary(Date beginDate, Date endDate) throws WxErrorException { + String url = API_URL_PREFIX + "/getusersummary"; + JsonObject param = new JsonObject(); + param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate)); + param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate)); + String responseContent = this.wxMpService.post(url, param.toString()); + return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), + new TypeToken>() { + }.getType()); + } + + @Override + public List getUserCumulate(Date beginDate, Date endDate) throws WxErrorException { + String url = API_URL_PREFIX + "/getusercumulate"; + JsonObject param = new JsonObject(); + param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate)); + param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate)); + String responseContent = this.wxMpService.post(url, param.toString()); + return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"), + new TypeToken>() { + }.getType()); + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java index be74fbc5..f510d01f 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java @@ -64,6 +64,8 @@ public class WxMpServiceImpl implements WxMpService { private WxMpPayService payService = new WxMpPayServiceImpl(this); + private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this); + private CloseableHttpClient httpClient; private HttpHost httpProxy; @@ -527,4 +529,9 @@ public class WxMpServiceImpl implements WxMpService { return this.payService; } + @Override + public WxMpDataCubeService getDataCubeService() { + return this.dataCubeService; + } + } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImpl.java index f2ceac34..c05eeb9f 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImpl.java @@ -1,24 +1,14 @@ package me.chanjar.weixin.mp.api.impl; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.internal.Streams; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; + import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpUserService; import me.chanjar.weixin.mp.bean.result.WxMpUser; -import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; import me.chanjar.weixin.mp.bean.result.WxMpUserList; -import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; -import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; - -import java.io.StringReader; -import java.util.Date; -import java.util.List; /** * Created by Binary Wang on 2016/7/21. @@ -55,29 +45,4 @@ public class WxMpUserServiceImpl implements WxMpUserService { return WxMpUserList.fromJson(responseContent); } - @Override - public List dataCubeUserSummary(Date beginDate, Date endDate) throws WxErrorException { - String url = "https://api.weixin.qq.com/datacube/getusersummary"; - JsonObject param = new JsonObject(); - param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate)); - param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate)); - String responseContent = this.wxMpService.post(url, param.toString()); - JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); - return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), - new TypeToken>() { - }.getType()); - } - - @Override - public List dataCubeUserCumulate(Date beginDate, Date endDate) throws WxErrorException { - String url = "https://api.weixin.qq.com/datacube/getusercumulate"; - JsonObject param = new JsonObject(); - param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate)); - param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate)); - String responseContent = this.wxMpService.post(url, param.toString()); - JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); - return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), - new TypeToken>() { - }.getType()); - } } diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImplTest.java new file mode 100644 index 00000000..ebfdee43 --- /dev/null +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImplTest.java @@ -0,0 +1,51 @@ +package me.chanjar.weixin.mp.api.impl; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +import org.testng.Assert; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +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.result.WxMpUserCumulate; +import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; + +/** + * 测试统计分析相关的接口 + * Created by Binary Wang on 2016/8/23. + * @author binarywang (https://github.com/binarywang) + */ +@Guice(modules = ApiTestModule.class) +public class WxMpDataCubeServiceImplTest { + private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + @Inject + protected WxMpServiceImpl wxService; + + @Test + public void testGetUserSummary() throws WxErrorException, ParseException { + Date beginDate = simpleDateFormat.parse("2016-08-20"); + Date endDate = simpleDateFormat.parse("2016-08-22"); + List summaries = this.wxService.getDataCubeService() + .getUserSummary(beginDate, endDate); + Assert.assertNotNull(summaries); + System.out.println(summaries); + } + + @Test + public void testGetUserCumulate() throws WxErrorException, ParseException { + Date beginDate = simpleDateFormat.parse("2016-08-21"); + Date endDate = simpleDateFormat.parse("2016-08-22"); + List cumulates = this.wxService.getDataCubeService() + .getUserCumulate(beginDate, endDate); + Assert.assertNotNull(cumulates); + System.out.println(cumulates); + } + +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImplTest.java index af5289f0..05ac40c9 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImplTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImplTest.java @@ -1,20 +1,15 @@ package me.chanjar.weixin.mp.api.impl; +import org.testng.Assert; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + 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.result.WxMpUser; -import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; import me.chanjar.weixin.mp.bean.result.WxMpUserList; -import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; -import org.testng.Assert; -import org.testng.annotations.Guice; -import org.testng.annotations.Test; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; /** * 测试用户相关的接口 @@ -61,24 +56,4 @@ public class WxMpUserServiceImplTest { this.wxService.getGroupService().userUpdateGroup(configStorage.getOpenId(), this.wxService.getGroupService().groupGet().get(3).getId()); } - @Test - public void testGetUserSummary() throws WxErrorException, ParseException { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - Date beginDate = simpleDateFormat.parse("2015-01-01"); - Date endDate = simpleDateFormat.parse("2015-01-02"); - List summaries = this.wxService.getUserService().dataCubeUserSummary(beginDate, endDate); - Assert.assertNotNull(summaries); - System.out.println(summaries); - } - - @Test - public void testGetUserCumulate() throws WxErrorException, ParseException { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - Date beginDate = simpleDateFormat.parse("2015-01-01"); - Date endDate = simpleDateFormat.parse("2015-01-02"); - List cumulates = this.wxService.getUserService().dataCubeUserCumulate(beginDate, endDate); - Assert.assertNotNull(cumulates); - System.out.println(cumulates); - } - }