1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.util.Calendar;
4 import java.util.Date;
5 import java.util.List;
6
7 import com.alibaba.fastjson.JSON;
8 import com.alibaba.fastjson.JSONObject;
9 import com.foxinmy.weixin4j.exception.WeixinException;
10 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
11 import com.foxinmy.weixin4j.model.Token;
12 import com.foxinmy.weixin4j.mp.type.DatacubeType;
13 import com.foxinmy.weixin4j.token.TokenManager;
14 import com.foxinmy.weixin4j.util.DateUtil;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class DataApi extends MpApi {
30 private final TokenManager tokenManager;
31
32 public DataApi(TokenManager tokenManager) {
33 this.tokenManager = tokenManager;
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public List<?> datacube(DatacubeType datacubeType, Date beginDate,
49 int offset) throws WeixinException {
50 Calendar ca = Calendar.getInstance();
51 ca.setTime(beginDate);
52 ca.add(Calendar.DAY_OF_MONTH, offset);
53 return datacube(datacubeType, beginDate, ca.getTime());
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public List<?> datacube(DatacubeType datacubeType, int offset, Date endDate)
69 throws WeixinException {
70 Calendar ca = Calendar.getInstance();
71 ca.setTime(endDate);
72 ca.add(Calendar.DAY_OF_MONTH, 0 - offset);
73 return datacube(datacubeType, ca.getTime(), endDate);
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public List<?> datacube(DatacubeType datacubeType, Date date)
87 throws WeixinException {
88 return datacube(datacubeType, date, date);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public List<?> datacube(DatacubeType datacubeType, Date beginDate,
120 Date endDate) throws WeixinException {
121 String datacube_uri = getRequestUri("datacube_uri");
122 Token token = tokenManager.getCache();
123 JSONObject obj = new JSONObject();
124 obj.put("begin_date", DateUtil.fortmat2yyyy_MM_dd(beginDate));
125 obj.put("end_date", DateUtil.fortmat2yyyy_MM_dd(endDate));
126 WeixinResponse response = weixinExecutor.post(String.format(datacube_uri,
127 datacubeType.name().toLowerCase(), token.getAccessToken()), obj
128 .toJSONString());
129
130 return JSON.parseArray(response.getAsJson().getString("list"),
131 datacubeType.getClazz());
132 }
133 }