1 package com.foxinmy.weixin4j.qy.api;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6 import java.util.Map;
7
8 import com.alibaba.fastjson.JSON;
9 import com.alibaba.fastjson.JSONObject;
10 import com.foxinmy.weixin4j.exception.WeixinException;
11 import com.foxinmy.weixin4j.http.weixin.ApiResult;
12 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
13 import com.foxinmy.weixin4j.model.Token;
14 import com.foxinmy.weixin4j.qy.message.CustomeMessage;
15 import com.foxinmy.weixin4j.qy.message.NotifyMessage;
16 import com.foxinmy.weixin4j.qy.model.IdParameter;
17 import com.foxinmy.weixin4j.qy.type.KfType;
18 import com.foxinmy.weixin4j.token.TokenManager;
19 import com.foxinmy.weixin4j.tuple.MpNews;
20 import com.foxinmy.weixin4j.tuple.NotifyTuple;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class NotifyApi extends QyApi {
39
40 private final TokenManager tokenManager;
41
42 public NotifyApi(TokenManager tokenManager) {
43 this.tokenManager = tokenManager;
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public IdParameter sendNotifyMessage(NotifyMessage message)
72 throws WeixinException {
73 NotifyTuple tuple = message.getTuple();
74 if (tuple instanceof MpNews) {
75 if (((MpNews) tuple).getArticles().isEmpty()) {
76 throw new WeixinException("notify fail:articles is required");
77 }
78 }
79 Map<String, String> target = message.getTarget().getParameter();
80 String msgtype = tuple.getMessageType();
81 JSONObject obj = (JSONObject) JSON.toJSON(message);
82 obj.put("msgtype", msgtype);
83 obj.put(msgtype, tuple);
84 if (target == null || target.isEmpty()) {
85 obj.put("touser", "@all");
86 } else {
87 obj.putAll(target);
88 }
89 String message_send_uri = getRequestUri("message_send_uri");
90 Token token = tokenManager.getCache();
91 WeixinResponse response = weixinExecutor.post(
92 String.format(message_send_uri, token.getAccessToken()),
93 obj.toJSONString());
94 obj = response.getAsJson();
95 IdParameter idParameter = IdParameter.get();
96 if (obj.containsKey("invaliduser")) {
97 idParameter.setUserIds(Arrays.asList(obj.getString("invaliduser")
98 .split(IdParameter.SEPARATORS)));
99 }
100 if (obj.containsKey("invalidparty")) {
101 List<Integer> partyIds = new ArrayList<Integer>();
102 for (String id : obj.getString("invalidparty").split(
103 IdParameter.SEPARATORS)) {
104 partyIds.add(Integer.parseInt(id));
105 }
106 idParameter.setPartyIds(partyIds);
107 }
108 if (obj.containsKey("invalidtag")) {
109 List<Integer> tagIds = new ArrayList<Integer>();
110 for (String id : obj.getString("invalidtag").split(
111 IdParameter.SEPARATORS)) {
112 tagIds.add(Integer.parseInt(id));
113 }
114 idParameter.setTagIds(tagIds);
115 }
116 return idParameter;
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public ApiResult sendCustomeMessage(CustomeMessage message)
136 throws WeixinException {
137 NotifyTuple tuple = message.getTuple();
138 String msgtype = tuple.getMessageType();
139 JSONObject obj = (JSONObject) JSON.toJSON(message);
140 obj.put("msgtype", msgtype);
141 obj.put(msgtype, tuple);
142 String message_kf_send_uri = getRequestUri("message_kf_send_uri");
143 Token token = tokenManager.getCache();
144 WeixinResponse response = weixinExecutor.post(
145 String.format(message_kf_send_uri, token.getAccessToken()),
146 obj.toJSONString());
147 return response.getAsResult();
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161 public IdParameter[] getKfList(KfType kfType) throws WeixinException {
162 String message_kf_list_uri = getRequestUri("message_kf_list_uri");
163 if (kfType != null) {
164 message_kf_list_uri += "&type=" + kfType.name();
165 }
166 Token token = tokenManager.getCache();
167 WeixinResponse response = weixinExecutor.get(String.format(
168 message_kf_list_uri, token.getAccessToken()));
169 JSONObject obj = response.getAsJson();
170 return new IdParameter[] {
171 obj.containsKey("internal") ? obj.getObject("internal",
172 IdParameter.class) : null,
173 obj.containsKey("external") ? obj.getObject("external",
174 IdParameter.class) : null };
175 }
176 }