转移统计分析相关接口为单独类,为以后完善补充统计分析接口做准备

This commit is contained in:
BinaryWang 2016-08-23 18:00:34 +08:00
parent a70f937d76
commit bb0147bd57
8 changed files with 161 additions and 94 deletions

View File

@ -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;
}

View File

@ -336,4 +336,11 @@ public interface WxMpService {
* @return WxMpPayService * @return WxMpPayService
*/ */
WxMpPayService getPayService(); WxMpPayService getPayService();
/**
* 返回数据分析统计相关接口的方法实现类以方便调用个其各种接口
*
* @return WxMpDataCubeService
*/
WxMpDataCubeService getDataCubeService();
} }

View File

@ -2,12 +2,7 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpUser; 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.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 可选第一个拉取的OPENIDnull为从头开始拉取 * @param next_openid 可选第一个拉取的OPENIDnull为从头开始拉取
*/ */
WxMpUserList userList(String next_openid) throws WxErrorException; 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;
} }

View File

@ -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());
}
}

View File

@ -64,6 +64,8 @@ public class WxMpServiceImpl implements WxMpService {
private WxMpPayService payService = new WxMpPayServiceImpl(this); private WxMpPayService payService = new WxMpPayServiceImpl(this);
private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this);
private CloseableHttpClient httpClient; private CloseableHttpClient httpClient;
private HttpHost httpProxy; private HttpHost httpProxy;
@ -527,4 +529,9 @@ public class WxMpServiceImpl implements WxMpService {
return this.payService; return this.payService;
} }
@Override
public WxMpDataCubeService getDataCubeService() {
return this.dataCubeService;
}
} }

View File

@ -1,24 +1,14 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; 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.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService; import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUser; 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.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. * Created by Binary Wang on 2016/7/21.
@ -55,29 +45,4 @@ public class WxMpUserServiceImpl implements WxMpUserService {
return WxMpUserList.fromJson(responseContent); 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());
}
} }

View File

@ -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);
}
}

View File

@ -1,20 +1,15 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
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.Assert;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.text.ParseException; import com.google.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.Date; import me.chanjar.weixin.common.exception.WxErrorException;
import java.util.List; import me.chanjar.weixin.mp.api.ApiTestModule;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
/** /**
* 测试用户相关的接口 * 测试用户相关的接口
@ -61,24 +56,4 @@ public class WxMpUserServiceImplTest {
this.wxService.getGroupService().userUpdateGroup(configStorage.getOpenId(), this.wxService.getGroupService().groupGet().get(3).getId()); 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);
}
} }