@@ -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 { | |||
/** | |||
* <pre> | |||
* 获取用户增减数据 | |||
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html | |||
* </pre> | |||
* | |||
* @param beginDate 最大时间跨度7天 | |||
* @param endDate endDate不能早于begingDate | |||
*/ | |||
List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获取累计用户数据 | |||
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html | |||
* </pre> | |||
* | |||
* @param beginDate 最大时间跨度7天 | |||
* @param endDate endDate不能早于begingDate | |||
*/ | |||
List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException; | |||
} |
@@ -336,4 +336,11 @@ public interface WxMpService { | |||
* @return WxMpPayService | |||
*/ | |||
WxMpPayService getPayService(); | |||
/** | |||
* 返回数据分析统计相关接口的方法实现类,以方便调用个其各种接口 | |||
* | |||
* @return WxMpDataCubeService | |||
*/ | |||
WxMpDataCubeService getDataCubeService(); | |||
} |
@@ -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; | |||
/** | |||
* <pre> | |||
* 获取用户增减数据 | |||
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html | |||
* </pre> | |||
* | |||
* @param beginDate 最大时间跨度7天 | |||
* @param endDate endDate不能早于begingDate | |||
*/ | |||
List<WxMpUserSummary> dataCubeUserSummary(Date beginDate, Date endDate) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获取累计用户数据 | |||
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html | |||
* </pre> | |||
* | |||
* @param beginDate 最大时间跨度7天 | |||
* @param endDate endDate不能早于begingDate | |||
*/ | |||
List<WxMpUserCumulate> dataCubeUserCumulate(Date beginDate, Date endDate) throws WxErrorException; | |||
} |
@@ -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<WxMpUserSummary> 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<List<WxMpUserSummary>>() { | |||
}.getType()); | |||
} | |||
@Override | |||
public List<WxMpUserCumulate> 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<List<WxMpUserCumulate>>() { | |||
}.getType()); | |||
} | |||
} |
@@ -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; | |||
} | |||
} |
@@ -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<WxMpUserSummary> 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<List<WxMpUserSummary>>() { | |||
}.getType()); | |||
} | |||
@Override | |||
public List<WxMpUserCumulate> 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<List<WxMpUserCumulate>>() { | |||
}.getType()); | |||
} | |||
} |
@@ -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<WxMpUserSummary> 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<WxMpUserCumulate> cumulates = this.wxService.getDataCubeService() | |||
.getUserCumulate(beginDate, endDate); | |||
Assert.assertNotNull(cumulates); | |||
System.out.println(cumulates); | |||
} | |||
} |
@@ -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<WxMpUserSummary> 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<WxMpUserCumulate> cumulates = this.wxService.getUserService().dataCubeUserCumulate(beginDate, endDate); | |||
Assert.assertNotNull(cumulates); | |||
System.out.println(cumulates); | |||
} | |||
} |