Skip to content

Commit 978c51b

Browse files
committed
issue #100 用户分析数据接口
1 parent fb10acf commit 978c51b

File tree

8 files changed

+280
-2
lines changed

8 files changed

+280
-2
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import java.io.File;
1212
import java.io.IOException;
1313
import java.io.InputStream;
14+
import java.text.SimpleDateFormat;
15+
import java.util.Date;
1416
import java.util.List;
1517

1618
/**
1719
* 微信API的Service
1820
*/
1921
public interface WxMpService {
20-
22+
23+
public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
2124
/**
2225
* <pre>
2326
* 验证推送过来的消息的正确性
@@ -431,6 +434,30 @@ public interface WxMpService {
431434
*/
432435
String[] getCallbackIP() throws WxErrorException;
433436

437+
/**
438+
* <pre>
439+
* 获取用户增减数据
440+
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
441+
* </pre>
442+
* @param beginDate 最大时间跨度7天
443+
* @param endDate endDate不能早于begingDate
444+
* @return
445+
* @throws WxErrorException
446+
*/
447+
List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException;
448+
449+
/**
450+
* <pre>
451+
* 获取累计用户数据
452+
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
453+
* </pre>
454+
* @param beginDate 最大时间跨度7天
455+
* @param endDate endDate不能早于begingDate
456+
* @return
457+
* @throws WxErrorException
458+
*/
459+
List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException;
460+
434461
/**
435462
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
436463
* @param url

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.io.InputStream;
4545
import java.io.StringReader;
4646
import java.security.NoSuchAlgorithmException;
47+
import java.util.Date;
4748
import java.util.List;
4849
import java.util.UUID;
4950

@@ -465,6 +466,29 @@ public String[] getCallbackIP() throws WxErrorException {
465466
return ipArray;
466467
}
467468

469+
470+
@Override
471+
public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
472+
String url = "https://api.weixin.qq.com/datacube/getusersummary";
473+
JsonObject param = new JsonObject();
474+
param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
475+
param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
476+
String responseContent = post(url, param.toString());
477+
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
478+
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), new TypeToken<List<WxMpUserSummary>>(){}.getType());
479+
}
480+
481+
@Override
482+
public List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
483+
String url = "https://api.weixin.qq.com/datacube/getusercumulate";
484+
JsonObject param = new JsonObject();
485+
param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
486+
param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
487+
String responseContent = post(url, param.toString());
488+
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
489+
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), new TypeToken<List<WxMpUserCumulate>>(){}.getType());
490+
}
491+
468492
public String get(String url, String queryParam) throws WxErrorException {
469493
return execute(new SimpleGetRequestExecutor(), url, queryParam);
470494
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package me.chanjar.weixin.mp.bean.result;
2+
3+
import java.io.Serializable;
4+
import java.util.Date;
5+
6+
/**
7+
* <pre>
8+
* 累计用户数据接口的返回JSON数据包
9+
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
10+
* </pre>
11+
*/
12+
public class WxMpUserCumulate implements Serializable {
13+
14+
private Date refDate;
15+
16+
private Integer cumulateUser;
17+
18+
public Date getRefDate() {
19+
return refDate;
20+
}
21+
22+
public void setRefDate(Date refDate) {
23+
this.refDate = refDate;
24+
}
25+
26+
public Integer getCumulateUser() {
27+
return cumulateUser;
28+
}
29+
30+
public void setCumulateUser(Integer cumulateUser) {
31+
this.cumulateUser = cumulateUser;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "WxMpUserCumulate{" +
37+
"refDate=" + refDate +
38+
", cumulateUser=" + cumulateUser +
39+
'}';
40+
}
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package me.chanjar.weixin.mp.bean.result;
2+
3+
import java.io.Serializable;
4+
import java.util.Date;
5+
6+
/**
7+
* <pre>
8+
* 用户增减数据接口的返回JSON数据包
9+
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
10+
* </pre>
11+
*/
12+
public class WxMpUserSummary implements Serializable {
13+
14+
private Date refDate;
15+
16+
private Integer userSource;
17+
18+
private Integer newUser;
19+
20+
private Integer cancelUser;
21+
22+
public Date getRefDate() {
23+
return refDate;
24+
}
25+
26+
public void setRefDate(Date refDate) {
27+
this.refDate = refDate;
28+
}
29+
30+
public Integer getUserSource() {
31+
return userSource;
32+
}
33+
34+
public void setUserSource(Integer userSource) {
35+
this.userSource = userSource;
36+
}
37+
38+
public Integer getNewUser() {
39+
return newUser;
40+
}
41+
42+
public void setNewUser(Integer newUser) {
43+
this.newUser = newUser;
44+
}
45+
46+
public Integer getCancelUser() {
47+
return cancelUser;
48+
}
49+
50+
public void setCancelUser(Integer cancelUser) {
51+
this.cancelUser = cancelUser;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "WxMpUserSummary{" +
57+
"refDate=" + refDate +
58+
", userSource=" + userSource +
59+
", newUser=" + newUser +
60+
", cancelUser=" + cancelUser +
61+
'}';
62+
}
63+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class WxMpGsonBuilder {
2525
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter());
2626
INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter());
2727
INSTANCE.registerTypeAdapter(WxMpOAuth2AccessToken.class, new WxMpOAuth2AccessTokenAdapter());
28+
INSTANCE.registerTypeAdapter(WxMpUserSummary.class, new WxMpUserSummaryGsonAdapter());
29+
INSTANCE.registerTypeAdapter(WxMpUserCumulate.class, new WxMpUserCumulateGsonAdapter());
2830
}
2931

3032
public static Gson create() {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
3+
*
4+
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
5+
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
6+
* arose from modification of the original source, or other redistribution of this source
7+
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
8+
*/
9+
package me.chanjar.weixin.mp.util.json;
10+
11+
import com.google.gson.*;
12+
import me.chanjar.weixin.common.util.json.GsonHelper;
13+
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
14+
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
15+
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
16+
17+
import java.lang.reflect.Type;
18+
import java.text.ParseException;
19+
import java.text.SimpleDateFormat;
20+
21+
/**
22+
*
23+
* @author Daniel Qian
24+
*
25+
*/
26+
public class WxMpUserCumulateGsonAdapter implements JsonDeserializer<WxMpUserCumulate> {
27+
28+
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
29+
30+
public WxMpUserCumulate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
31+
WxMpUserCumulate cumulate = new WxMpUserCumulate();
32+
JsonObject summaryJsonObject = json.getAsJsonObject();
33+
34+
try {
35+
String refDate = GsonHelper.getString(summaryJsonObject, "ref_date");
36+
if (refDate != null) {
37+
cumulate.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate));
38+
}
39+
cumulate.setCumulateUser(GsonHelper.getInteger(summaryJsonObject, "cumulate_user"));
40+
} catch (ParseException e) {
41+
throw new JsonParseException(e);
42+
}
43+
return cumulate;
44+
45+
}
46+
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
3+
*
4+
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
5+
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
6+
* arose from modification of the original source, or other redistribution of this source
7+
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
8+
*/
9+
package me.chanjar.weixin.mp.util.json;
10+
11+
import com.google.gson.*;
12+
import me.chanjar.weixin.common.util.json.GsonHelper;
13+
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
14+
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
15+
16+
import java.lang.reflect.Type;
17+
import java.text.ParseException;
18+
import java.text.SimpleDateFormat;
19+
20+
/**
21+
* @author Daniel Qian
22+
*/
23+
public class WxMpUserSummaryGsonAdapter implements JsonDeserializer<WxMpUserSummary> {
24+
25+
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
26+
27+
public WxMpUserSummary deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
28+
throws JsonParseException {
29+
WxMpUserSummary summary = new WxMpUserSummary();
30+
JsonObject summaryJsonObject = json.getAsJsonObject();
31+
32+
try {
33+
String refDate = GsonHelper.getString(summaryJsonObject, "ref_date");
34+
if (refDate != null) {
35+
summary.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate));
36+
}
37+
summary.setUserSource(GsonHelper.getInteger(summaryJsonObject, "user_source"));
38+
summary.setNewUser(GsonHelper.getInteger(summaryJsonObject, "new_user"));
39+
summary.setCancelUser(GsonHelper.getInteger(summaryJsonObject, "cancel_user"));
40+
} catch (ParseException e) {
41+
throw new JsonParseException(e);
42+
}
43+
44+
return summary;
45+
}
46+
47+
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMiscAPITest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@
44
import me.chanjar.weixin.common.api.WxConsts;
55
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
66
import me.chanjar.weixin.common.exception.WxErrorException;
7+
import me.chanjar.weixin.common.session.WxSession;
78
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
89
import me.chanjar.weixin.mp.bean.WxMpMassNews;
910
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
1011
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
1112
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
1213
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
14+
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate;
15+
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary;
1316
import org.testng.Assert;
1417
import org.testng.annotations.DataProvider;
1518
import org.testng.annotations.Guice;
1619
import org.testng.annotations.Test;
1720

