Skip to content

Commit bb0147b

Browse files
committed
转移统计分析相关接口为单独类,为以后完善补充统计分析接口做准备
1 parent a70f937 commit bb0147b

File tree

8 files changed

+160
-93
lines changed

8 files changed

+160
-93
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
import me.chanjar.weixin.common.exception.WxErrorException;
7+
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
8+
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
9+
10+
/**
11+
* 统计分析相关接口
12+
* Created by Binary Wang on 2016/8/23.
13+
* @author binarywang (https://github.com/binarywang)
14+
*/
15+
public interface WxMpDataCubeService {
16+
/**
17+
* <pre>
18+
* 获取用户增减数据
19+
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
20+
* </pre>
21+
*
22+
* @param beginDate 最大时间跨度7天
23+
* @param endDate endDate不能早于begingDate
24+
*/
25+
List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException;
26+
27+
/**
28+
* <pre>
29+
* 获取累计用户数据
30+
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
31+
* </pre>
32+
*
33+
* @param beginDate 最大时间跨度7天
34+
* @param endDate endDate不能早于begingDate
35+
*/
36+
List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException;
37+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,11 @@ public interface WxMpService {
336336
* @return WxMpPayService
337337
*/
338338
WxMpPayService getPayService();
339+
340+
/**
341+
* 返回数据分析统计相关接口的方法实现类,以方便调用个其各种接口
342+
*
343+
* @return WxMpDataCubeService
344+
*/
345+
WxMpDataCubeService getDataCubeService();
339346
}

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import me.chanjar.weixin.common.exception.WxErrorException;
44
import me.chanjar.weixin.mp.bean.result.WxMpUser;
5-
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
65
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
7-
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
8-
9-
import java.util.Date;
10-
import java.util.List;
116

127
/**
138
* 用户管理和统计相关操作接口
@@ -47,26 +42,4 @@ public interface WxMpUserService {
4742
* @param next_openid 可选,第一个拉取的OPENID,null为从头开始拉取
4843
*/
4944
WxMpUserList userList(String next_openid) throws WxErrorException;
50-
51-
/**
52-
* <pre>
53-
* 获取用户增减数据
54-
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
55-
* </pre>
56-
*
57-
* @param beginDate 最大时间跨度7天
58-
* @param endDate endDate不能早于begingDate
59-
*/
60-
List<WxMpUserSummary> dataCubeUserSummary(Date beginDate, Date endDate) throws WxErrorException;
61-
62-
/**
63-
* <pre>
64-
* 获取累计用户数据
65-
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
66-
* </pre>
67-
*
68-
* @param beginDate 最大时间跨度7天
69-
* @param endDate endDate不能早于begingDate
70-
*/
71-
List<WxMpUserCumulate> dataCubeUserCumulate(Date beginDate, Date endDate) throws WxErrorException;
7245
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonParser;
8+
import com.google.gson.reflect.TypeToken;
9+
10+
import me.chanjar.weixin.common.exception.WxErrorException;
11+
import me.chanjar.weixin.mp.api.WxMpDataCubeService;
12+
import me.chanjar.weixin.mp.api.WxMpService;
13+
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
14+
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
15+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
16+
17+
/**
18+
* Created by Binary Wang on 2016/8/23.
19+
* @author binarywang (https://github.com/binarywang)
20+
*/
21+
public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
22+
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/datacube";
23+
private WxMpService wxMpService;
24+
25+
public WxMpDataCubeServiceImpl(WxMpService wxMpService) {
26+
this.wxMpService = wxMpService;
27+
}
28+
29+
@Override
30+
public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
31+
String url = API_URL_PREFIX + "/getusersummary";
32+
JsonObject param = new JsonObject();
33+
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
34+
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
35+
String responseContent = this.wxMpService.post(url, param.toString());
36+
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
37+
new TypeToken<List<WxMpUserSummary>>() {
38+
}.getType());
39+
}
40+
41+
@Override
42+
public List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
43+
String url = API_URL_PREFIX + "/getusercumulate";
44+
JsonObject param = new JsonObject();
45+
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
46+
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
47+
String responseContent = this.wxMpService.post(url, param.toString());
48+
return WxMpGsonBuilder.INSTANCE.create().fromJson(new JsonParser().parse(responseContent).getAsJsonObject().get("list"),
49+
new TypeToken<List<WxMpUserCumulate>>() {
50+
}.getType());
51+
}
52+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public class WxMpServiceImpl implements WxMpService {
6464

6565
private WxMpPayService payService = new WxMpPayServiceImpl(this);
6666

67+
private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this);
68+
6769
private CloseableHttpClient httpClient;
6870

6971
private HttpHost httpProxy;
@@ -527,4 +529,9 @@ public WxMpPayService getPayService() {
527529
return this.payService;
528530
}
529531

532+
@Override
533+
public WxMpDataCubeService getDataCubeService() {
534+
return this.dataCubeService;
535+
}
536+
530537
}
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import com.google.gson.JsonElement;
43
import com.google.gson.JsonObject;
5-
import com.google.gson.internal.Streams;
6-
import com.google.gson.reflect.TypeToken;
7-
import com.google.gson.stream.JsonReader;
4+
85
import me.chanjar.weixin.common.exception.WxErrorException;
96
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
107
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
118
import me.chanjar.weixin.mp.api.WxMpService;
129
import me.chanjar.weixin.mp.api.WxMpUserService;
1310
import me.chanjar.weixin.mp.bean.result.WxMpUser;
14-
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
1511
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
16-
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
17-
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
18-
19-
import java.io.StringReader;
20-
import java.util.Date;
21-
import java.util.List;
2212

