1 package com.foxinmy.weixin4j.qy.api;
2
3 import com.alibaba.fastjson.JSONObject;
4 import com.alibaba.fastjson.TypeReference;
5 import com.foxinmy.weixin4j.exception.WeixinException;
6 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
7 import com.foxinmy.weixin4j.model.Token;
8 import com.foxinmy.weixin4j.qy.model.BatchResult;
9 import com.foxinmy.weixin4j.qy.model.Callback;
10 import com.foxinmy.weixin4j.qy.model.IdParameter;
11 import com.foxinmy.weixin4j.token.TokenManager;
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class BatchApi extends QyApi {
26
27 private final TokenManager tokenManager;
28
29 public BatchApi(TokenManager tokenManager) {
30 this.tokenManager = tokenManager;
31 }
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public String inviteUser(IdParameter parameter, Callback callback,
50 String tips) throws WeixinException {
51 String batch_inviteuser_uri = getRequestUri("batch_inviteuser_uri");
52 Token token = tokenManager.getCache();
53 JSONObject obj = new JSONObject();
54 obj.putAll(parameter.getParameter());
55 obj.put("callback", callback);
56 obj.put("invite_tips", tips);
57 WeixinResponse response = weixinExecutor.post(
58 String.format(batch_inviteuser_uri, token.getAccessToken()),
59 obj.toJSONString());
60 return response.getAsJson().getString("jobid");
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public String syncUser(String mediaId, Callback callback)
83 throws WeixinException {
84 String batch_syncuser_uri = getRequestUri("batch_syncuser_uri");
85 return batch(batch_syncuser_uri, mediaId, callback);
86 }
87
88 private String batch(String batchUrl, String mediaId, Callback callback)
89 throws WeixinException {
90 Token token = tokenManager.getCache();
91 JSONObject obj = new JSONObject();
92 obj.put("media_id", mediaId);
93 obj.put("callback", callback);
94 WeixinResponse response = weixinExecutor.post(
95 String.format(batchUrl, token.getAccessToken()),
96 obj.toJSONString());
97 return response.getAsJson().getString("jobid");
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public String replaceUser(String mediaId, Callback callback)
120 throws WeixinException {
121 String batch_replaceuser_uri = getRequestUri("batch_replaceuser_uri");
122 return batch(batch_replaceuser_uri, mediaId, callback);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public String replaceParty(String mediaId, Callback callback)
144 throws WeixinException {
145 String batch_replaceparty_uri = getRequestUri("batch_replaceparty_uri");
146 return batch(batch_replaceparty_uri, mediaId, callback);
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160 public BatchResult getBatchResult(String jobId) throws WeixinException {
161 Token token = tokenManager.getCache();
162 String batch_getresult_uri = getRequestUri("batch_getresult_uri");
163 WeixinResponse response = weixinExecutor.get(String.format(
164 batch_getresult_uri, token.getAccessToken(), jobId));
165 return response.getAsObject(new TypeReference<BatchResult>() {
166 });
167 }
168 }