Skip to content

Commit 931533c

Browse files
committed
抽取数据统计相关接口请求URL到其接口类中,并重构重复代码 binarywang#195
1 parent a436e62 commit 931533c

File tree

2 files changed

+57
-112
lines changed

2 files changed

+57
-112
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@
1313
* @author binarywang (https://github.com/binarywang)
1414
*/
1515
public interface WxMpDataCubeService {
16+
String GET_USER_SUMMARY = "https://api.weixin.qq.com/datacube/getusersummary";
17+
String GET_USER_CUMULATE = "https://api.weixin.qq.com/datacube/getusercumulate";
18+
String GET_ARTICLE_SUMMARY = "https://api.weixin.qq.com/datacube/getarticlesummary";
19+
String GET_ARTICLE_TOTAL = "https://api.weixin.qq.com/datacube/getarticletotal";
20+
String GET_USER_READ = "https://api.weixin.qq.com/datacube/getuserread";
21+
String GET_USER_READ_HOUR = "https://api.weixin.qq.com/datacube/getuserreadhour";
22+
String GET_USER_SHARE = "https://api.weixin.qq.com/datacube/getusershare";
23+
String GET_USER_SHARE_HOUR = "https://api.weixin.qq.com/datacube/getusersharehour";
24+
String GET_UPSTREAM_MSG = "https://api.weixin.qq.com/datacube/getupstreammsg";
25+
String GET_UPSTREAM_MSG_HOUR = "https://api.weixin.qq.com/datacube/getupstreammsghour";
26+
String GET_UPSTREAM_MSG_WEEK = "https://api.weixin.qq.com/datacube/getupstreammsgweek";
27+
String GET_UPSTREAM_MSG_MONTH = "https://api.weixin.qq.com/datacube/getupstreammsgmonth";
28+
String GET_UPSTREAM_MSG_DIST = "https://api.weixin.qq.com/datacube/getupstreammsgdist";
29+
String GET_UPSTREAM_MSG_DIST_WEEK = "https://api.weixin.qq.com/datacube/getupstreammsgdistweek";
30+
String GET_UPSTREAM_MSG_DIST_MONTH = "https://api.weixin.qq.com/datacube/getupstreammsgdistmonth";
31+
String GET_INTERFACE_SUMMARY = "https://api.weixin.qq.com/datacube/getinterfacesummary";
32+
String GET_INTERFACE_SUMMARY_HOUR = "https://api.weixin.qq.com/datacube/getinterfacesummaryhour";
33+
1634
//*******************用户分析数据接口***********************//
1735

1836
/**

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDataCubeServiceImpl.java

Lines changed: 39 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* @author binarywang (https://github.com/binarywang)
1818
*/
1919
public class WxMpDataCubeServiceImpl implements WxMpDataCubeService {
20-
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/datacube";
2120

2221
private final Format dateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
2322

@@ -29,180 +28,108 @@ public WxMpDataCubeServiceImpl(WxMpService wxMpService) {
2928

3029
@Override
3130
public List<WxDataCubeUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
32-
String url = API_URL_PREFIX + "/getusersummary";
33-
JsonObject param = new JsonObject();
34-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
35-
param.addProperty("end_date", this.dateFormat.format(endDate));
36-
String responseContent = this.wxMpService.post(url, param.toString());
31+
String responseContent = this.wxMpService.post(GET_USER_SUMMARY, buildParams(beginDate, endDate));
3732
return WxDataCubeUserSummary.fromJson(responseContent);
3833
}
3934

4035
@Override
4136
public List<WxDataCubeUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
42-
String url = API_URL_PREFIX + "/getusercumulate";
43-
JsonObject param = new JsonObject();
44-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
45-
param.addProperty("end_date", this.dateFormat.format(endDate));
46-
String responseContent = this.wxMpService.post(url, param.toString());
37+
String responseContent = this.wxMpService.post(GET_USER_CUMULATE, buildParams(beginDate, endDate));
4738
return WxDataCubeUserCumulate.fromJson(responseContent);
4839
}
4940

5041
@Override
5142
public List<WxDataCubeArticleResult> getArticleSummary(Date beginDate, Date endDate) throws WxErrorException {
52-
String url = API_URL_PREFIX + "/getarticlesummary";
53-
JsonObject param = new JsonObject();
54-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
55-
param.addProperty("end_date", this.dateFormat.format(endDate));
56-
String responseContent = this.wxMpService.post(url, param.toString());
57-
return WxDataCubeArticleResult.fromJson(responseContent);
43+
return this.getArticleResults(GET_ARTICLE_SUMMARY, beginDate, endDate);
5844
}
5945

6046
@Override
6147
public List<WxDataCubeArticleTotal> getArticleTotal(Date beginDate, Date endDate) throws WxErrorException {
62-
String url = API_URL_PREFIX + "/getarticletotal";
63-
JsonObject param = new JsonObject();
64-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
65-
param.addProperty("end_date", this.dateFormat.format(endDate));
66-
String responseContent = this.wxMpService.post(url, param.toString());
48+
String responseContent = this.wxMpService.post(GET_ARTICLE_TOTAL, buildParams(beginDate, endDate));
6749
return WxDataCubeArticleTotal.fromJson(responseContent);
6850
}
6951

7052
@Override
7153
public List<WxDataCubeArticleResult> getUserRead(Date beginDate, Date endDate) throws WxErrorException {
72-
String url = API_URL_PREFIX + "/getuserread";
73-
JsonObject param = new JsonObject();
74-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
75-
param.addProperty("end_date", this.dateFormat.format(endDate));
76-
String responseContent = this.wxMpService.post(url, param.toString());
77-
return WxDataCubeArticleResult.fromJson(responseContent);
54+
return this.getArticleResults(GET_USER_READ, beginDate, endDate);
7855
}
7956

8057
@Override
8158
public List<WxDataCubeArticleResult> getUserReadHour(Date beginDate, Date endDate) throws WxErrorException {
82-
String url = API_URL_PREFIX + "/getuserreadhour";
83-
JsonObject param = new JsonObject();
84-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
85-
param.addProperty("end_date", this.dateFormat.format(endDate));
86-
String responseContent = this.wxMpService.post(url, param.toString());
87-
return WxDataCubeArticleResult.fromJson(responseContent);
59+
return this.getArticleResults(GET_USER_READ_HOUR, beginDate, endDate);
8860
}
8961

9062
@Override
9163
public List<WxDataCubeArticleResult> getUserShare(Date beginDate, Date endDate) throws WxErrorException {
92-
String url = API_URL_PREFIX + "/getusershare";
93-
JsonObject param = new JsonObject();
94-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
95-
param.addProperty("end_date", this.dateFormat.format(endDate));
96-
String responseContent = this.wxMpService.post(url, param.toString());
97-
return WxDataCubeArticleResult.fromJson(responseContent);
64+
return this.getArticleResults(GET_USER_SHARE, beginDate, endDate);
9865
}
9966

10067
@Override
10168
public List<WxDataCubeArticleResult> getUserShareHour(Date beginDate, Date endDate) throws WxErrorException {
102-
String url = API_URL_PREFIX + "/getusersharehour";
103-
JsonObject param = new JsonObject();
104-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
105-
param.addProperty("end_date", this.dateFormat.format(endDate));
106-
String responseContent = this.wxMpService.post(url, param.toString());
69+
return this.getArticleResults(GET_USER_SHARE_HOUR, beginDate, endDate);
70+
}
71+
72+
private List<WxDataCubeArticleResult> getArticleResults(String url, Date beginDate, Date endDate) throws WxErrorException {
73+
String responseContent = this.wxMpService.post(url, buildParams(beginDate, endDate));
10774
return WxDataCubeArticleResult.fromJson(responseContent);
10875
}
10976

11077
@Override
111-
public List<WxDataCubeMsgResult> getUpstreamMsg(Date beginDate, Date endDate)
112-
throws WxErrorException {
113-
String url = API_URL_PREFIX + "/getupstreammsg";
114-
JsonObject param = new JsonObject();
115-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
116-
param.addProperty("end_date", this.dateFormat.format(endDate));
117-
String responseContent = this.wxMpService.post(url, param.toString());
118-
return WxDataCubeMsgResult.fromJson(responseContent);
78+
public List<WxDataCubeMsgResult> getUpstreamMsg(Date beginDate, Date endDate) throws WxErrorException {
79+
return this.getUpstreamMsg(GET_UPSTREAM_MSG, beginDate, endDate);
11980
}
12081

12182
@Override
122-
public List<WxDataCubeMsgResult> getUpstreamMsgHour(Date beginDate,
123-
Date endDate) throws WxErrorException {
124-
String url = API_URL_PREFIX + "/getupstreammsghour";
125-
JsonObject param = new JsonObject();
126-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
127-
param.addProperty("end_date", this.dateFormat.format(endDate));
128-
String responseContent = this.wxMpService.post(url, param.toString());
129-
return WxDataCubeMsgResult.fromJson(responseContent);
83+
public List<WxDataCubeMsgResult> getUpstreamMsgHour(Date beginDate, Date endDate) throws WxErrorException {
84+
return this.getUpstreamMsg(GET_UPSTREAM_MSG_HOUR, beginDate, endDate);
13085
}
13186

13287
@Override
133-
public List<WxDataCubeMsgResult> getUpstreamMsgWeek(Date beginDate,
134-
Date endDate) throws WxErrorException {
135-
String url = API_URL_PREFIX + "/getupstreammsgweek";
136-
JsonObject param = new JsonObject();
137-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
138-
param.addProperty("end_date", this.dateFormat.format(endDate));
139-
String responseContent = this.wxMpService.post(url, param.toString());
140-
return WxDataCubeMsgResult.fromJson(responseContent);
88+
public List<WxDataCubeMsgResult> getUpstreamMsgWeek(Date beginDate, Date endDate) throws WxErrorException {
89+
return this.getUpstreamMsg(GET_UPSTREAM_MSG_WEEK, beginDate, endDate);
14190
}
14291

14392
@Override
144-
public List<WxDataCubeMsgResult> getUpstreamMsgMonth(Date beginDate,
145-
Date endDate) throws WxErrorException {
146-
String url = API_URL_PREFIX + "/getupstreammsgmonth";
147-
JsonObject param = new JsonObject();
148-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
149-
param.addProperty("end_date", this.dateFormat.format(endDate));
150-
String responseContent = this.wxMpService.post(url, param.toString());
151-
return WxDataCubeMsgResult.fromJson(responseContent);
93+
public List<WxDataCubeMsgResult> getUpstreamMsgMonth(Date beginDate, Date endDate) throws WxErrorException {
94+
return this.getUpstreamMsg(GET_UPSTREAM_MSG_MONTH, beginDate, endDate);
15295
}
15396

15497
@Override
155-
public List<WxDataCubeMsgResult> getUpstreamMsgDist(Date beginDate,
156-
Date endDate) throws WxErrorException {
157-
String url = API_URL_PREFIX + "/getupstreammsgdist";
158-
JsonObject param = new JsonObject();
159-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
160-
param.addProperty("end_date", this.dateFormat.format(endDate));
161-
String responseContent = this.wxMpService.post(url, param.toString());
162-
return WxDataCubeMsgResult.fromJson(responseContent);
98+
public List<WxDataCubeMsgResult> getUpstreamMsgDist(Date beginDate, Date endDate) throws WxErrorException {
99+
return this.getUpstreamMsg(GET_UPSTREAM_MSG_DIST, beginDate, endDate);
163100
}
164101

165102
@Override
166-
public List<WxDataCubeMsgResult> getUpstreamMsgDistWeek(Date beginDate,
167-
Date endDate) throws WxErrorException {
168-
String url = API_URL_PREFIX + "/getupstreammsgdistweek";
169-
JsonObject param = new JsonObject();
170-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
171-
param.addProperty("end_date", this.dateFormat.format(endDate));
172-
String responseContent = this.wxMpService.post(url, param.toString());
173-
return WxDataCubeMsgResult.fromJson(responseContent);
103+
public List<WxDataCubeMsgResult> getUpstreamMsgDistWeek(Date beginDate, Date endDate) throws WxErrorException {
104+
return this.getUpstreamMsg(GET_UPSTREAM_MSG_DIST_WEEK, beginDate, endDate);
174105
}
175106

176107
@Override
177-
public List<WxDataCubeMsgResult> getUpstreamMsgDistMonth(Date beginDate,
178-
Date endDate) throws WxErrorException {
179-
String url = API_URL_PREFIX + "/getupstreammsgdistmonth";
180-
JsonObject param = new JsonObject();
181-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
182-
param.addProperty("end_date", this.dateFormat.format(endDate));
183-
String responseContent = this.wxMpService.post(url, param.toString());
108+
public List<WxDataCubeMsgResult> getUpstreamMsgDistMonth(Date beginDate, Date endDate) throws WxErrorException {
109+
return this.getUpstreamMsg(GET_UPSTREAM_MSG_DIST_MONTH, beginDate, endDate);
110+
}
111+
112+
private List<WxDataCubeMsgResult> getUpstreamMsg(String url, Date beginDate, Date endDate) throws WxErrorException {
113+
String responseContent = this.wxMpService.post(url, buildParams(beginDate, endDate));
184114
return WxDataCubeMsgResult.fromJson(responseContent);
185115
}
186116

187117
@Override
188-
public List<WxDataCubeInterfaceResult> getInterfaceSummary(Date beginDate,
189-
Date endDate) throws WxErrorException {
190-
String url = API_URL_PREFIX + "/getinterfacesummary";
191-
JsonObject param = new JsonObject();
192-
param.addProperty("begin_date", this.dateFormat.format(beginDate));
193-
param.addProperty("end_date", this.dateFormat.format(endDate));
194-
String responseContent = this.wxMpService.post(url, param.toString());
118+
public List<WxDataCubeInterfaceResult> getInterfaceSummary(Date beginDate, Date endDate) throws WxErrorException {
119+
String responseContent = this.wxMpService.post(GET_INTERFACE_SUMMARY, buildParams(beginDate, endDate));
195120
return WxDataCubeInterfaceResult.fromJson(responseContent);
196121
}
197122

198-
@Override
199-
public List<WxDataCubeInterfaceResult> getInterfaceSummaryHour(Date beginDate,
200-
Date endDate) throws WxErrorException {
201-
String url = API_URL_PREFIX + "/getinterfacesummaryhour";
123+
private String buildParams(Date beginDate, Date endDate) {
202124
JsonObject param = new JsonObject();
203125
param.addProperty("begin_date", this.dateFormat.format(beginDate));
204126
param.addProperty("end_date", this.dateFormat.format(endDate));
205-
String responseContent = this.wxMpService.post(url, param.toString());
127+
return param.toString();
128+
}
129+
130+
@Override
131+
public List<WxDataCubeInterfaceResult> getInterfaceSummaryHour(Date beginDate, Date endDate) throws WxErrorException {
132+
String responseContent = this.wxMpService.post(GET_INTERFACE_SUMMARY_HOUR, buildParams(beginDate, endDate));
206133
return WxDataCubeInterfaceResult.fromJson(responseContent);
207134
}
208135
}

0 commit comments

Comments
 (0)