Skip to content

Commit 37a009c

Browse files
committed
🆕 binarywang#1723 企业微信增加查询应用消息发送统计的接口
1 parent 91d484f commit 37a009c

File tree

6 files changed

+131
-28
lines changed

6 files changed

+131
-28
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMessageService.java

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import me.chanjar.weixin.cp.bean.message.WxCpLinkedCorpMessage;
55
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
66
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
7+
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendStatistics;
78

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

28+
/**
29+
* <pre>
30+
* 查询应用消息发送统计
31+
* 请求方式:POST(HTTPS)
32+
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/message/get_statistics?access_token=ACCESS_TOKEN
33+
*
34+
* 详情请见: https://work.weixin.qq.com/api/doc/90000/90135/92369
35+
* </pre>
36+
*
37+
* @param timeType 查询哪天的数据,0:当天;1:昨天。默认为0。
38+
* @return 统计结果
39+
* @throws WxErrorException the wx error exception
40+
*/
41+
WxCpMessageSendStatistics getStatistics(int timeType) throws WxErrorException;
42+
2743
/**
2844
* <pre>
2945
* 互联企业的应用支持推送文本、图片、视频、文件、图文等类型。

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMessageServiceImpl.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import com.google.common.collect.ImmutableMap;
34
import lombok.RequiredArgsConstructor;
45
import me.chanjar.weixin.common.error.WxErrorException;
56
import me.chanjar.weixin.cp.api.WxCpMessageService;
67
import me.chanjar.weixin.cp.api.WxCpService;
78
import me.chanjar.weixin.cp.bean.message.WxCpLinkedCorpMessage;
89
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
910
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
10-
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
11+
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendStatistics;
12+
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Message;
13+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
1114

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

2932
return WxCpMessageSendResult.fromJson(this.cpService.post(this.cpService.getWxCpConfigStorage()
30-
.getApiUrl(WxCpApiPathConsts.Message.MESSAGE_SEND), message.toJson()));
33+
.getApiUrl(Message.MESSAGE_SEND), message.toJson()));
34+
}
35+
36+
@Override
37+
public WxCpMessageSendStatistics getStatistics(int timeType) throws WxErrorException {
38+
return WxCpMessageSendStatistics.fromJson(this.cpService.post(this.cpService.getWxCpConfigStorage().getApiUrl(Message.GET_STATISTICS),
39+
WxCpGsonBuilder.create().toJson(ImmutableMap.of("time_type", timeType))));
3140
}
3241

3342
@Override
@@ -38,6 +47,6 @@ public WxCpMessageSendResult sendLinkedCorpMessage(WxCpLinkedCorpMessage message
3847
}
3948

4049
return WxCpMessageSendResult.fromJson(this.cpService.post(this.cpService.getWxCpConfigStorage()
41-
.getApiUrl(WxCpApiPathConsts.Message.LINKEDCORP_MESSAGE_SEND), message.toJson()));
50+
.getApiUrl(Message.LINKEDCORP_MESSAGE_SEND), message.toJson()));
4251
}
4352
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.chanjar.weixin.cp.bean.message;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
6+
7+
import java.util.List;
8+
9+
/**
10+
* 应用消息发送统计信息.
11+
*
12+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
13+
* @date 2020-09-13
14+
*/
15+
@Data
16+
public class WxCpMessageSendStatistics {
17+
public static WxCpMessageSendStatistics fromJson(String json) {
18+
return WxCpGsonBuilder.create().fromJson(json, WxCpMessageSendStatistics.class);
19+
}
20+
21+
private List<StatisticItem> statistics;
22+
23+
@Data
24+
public static class StatisticItem {
25+
/**
26+
* 应用名
27+
*/
28+
@SerializedName("app_name")
29+
private String appName;
30+
31+
/**
32+
* 应用id
33+
*/
34+
@SerializedName("agentid")
35+
private Integer agentId;
36+
37+
/**
38+
* 发消息成功人次
39+
*/
40+
@SerializedName("count")
41+
private Integer count;
42+
}
43+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static class Message {
3636
*/
3737
public static final String MESSAGE_SEND = "/cgi-bin/message/send";
3838

39+
/**
40+
* 查询应用消息发送统计
41+
*/
42+
public static final String GET_STATISTICS = "/cgi-bin/message/get_statistics";
43+
3944
/**
4045
* 互联企业发送应用消息
4146
*/

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMessageServiceImplTest.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import me.chanjar.weixin.cp.bean.message.WxCpLinkedCorpMessage;
1313
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
1414
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
15+
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendStatistics;
1516
import org.testng.annotations.AfterTest;
1617
import org.testng.annotations.BeforeTest;
1718
import org.testng.annotations.Guice;
@@ -20,6 +21,7 @@
2021
import static com.github.dreamhead.moco.Moco.file;
2122
import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer;
2223
import static me.chanjar.weixin.cp.api.ApiTestModuleWithMockServer.mockServerPort;
24+
import static org.assertj.core.api.Assertions.assertThat;
2325
import static org.testng.Assert.assertNotNull;
2426

2527
/**
@@ -29,8 +31,8 @@
2931
* @date 2020-08-30
3032
*/
3133
@Test
32-
@Guice(modules = ApiTestModuleWithMockServer.class)
33-
//@Guice(modules = ApiTestModule.class)
34+
//@Guice(modules = ApiTestModuleWithMockServer.class)
35+
@Guice(modules = ApiTestModule.class)
3436
public class WxCpMessageServiceImplTest {
3537
@Inject
3638
protected WxCpService wxService;
@@ -154,11 +156,24 @@ public void testSendMessage_miniProgram_notice() throws WxErrorException {
154156
}
155157

156158
@Test
157-
public void testLinkedCorpMessageSend() throws WxErrorException {
159+
public void testSendLinkedCorpMessage() throws WxErrorException {
158160
this.wxService.getMessageService().sendLinkedCorpMessage(WxCpLinkedCorpMessage.builder()
159161
.msgType(WxConsts.KefuMsgType.TEXT)
160162
.toUsers(new String[]{configStorage.getUserId()})
161163
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
162164
.build());
163165
}
166+
167+
@Test
168+
public void testSend() {
169+
// see other test methods
170+
}
171+
172+
@Test
173+
public void testGetStatistics() throws WxErrorException {
174+
final WxCpMessageSendStatistics statistics = this.wxService.getMessageService().getStatistics(1);
175+
assertNotNull(statistics);
176+
assertThat(statistics.getStatistics()).isNotNull();
177+
}
178+
164179
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/EcommerceService.java

+37-22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* </pre>
1313
*
1414
* @author cloudX
15-
* @date 2020/08/17
15+
* @date 2020 /08/17
1616
*/
1717
public interface EcommerceService {
1818
/**
@@ -63,8 +63,9 @@ public interface EcommerceService {
6363
* </pre>
6464
*
6565
* @param tradeType 支付方式
66-
* @param request 请求对象
67-
* @return 微信合单支付返回
66+
* @param request 请求对象
67+
* @return 微信合单支付返回 transactions result
68+
* @throws WxPayException the wx pay exception
6869
*/
6970
TransactionsResult combine(TradeTypeEnum tradeType, CombineTransactionsRequest request) throws WxPayException;
7071

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

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

96100
/**
97-
* <pre>
101+
* <pre>
98102
* 服务商模式普通支付API(APP支付、JSAPI支付、H5支付、NATIVE支付).
99103
* 请求URL:https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi
100104
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/transactions_sl.shtml
101105
* </pre>
106+
*
102107
* @param tradeType 支付方式
103-
* @param request 请求对象
104-
* @return 调起支付需要的参数
108+
* @param request 请求对象
109+
* @return 调起支付需要的参数 transactions result
110+
* @throws WxPayException the wx pay exception
105111
*/
106112
TransactionsResult partner(TradeTypeEnum tradeType, PartnerTransactionsRequest request) throws WxPayException;
107113

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

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

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

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

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

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

@@ -182,7 +196,8 @@ public interface EcommerceService {
182196
* </pre>
183197
*
184198
* @param request 分账请求
185-
* @return 返回数据
199+
* @return 返回数据 profit sharing result
200+
* @throws WxPayException the wx pay exception
186201
*/
187202
ProfitSharingResult profitSharing(ProfitSharingRequest request) throws WxPayException;
188203

0 commit comments

Comments
 (0)