1821
import java.io.IOException;
1922
import java.io.InputStream;
23+
import java.text.ParseException;
24+
import java.text.SimpleDateFormat;
2025
import java.util.Arrays;
26+
import java.util.Date;
27+
import java.util.List;
2128

2229
/**
2330
*
@@ -32,11 +39,31 @@ public class WxMpMiscAPITest {
3239
protected WxMpServiceImpl wxService;
3340

3441
@Test
35-
public void getCallbackIP() throws WxErrorException {
42+
public void testGetCallbackIP() throws WxErrorException {
3643
String[] ipArray = wxService.getCallbackIP();
3744
System.out.println(Arrays.toString(ipArray));
3845
Assert.assertNotNull(ipArray);
3946
Assert.assertNotEquals(ipArray.length, 0);
4047
}
4148

49+
@Test
50+
public void testGetUserSummary() throws WxErrorException, ParseException {
51+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
52+
Date beginDate = simpleDateFormat.parse("2015-01-01");
53+
Date endDate = simpleDateFormat.parse("2015-01-02");
54+
List<WxMpUserSummary> summaries = wxService.getUserSummary(beginDate, endDate);
55+
System.out.println(summaries);
56+
Assert.assertNotNull(summaries);
57+
}
58+
59+
@Test
60+
public void testGetUserCumulate() throws WxErrorException, ParseException {
61+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
62+
Date beginDate = simpleDateFormat.parse("2015-01-01");
63+
Date endDate = simpleDateFormat.parse("2015-01-02");
64+
List<WxMpUserCumulate> cumulates = wxService.getUserCumulate(beginDate, endDate);
65+
System.out.println(cumulates);
66+
Assert.assertNotNull(cumulates);
67+
}
68+
4269
}

0 commit comments

Comments
 (0)