2313
/**
2414
* Created by Binary Wang on 2016/7/21.
@@ -55,29 +45,4 @@ public WxMpUserList userList(String next_openid) throws WxErrorException {
5545
return WxMpUserList.fromJson(responseContent);
5646
}
5747

58-
@Override
59-
public List<WxMpUserSummary> dataCubeUserSummary(Date beginDate, Date endDate) throws WxErrorException {
60-
String url = "https://api.weixin.qq.com/datacube/getusersummary";
61-
JsonObject param = new JsonObject();
62-
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
63-
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
64-
String responseContent = this.wxMpService.post(url, param.toString());
65-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
66-
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"),
67-
new TypeToken<List<WxMpUserSummary>>() {
68-
}.getType());
69-
}
70-
71-
@Override
72-
public List<WxMpUserCumulate> dataCubeUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
73-
String url = "https://api.weixin.qq.com/datacube/getusercumulate";
74-
JsonObject param = new JsonObject();
75-
param.addProperty("begin_date", WxMpService.SIMPLE_DATE_FORMAT.format(beginDate));
76-
param.addProperty("end_date", WxMpService.SIMPLE_DATE_FORMAT.format(endDate));
77-
String responseContent = this.wxMpService.post(url, param.toString());
78-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
79-
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"),
80-
new TypeToken<List<WxMpUserCumulate>>() {
81-
}.getType());
82-
}
8348
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
import org.testng.Assert;
9+
import org.testng.annotations.Guice;
10+
import org.testng.annotations.Test;
11+
12+
import com.google.inject.Inject;
13+
14+
import me.chanjar.weixin.common.exception.WxErrorException;
15+
import me.chanjar.weixin.mp.api.ApiTestModule;
16+
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
17+
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
18+
19+
/**
20+
* 测试统计分析相关的接口
21+
* Created by Binary Wang on 2016/8/23.
22+
* @author binarywang (https://github.com/binarywang)
23+
*/
24+
@Guice(modules = ApiTestModule.class)
25+
public class WxMpDataCubeServiceImplTest {
26+
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
27+
28+
@Inject
29+
protected WxMpServiceImpl wxService;
30+
31+
@Test
32+
public void testGetUserSummary() throws WxErrorException, ParseException {
33+
Date beginDate = simpleDateFormat.parse("2016-08-20");
34+
Date endDate = simpleDateFormat.parse("2016-08-22");
35+
List<WxMpUserSummary> summaries = this.wxService.getDataCubeService()
36+
.getUserSummary(beginDate, endDate);
37+
Assert.assertNotNull(summaries);
38+
System.out.println(summaries);
39+
}
40+
41+
@Test
42+
public void testGetUserCumulate() throws WxErrorException, ParseException {
43+
Date beginDate = simpleDateFormat.parse("2016-08-21");
44+
Date endDate = simpleDateFormat.parse("2016-08-22");
45+
List<WxMpUserCumulate> cumulates = this.wxService.getDataCubeService()
46+
.getUserCumulate(beginDate, endDate);
47+
Assert.assertNotNull(cumulates);
48+
System.out.println(cumulates);
49+
}
50+
51+
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImplTest.java

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import org.testng.Assert;
4+
import org.testng.annotations.Guice;
5+
import org.testng.annotations.Test;
6+
37
import com.google.inject.Inject;
8+
49
import me.chanjar.weixin.common.exception.WxErrorException;
510
import me.chanjar.weixin.mp.api.ApiTestModule;
611
import me.chanjar.weixin.mp.bean.result.WxMpUser;
7-
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
812
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
9-
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
10-
import org.testng.Assert;
11-
import org.testng.annotations.Guice;
12-
import org.testng.annotations.Test;
13-
14-
import java.text.ParseException;
15-
import java.text.SimpleDateFormat;
16-
import java.util.Date;
17-
import java.util.List;
1813

1914
/**
2015
* 测试用户相关的接口
@@ -61,24 +56,4 @@ public void testGroupMoveUser() throws WxErrorException {
6156
this.wxService.getGroupService().userUpdateGroup(configStorage.getOpenId(), this.wxService.getGroupService().groupGet().get(3).getId());
6257
}
6358

64-
@Test
65-
public void testGetUserSummary() throws WxErrorException, ParseException {
66-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
67-
Date beginDate = simpleDateFormat.parse("2015-01-01");
68-
Date endDate = simpleDateFormat.parse("2015-01-02");
69-
List<WxMpUserSummary> summaries = this.wxService.getUserService().dataCubeUserSummary(beginDate, endDate);
70-
Assert.assertNotNull(summaries);
71-
System.out.println(summaries);
72-
}
73-
74-
@Test
75-
public void testGetUserCumulate() throws WxErrorException, ParseException {
76-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
77-
Date beginDate = simpleDateFormat.parse("2015-01-01");
78-
Date endDate = simpleDateFormat.parse("2015-01-02");
79-
List<WxMpUserCumulate> cumulates = this.wxService.getUserService().dataCubeUserCumulate(beginDate, endDate);
80-
Assert.assertNotNull(cumulates);
81-
System.out.println(cumulates);
82-
}
83-
8459
}

0 commit comments

Comments
 (0)