1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.alibaba.fastjson.JSON;
7 import com.alibaba.fastjson.JSONArray;
8 import com.alibaba.fastjson.JSONObject;
9 import com.alibaba.fastjson.TypeReference;
10 import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
11 import com.foxinmy.weixin4j.exception.WeixinException;
12 import com.foxinmy.weixin4j.http.weixin.ApiResult;
13 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
14 import com.foxinmy.weixin4j.model.Button;
15 import com.foxinmy.weixin4j.model.Token;
16 import com.foxinmy.weixin4j.mp.model.AutoReplySetting;
17 import com.foxinmy.weixin4j.mp.model.MenuSetting;
18 import com.foxinmy.weixin4j.mp.model.SemQuery;
19 import com.foxinmy.weixin4j.mp.model.SemResult;
20 import com.foxinmy.weixin4j.token.TokenManager;
21 import com.foxinmy.weixin4j.tuple.MpArticle;
22
23
24
25
26
27
28
29
30
31
32 public class HelperApi extends MpApi {
33
34 private final TokenManager tokenManager;
35
36 public HelperApi(TokenManager tokenManager) {
37 this.tokenManager = tokenManager;
38 }
39
40
41
42
43
44
45
46
47
48
49
50 public String getShorturl(String url) throws WeixinException {
51 String shorturl_uri = getRequestUri("shorturl_uri");
52 Token token = tokenManager.getCache();
53 JSONObject obj = new JSONObject();
54 obj.put("action", "long2short");
55 obj.put("long_url", url);
56 WeixinResponse response = weixinExecutor.post(String.format(shorturl_uri, token.getAccessToken()),
57 obj.toJSONString());
58
59 return response.getAsJson().getString("short_url");
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public SemResult semantic(SemQuery semQuery) throws WeixinException {
75 String semantic_uri = getRequestUri("semantic_uri");
76 Token token = tokenManager.getCache();
77 WeixinResponse response = weixinExecutor.post(String.format(semantic_uri, token.getAccessToken()),
78 semQuery.toJson());
79 return response.getAsObject(new TypeReference<SemResult>() {
80 });
81 }
82
83
84
85
86
87
88
89
90
91 public List<String> getWechatServerIp() throws WeixinException {
92 String getcallbackip_uri = getRequestUri("getcallbackip_uri");
93 Token token = tokenManager.getCache();
94 WeixinResponse response = weixinExecutor.get(String.format(getcallbackip_uri, token.getAccessToken()));
95 return JSON.parseArray(response.getAsJson().getString("ip_list"), String.class);
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public MenuSetting getMenuSetting() throws WeixinException {
112 String menu_get_selfmenu_uri = getRequestUri("menu_get_selfmenu_uri");
113 Token token = tokenManager.getCache();
114 WeixinResponse response = weixinExecutor.get(String.format(menu_get_selfmenu_uri, token.getAccessToken()));
115 JSONObject result = response.getAsJson();
116 JSONArray buttons = result.getJSONObject("selfmenu_info").getJSONArray("button");
117 List<Button> buttonList = new ArrayList<Button>(buttons.size());
118 JSONObject buttonObj = null;
119 for (int i = 0; i < buttons.size(); i++) {
120 buttonObj = buttons.getJSONObject(i);
121 if (buttonObj.containsKey("sub_button")) {
122 buttonObj.put("sub_button", buttonObj.getJSONObject("sub_button").getJSONArray("list"));
123 buttonObj.put("type", "popups");
124 }
125 buttonList.add(JSON.parseObject(buttonObj.toJSONString(), Button.class, ButtonExtraProcessor.global));
126 }
127 return new MenuSetting(result.getBooleanValue("is_menu_open"), buttonList);
128 }
129
130 private static final class ButtonExtraProcessor implements ExtraProcessor {
131 private static ButtonExtraProcessor global = new ButtonExtraProcessor();
132 private static final String KEY = "news_info";
133
134 private ButtonExtraProcessor() {
135 }
136
137 @Override
138 public void processExtra(Object object, String key, Object value) {
139 if (KEY.equalsIgnoreCase(key)) {
140 JSONArray news = ((JSONObject) value).getJSONArray("list");
141 List<MpArticle> newsList = new ArrayList<MpArticle>(news.size());
142 JSONObject article = null;
143 for (int i = 0; i < news.size(); i++) {
144 article = news.getJSONObject(i);
145 article.put("show_cover_pic", article.remove("show_cover"));
146 article.put("thumb_url", article.remove("cover_url"));
147 article.put("url", article.remove("content_url"));
148 article.put("content_source_url", article.remove("source_url"));
149 newsList.add(JSON.toJavaObject(article, MpArticle.class));
150 }
151 ((Button) object).setExtra(newsList);
152 } else {
153 ((Button) object).setContent(String.valueOf(value));
154 }
155 }
156 };
157
158
159
160
161
162
163
164
165
166
167 public AutoReplySetting getAutoReplySetting() throws WeixinException {
168 String autoreply_setting_get_uri = getRequestUri("autoreply_setting_get_uri");
169 Token token = tokenManager.getCache();
170 WeixinResponse response = weixinExecutor.get(String.format(autoreply_setting_get_uri, token.getAccessToken()));
171
172 JSONObject result = response.getAsJson();
173
174 AutoReplySetting replySetting = JSON.toJavaObject(result, AutoReplySetting.class);
175 List<AutoReplySetting.Rule> ruleList = null;
176 if (result.containsKey("keyword_autoreply_info")) {
177 JSONArray keywordList = result.getJSONObject("keyword_autoreply_info").getJSONArray("list");
178 ruleList = new ArrayList<AutoReplySetting.Rule>(keywordList.size());
179 JSONObject keywordObj = null;
180 JSONArray replyList = null;
181 JSONObject replyObj = null;
182 for (int i = 0; i < keywordList.size(); i++) {
183 keywordObj = keywordList.getJSONObject(i);
184 AutoReplySetting.Rule rule = JSON.toJavaObject(keywordObj, AutoReplySetting.Rule.class);
185 replyList = keywordObj.getJSONArray("reply_list_info");
186 List<AutoReplySetting.Entry> entryList = new ArrayList<AutoReplySetting.Entry>(replyList.size());
187 for (int j = 0; j < replyList.size(); j++) {
188 replyObj = replyList.getJSONObject(j);
189 if (replyObj.getString("type").equals("news")) {
190 entryList.add(JSON.parseObject(replyObj.toJSONString(), AutoReplySetting.Entry.class,
191 ButtonExtraProcessor.global));
192 } else {
193 entryList.add(JSON.toJavaObject(replyObj, AutoReplySetting.Entry.class));
194 }
195 }
196 rule.setReplyList(entryList);
197 ruleList.add(rule);
198 }
199 }
200 replySetting.setKeywordReplyList(ruleList);
201 return replySetting;
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216 public ApiResult clearQuota(String appId) throws WeixinException {
217 String clearquota_uri = getRequestUri("clearquota_uri");
218 String body = String.format("{\"appid\":\"%s\"}", appId);
219 WeixinResponse response = weixinExecutor.post(String.format(clearquota_uri, tokenManager.getAccessToken()),
220 body);
221 return response.getAsResult();
222 }
223 }