Skip to content

Commit

Permalink
🆕 #1723 企业微信增加查询应用消息发送统计的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Sep 13, 2020
1 parent 91d484f commit 37a009c
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.chanjar.weixin.cp.bean.message.WxCpLinkedCorpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendStatistics;

/**
* 消息推送接口.
Expand All @@ -24,6 +25,21 @@ public interface WxCpMessageService {
*/
WxCpMessageSendResult send(WxCpMessage message) throws WxErrorException;

/**
* <pre>
* 查询应用消息发送统计
* 请求方式:POST(HTTPS)
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/message/get_statistics?access_token=ACCESS_TOKEN
*
* 详情请见: https://work.weixin.qq.com/api/doc/90000/90135/92369
* </pre>
*
* @param timeType 查询哪天的数据,0:当天;1:昨天。默认为0。
* @return 统计结果
* @throws WxErrorException the wx error exception
*/
WxCpMessageSendStatistics getStatistics(int timeType) throws WxErrorException;

/**
* <pre>
* 互联企业的应用支持推送文本、图片、视频、文件、图文等类型。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package me.chanjar.weixin.cp.api.impl;

import com.google.common.collect.ImmutableMap;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMessageService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpLinkedCorpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendStatistics;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Message;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

/**
* 消息推送接口实现类.
Expand All @@ -27,7 +30,13 @@ public WxCpMessageSendResult send(WxCpMessage message) throws WxErrorException {
}

return WxCpMessageSendResult.fromJson(this.cpService.post(this.cpService.getWxCpConfigStorage()
.getApiUrl(WxCpApiPathConsts.Message.MESSAGE_SEND), message.toJson()));
.getApiUrl(Message.MESSAGE_SEND), message.toJson()));
}

@Override
public WxCpMessageSendStatistics getStatistics(int timeType) throws WxErrorException {
return WxCpMessageSendStatistics.fromJson(this.cpService.post(this.cpService.getWxCpConfigStorage().getApiUrl(Message.GET_STATISTICS),
WxCpGsonBuilder.create().toJson(ImmutableMap.of("time_type", timeType))));
}

@Override
Expand All @@ -38,6 +47,6 @@ public WxCpMessageSendResult sendLinkedCorpMessage(WxCpLinkedCorpMessage message
}

return WxCpMessageSendResult.fromJson(this.cpService.post(this.cpService.getWxCpConfigStorage()
.getApiUrl(WxCpApiPathConsts.Message.LINKEDCORP_MESSAGE_SEND), message.toJson()));
.getApiUrl(Message.LINKEDCORP_MESSAGE_SEND), message.toJson()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.chanjar.weixin.cp.bean.message;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.util.List;

/**
* 应用消息发送统计信息.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-09-13
*/
@Data
public class WxCpMessageSendStatistics {
public static WxCpMessageSendStatistics fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpMessageSendStatistics.class);
}

private List<StatisticItem> statistics;

