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 862f854b..68a19bbe 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 @@ -11,13 +11,16 @@ import me.chanjar.weixin.mp.bean.result.*; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; /** * 微信API的Service */ public interface WxMpService { - + + public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); /** *
    * 验证推送过来的消息的正确性
@@ -431,6 +434,30 @@ public interface WxMpService {
    */
   String[] getCallbackIP() throws WxErrorException;
 
+  /**
+   * 
+   * 获取用户增减数据
+   * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
+   * 
+ * @param beginDate 最大时间跨度7天 + * @param endDate endDate不能早于begingDate + * @return + * @throws WxErrorException + */ + List getUserSummary(Date beginDate, Date endDate) throws WxErrorException; + + /** + *
+   * 获取累计用户数据
+   * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
+   * 
+ * @param beginDate 最大时间跨度7天 + * @param endDate endDate不能早于begingDate + * @return + * @throws WxErrorException + */ + List getUserCumulate(Date beginDate, Date endDate) throws WxErrorException; + /** * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求 * @param url diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java index 74268b9e..059a09f3 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java @@ -44,6 +44,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.security.NoSuchAlgorithmException; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -465,6 +466,29 @@ public class WxMpServiceImpl implements WxMpService { return ipArray; } + + @Override + public List getUserSummary(Date beginDate, Date endDate) throws WxErrorException { + String url = "https://api.weixin.qq.com/datacube/getusersummary"; + JsonObject param = new JsonObject(); + param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate)); + param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate)); + String responseContent = 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 getUserCumulate(Date beginDate, Date endDate) throws WxErrorException { + String url = "https://api.weixin.qq.com/datacube/getusercumulate"; + JsonObject param = new JsonObject(); + param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate)); + param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate)); + String responseContent = 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()); + } + public String get(String url, String queryParam) throws WxErrorException { return execute(new SimpleGetRequestExecutor(), url, queryParam); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpUserCumulate.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpUserCumulate.java new file mode 100644 index 00000000..4c295c41 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpUserCumulate.java @@ -0,0 +1,41 @@ +package me.chanjar.weixin.mp.bean.result; + +import java.io.Serializable; +import java.util.Date; + +/** + *
+ * 累计用户数据接口的返回JSON数据包
+ * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
+ * 
+ */ +public class WxMpUserCumulate implements Serializable { + + private Date refDate; + + private Integer cumulateUser; + + public Date getRefDate() { + return refDate; + } + + public void setRefDate(Date refDate) { + this.refDate = refDate; + } + + public Integer getCumulateUser() { + return cumulateUser; + } + + public void setCumulateUser(Integer cumulateUser) { + this.cumulateUser = cumulateUser; + } + + @Override + public String toString() { + return "WxMpUserCumulate{" + + "refDate=" + refDate + + ", cumulateUser=" + cumulateUser + + '}'; + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpUserSummary.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpUserSummary.java new file mode 100644 index 00000000..de61a80b --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpUserSummary.java @@ -0,0 +1,63 @@ +package me.chanjar.weixin.mp.bean.result; + +import java.io.Serializable; +import java.util.Date; + +/** + *
+ * 用户增减数据接口的返回JSON数据包
+ * http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html
+ * 
+ */ +public class WxMpUserSummary implements Serializable { + + private Date refDate; + + private Integer userSource; + + private Integer newUser; + + private Integer cancelUser; + + public Date getRefDate() { + return refDate; + } + + public void setRefDate(Date refDate) { + this.refDate = refDate; + } + + public Integer getUserSource() { + return userSource; + } + + public void setUserSource(Integer userSource) { + this.userSource = userSource; + } + + public Integer getNewUser() { + return newUser; + } + + public void setNewUser(Integer newUser) { + this.newUser = newUser; + } + + public Integer getCancelUser() { + return cancelUser; + } + + public void setCancelUser(Integer cancelUser) { + this.cancelUser = cancelUser; + } + + @Override + public String toString() { + return "WxMpUserSummary{" + + "refDate=" + refDate + + ", userSource=" + userSource + + ", newUser=" + newUser + + ", cancelUser=" + cancelUser + + '}'; + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java index 132c0415..3516cb9a 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java @@ -25,6 +25,8 @@ public class WxMpGsonBuilder { INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter()); INSTANCE.registerTypeAdapter(WxMpOAuth2AccessToken.class, new WxMpOAuth2AccessTokenAdapter()); + INSTANCE.registerTypeAdapter(WxMpUserSummary.class, new WxMpUserSummaryGsonAdapter()); + INSTANCE.registerTypeAdapter(WxMpUserCumulate.class, new WxMpUserCumulateGsonAdapter()); } public static Gson create() { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserCumulateGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserCumulateGsonAdapter.java new file mode 100644 index 00000000..ab4449a6 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserCumulateGsonAdapter.java @@ -0,0 +1,47 @@ +/* + * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved. + * + * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended + * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction + * arose from modification of the original source, or other redistribution of this source + * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. + */ +package me.chanjar.weixin.mp.util.json; + +import com.google.gson.*; +import me.chanjar.weixin.common.util.json.GsonHelper; +import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; +import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; +import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; + +import java.lang.reflect.Type; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +/** + * + * @author Daniel Qian + * + */ +public class WxMpUserCumulateGsonAdapter implements JsonDeserializer { + + private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + + public WxMpUserCumulate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + WxMpUserCumulate cumulate = new WxMpUserCumulate(); + JsonObject summaryJsonObject = json.getAsJsonObject(); + + try { + String refDate = GsonHelper.getString(summaryJsonObject, "ref_date"); + if (refDate != null) { + cumulate.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate)); + } + cumulate.setCumulateUser(GsonHelper.getInteger(summaryJsonObject, "cumulate_user")); + } catch (ParseException e) { + throw new JsonParseException(e); + } + return cumulate; + + } + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserSummaryGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserSummaryGsonAdapter.java new file mode 100644 index 00000000..9db88884 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpUserSummaryGsonAdapter.java @@ -0,0 +1,47 @@ +/* + * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved. + * + * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended + * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction + * arose from modification of the original source, or other redistribution of this source + * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. + */ +package me.chanjar.weixin.mp.util.json; + +import com.google.gson.*; +import me.chanjar.weixin.common.util.json.GsonHelper; +import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; +import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; + +import java.lang.reflect.Type; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +/** + * @author Daniel Qian + */ +public class WxMpUserSummaryGsonAdapter implements JsonDeserializer { + + private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + + public WxMpUserSummary deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + WxMpUserSummary summary = new WxMpUserSummary(); + JsonObject summaryJsonObject = json.getAsJsonObject(); + + try { + String refDate = GsonHelper.getString(summaryJsonObject, "ref_date"); + if (refDate != null) { + summary.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate)); + } + summary.setUserSource(GsonHelper.getInteger(summaryJsonObject, "user_source")); + summary.setNewUser(GsonHelper.getInteger(summaryJsonObject, "new_user")); + summary.setCancelUser(GsonHelper.getInteger(summaryJsonObject, "cancel_user")); + } catch (ParseException e) { + throw new JsonParseException(e); + } + + return summary; + } + +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMiscAPITest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMiscAPITest.java index 0b160306..5f2a7392 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMiscAPITest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMiscAPITest.java @@ -4,12 +4,15 @@ import com.google.inject.Inject; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.session.WxSession; import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage; import me.chanjar.weixin.mp.bean.WxMpMassNews; import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; import me.chanjar.weixin.mp.bean.WxMpMassVideo; import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; +import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; +import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Guice; @@ -17,7 +20,11 @@ import org.testng.annotations.Test; import java.io.IOException; import java.io.InputStream; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Arrays; +import java.util.Date; +import java.util.List; /** * @@ -32,11 +39,31 @@ public class WxMpMiscAPITest { protected WxMpServiceImpl wxService; @Test - public void getCallbackIP() throws WxErrorException { + public void testGetCallbackIP() throws WxErrorException { String[] ipArray = wxService.getCallbackIP(); System.out.println(Arrays.toString(ipArray)); Assert.assertNotNull(ipArray); Assert.assertNotEquals(ipArray.length, 0); } + @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 = wxService.getUserSummary(beginDate, endDate); + System.out.println(summaries); + Assert.assertNotNull(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 = wxService.getUserCumulate(beginDate, endDate); + System.out.println(cumulates); + Assert.assertNotNull(cumulates); + } + }