@@ -11,13 +11,16 @@ import me.chanjar.weixin.mp.bean.result.*; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.text.SimpleDateFormat; | |||||
import java.util.Date; | |||||
import java.util.List; | import java.util.List; | ||||
/** | /** | ||||
* 微信API的Service | * 微信API的Service | ||||
*/ | */ | ||||
public interface WxMpService { | public interface WxMpService { | ||||
public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 验证推送过来的消息的正确性 | * 验证推送过来的消息的正确性 | ||||
@@ -431,6 +434,30 @@ public interface WxMpService { | |||||
*/ | */ | ||||
String[] getCallbackIP() throws WxErrorException; | String[] getCallbackIP() throws WxErrorException; | ||||
/** | |||||
* <pre> | |||||
* 获取用户增减数据 | |||||
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html | |||||
* </pre> | |||||
* @param beginDate 最大时间跨度7天 | |||||
* @param endDate endDate不能早于begingDate | |||||
* @return | |||||
* @throws WxErrorException | |||||
*/ | |||||
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 | |||||
* @return | |||||
* @throws WxErrorException | |||||
*/ | |||||
List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException; | |||||
/** | /** | ||||
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求 | * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求 | ||||
* @param url | * @param url | ||||
@@ -44,6 +44,7 @@ import java.io.IOException; | |||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.io.StringReader; | import java.io.StringReader; | ||||
import java.security.NoSuchAlgorithmException; | import java.security.NoSuchAlgorithmException; | ||||
import java.util.Date; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.UUID; | import java.util.UUID; | ||||
@@ -465,6 +466,29 @@ public class WxMpServiceImpl implements WxMpService { | |||||
return ipArray; | return ipArray; | ||||
} | } | ||||
@Override | |||||
public List<WxMpUserSummary> 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<List<WxMpUserSummary>>(){}.getType()); | |||||
} | |||||
@Override | |||||
public List<WxMpUserCumulate> 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<List<WxMpUserCumulate>>(){}.getType()); | |||||
} | |||||
public String get(String url, String queryParam) throws WxErrorException { | public String get(String url, String queryParam) throws WxErrorException { | ||||
return execute(new SimpleGetRequestExecutor(), url, queryParam); | return execute(new SimpleGetRequestExecutor(), url, queryParam); | ||||
} | } | ||||
@@ -0,0 +1,41 @@ | |||||
package me.chanjar.weixin.mp.bean.result; | |||||
import java.io.Serializable; | |||||
import java.util.Date; | |||||
/** | |||||
* <pre> | |||||
* 累计用户数据接口的返回JSON数据包 | |||||
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html | |||||
* </pre> | |||||
*/ | |||||
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 + | |||||
'}'; | |||||
} | |||||
} |
@@ -0,0 +1,63 @@ | |||||
package me.chanjar.weixin.mp.bean.result; | |||||
import java.io.Serializable; | |||||
import java.util.Date; | |||||
/** | |||||
* <pre> | |||||
* 用户增减数据接口的返回JSON数据包 | |||||
* http://mp.weixin.qq.com/wiki/3/ecfed6e1a0a03b5f35e5efac98e864b7.html | |||||
* </pre> | |||||
*/ | |||||
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 + | |||||
'}'; | |||||
} | |||||
} |
@@ -25,6 +25,8 @@ public class WxMpGsonBuilder { | |||||
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter()); | INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMpOAuth2AccessToken.class, new WxMpOAuth2AccessTokenAdapter()); | INSTANCE.registerTypeAdapter(WxMpOAuth2AccessToken.class, new WxMpOAuth2AccessTokenAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMpUserSummary.class, new WxMpUserSummaryGsonAdapter()); | |||||
INSTANCE.registerTypeAdapter(WxMpUserCumulate.class, new WxMpUserCumulateGsonAdapter()); | |||||
} | } | ||||
public static Gson create() { | public static Gson create() { | ||||
@@ -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<WxMpUserCumulate> { | |||||
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; | |||||
} | |||||
} |
@@ -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<WxMpUserSummary> { | |||||
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; | |||||
} | |||||
} |
@@ -4,12 +4,15 @@ import com.google.inject.Inject; | |||||
import me.chanjar.weixin.common.api.WxConsts; | import me.chanjar.weixin.common.api.WxConsts; | ||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||
import me.chanjar.weixin.common.exception.WxErrorException; | 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.WxMpMassGroupMessage; | ||||
import me.chanjar.weixin.mp.bean.WxMpMassNews; | import me.chanjar.weixin.mp.bean.WxMpMassNews; | ||||
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; | import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; | ||||
import me.chanjar.weixin.mp.bean.WxMpMassVideo; | import me.chanjar.weixin.mp.bean.WxMpMassVideo; | ||||
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; | import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; | ||||
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; | 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.Assert; | ||||
import org.testng.annotations.DataProvider; | import org.testng.annotations.DataProvider; | ||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
@@ -17,7 +20,11 @@ import org.testng.annotations.Test; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.text.ParseException; | |||||
import java.text.SimpleDateFormat; | |||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Date; | |||||
import java.util.List; | |||||
/** | /** | ||||
* | * | ||||
@@ -32,11 +39,31 @@ public class WxMpMiscAPITest { | |||||
protected WxMpServiceImpl wxService; | protected WxMpServiceImpl wxService; | ||||
@Test | @Test | ||||
public void getCallbackIP() throws WxErrorException { | |||||
public void testGetCallbackIP() throws WxErrorException { | |||||
String[] ipArray = wxService.getCallbackIP(); | String[] ipArray = wxService.getCallbackIP(); | ||||
System.out.println(Arrays.toString(ipArray)); | System.out.println(Arrays.toString(ipArray)); | ||||
Assert.assertNotNull(ipArray); | Assert.assertNotNull(ipArray); | ||||
Assert.assertNotEquals(ipArray.length, 0); | 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<WxMpUserSummary> 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<WxMpUserCumulate> cumulates = wxService.getUserCumulate(beginDate, endDate); | |||||
System.out.println(cumulates); | |||||
Assert.assertNotNull(cumulates); | |||||
} | |||||
} | } |