@Data
public static class StatisticItem {
/**
* 应用名
*/
@SerializedName("app_name")
private String appName;

/**
* 应用id
*/
@SerializedName("agentid")
private Integer agentId;

/**
* 发消息成功人次
*/
@SerializedName("count")
private Integer count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public static class Message {
*/
public static final String MESSAGE_SEND = "/cgi-bin/message/send";

/**
* 查询应用消息发送统计
*/
public static final String GET_STATISTICS = "/cgi-bin/message/get_statistics";

/**
* 互联企业发送应用消息
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import me.chanjar.weixin.cp.bean.message.WxCpLinkedCorpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendStatistics;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Guice;
Expand All @@ -20,6 +21,7 @@
import static com.github.dreamhead.moco.Moco.file;
import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer;
import static me.chanjar.weixin.cp.api.ApiTestModuleWithMockServer.mockServerPort;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertNotNull;

/**
Expand All @@ -29,8 +31,8 @@
* @date 2020-08-30
*/
@Test
@Guice(modules = ApiTestModuleWithMockServer.class)
//@Guice(modules = ApiTestModule.class)
//@Guice(modules = ApiTestModuleWithMockServer.class)
@Guice(modules = ApiTestModule.class)
public class WxCpMessageServiceImplTest {
@Inject
protected WxCpService wxService;
Expand Down Expand Up @@ -154,11 +156,24 @@ public void testSendMessage_miniProgram_notice() throws WxErrorException {
}

@Test
public void testLinkedCorpMessageSend() throws WxErrorException {
public void testSendLinkedCorpMessage() throws WxErrorException {
this.wxService.getMessageService().sendLinkedCorpMessage(WxCpLinkedCorpMessage.builder()
.msgType(WxConsts.KefuMsgType.TEXT)
.toUsers(new String[]{configStorage.getUserId()})
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
.build());
}

@Test
public void testSend() {
// see other test methods
}

@Test
public void testGetStatistics() throws WxErrorException {
final WxCpMessageSendStatistics statistics = this.wxService.getMessageService().getStatistics(1);
assertNotNull(statistics);
assertThat(statistics.getStatistics()).isNotNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* </pre>
*
* @author cloudX
* @date 2020/08/17
* @date 2020 /08/17
*/
public interface EcommerceService {
/**
Expand Down Expand Up @@ -63,8 +63,9 @@ public interface EcommerceService {
* </pre>
*
* @param tradeType 支付方式
* @param request 请求对象
* @return 微信合单支付返回
* @param request 请求对象
* @return 微信合单支付返回 transactions result
* @throws WxPayException the wx pay exception
*/
TransactionsResult combine(TradeTypeEnum tradeType, CombineTransactionsRequest request) throws WxPayException;

Expand All @@ -75,9 +76,11 @@ public interface EcommerceService {
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/e-combine.shtml
* </pre>
*
* @param <T> the type parameter
* @param tradeType 支付方式
* @param request 请求对象
* @return 调起支付需要的参数
* @param request 请求对象
* @return 调起支付需要的参数 t
* @throws WxPayException the wx pay exception
*/
<T> T combineTransactions(TradeTypeEnum tradeType, CombineTransactionsRequest request) throws WxPayException;

Expand All @@ -88,32 +91,38 @@ public interface EcommerceService {
* </pre>
*
* @param notifyData 通知数据
* @param header 通知头部数据,不传则表示不校验头
* @return 解密后通知数据
* @param header 通知头部数据,不传则表示不校验头
* @return 解密后通知数据 combine transactions notify result
* @throws WxPayException the wx pay exception
*/
CombineTransactionsNotifyResult parseCombineNotifyResult(String notifyData, SignatureHeader header) throws WxPayException;

/**
* <pre>
* <pre>
* 服务商模式普通支付API(APP支付、JSAPI支付、H5支付、NATIVE支付).
* 请求URL:https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/transactions_sl.shtml
* </pre>
*
* @param tradeType 支付方式
* @param request 请求对象
* @return 调起支付需要的参数
* @param request 请求对象
* @return 调起支付需要的参数 transactions result
* @throws WxPayException the wx pay exception
*/
TransactionsResult partner(TradeTypeEnum tradeType, PartnerTransactionsRequest request) throws WxPayException;

/**
* <pre>
* <pre>
* 服务商模式普通支付API(APP支付、JSAPI支付、H5支付、NATIVE支付).
* 请求URL:https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/transactions_sl.shtml
* </pre>
*
* @param <T> the type parameter
* @param tradeType 支付方式
* @param request 请求对象
* @return 调起支付需要的参数
* @param request 请求对象
* @return 调起支付需要的参数 t
* @throws WxPayException the wx pay exception
*/
<T> T partnerTransactions(TradeTypeEnum tradeType, PartnerTransactionsRequest request) throws WxPayException;

Expand All @@ -124,8 +133,9 @@ public interface EcommerceService {
* </pre>
*
* @param notifyData 通知数据
* @param header 通知头部数据,不传则表示不校验头
* @return 解密后通知数据
* @param header 通知头部数据,不传则表示不校验头
* @return 解密后通知数据 partner transactions notify result
* @throws WxPayException the wx pay exception
*/
PartnerTransactionsNotifyResult parsePartnerNotifyResult(String notifyData, SignatureHeader header) throws WxPayException;

Expand All @@ -136,7 +146,8 @@ public interface EcommerceService {
* </pre>
*
* @param accountType 服务商账户类型
* @return 返回数据
* @return 返回数据 fund balance result
* @throws WxPayException the wx pay exception
*/
FundBalanceResult spNowBalance(SpAccountTypeEnum accountType) throws WxPayException;

Expand All @@ -147,8 +158,9 @@ public interface EcommerceService {
* </pre>
*
* @param accountType 服务商账户类型
* @param date 查询日期 2020-09-11
* @return 返回数据
* @param date 查询日期 2020-09-11
* @return 返回数据 fund balance result
* @throws WxPayException the wx pay exception
*/
FundBalanceResult spDayEndBalance(SpAccountTypeEnum accountType, String date) throws WxPayException;

Expand All @@ -159,7 +171,8 @@ public interface EcommerceService {
* </pre>
*
* @param subMchid 二级商户号
* @return 返回数据
* @return 返回数据 fund balance result
* @throws WxPayException the wx pay exception
*/
FundBalanceResult subNowBalance(String subMchid) throws WxPayException;

Expand All @@ -170,8 +183,9 @@ public interface EcommerceService {
* </pre>
*
* @param subMchid 二级商户号
* @param date 查询日期 2020-09-11
* @return 返回数据
* @param date 查询日期 2020-09-11
* @return 返回数据 fund balance result
* @throws WxPayException the wx pay exception
*/
FundBalanceResult subDayEndBalance(String subMchid, String date) throws WxPayException;

Expand All @@ -182,7 +196,8 @@ public interface EcommerceService {
* </pre>
*
* @param request 分账请求
* @return 返回数据
* @return 返回数据 profit sharing result
* @throws WxPayException the wx pay exception
*/
ProfitSharingResult profitSharing(ProfitSharingRequest request) throws WxPayException;

Expand Down

0 comments on commit 37a009c

Please sign in to comment.