View Javadoc
1   package com.foxinmy.weixin4j.qy.api;
2   
3   import java.util.List;
4   
5   import com.alibaba.fastjson.JSON;
6   import com.alibaba.fastjson.JSONObject;
7   import com.foxinmy.weixin4j.exception.WeixinException;
8   import com.foxinmy.weixin4j.http.weixin.ApiResult;
9   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
10  import com.foxinmy.weixin4j.model.Token;
11  import com.foxinmy.weixin4j.qy.message.ChatMessage;
12  import com.foxinmy.weixin4j.qy.model.ChatInfo;
13  import com.foxinmy.weixin4j.qy.model.ChatMute;
14  import com.foxinmy.weixin4j.qy.type.ChatType;
15  import com.foxinmy.weixin4j.token.TokenManager;
16  import com.foxinmy.weixin4j.tuple.ChatTuple;
17  import com.foxinmy.weixin4j.util.ObjectId;
18  import com.foxinmy.weixin4j.util.StringUtil;
19  
20  /**
21   * 会话服务接口
22   * 
23   * @className ChatApi
24   * @author jinyu(foxinmy@gmail.com)
25   * @date 2015年7月31日
26   * @since JDK 1.6
27   * @see <a
28   *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%9C%8D%E5%8A%A1">企业号消息服务</a>
29   */
30  public class ChatApi extends QyApi {
31  	private final TokenManager tokenManager;
32  
33  	public ChatApi(TokenManager tokenManager) {
34  		this.tokenManager = tokenManager;
35  	}
36  
37  	/**
38  	 * 创建会话 <font color="red">如果会话id为空,程序会自动生成一个唯一ID</font>
39  	 * 
40  	 * @param chatInfo
41  	 *            会话信息
42  	 * @return 会话ID
43  	 * @see com.foxinmy.weixin4j.qy.model.ChatInfo
44  	 * @see <a
45  	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E5.88.9B.E5.BB.BA.E4.BC.9A.E8.AF.9D">创建会话</a>
46  	 * @throws WeixinException
47  	 */
48  	public String createChat(ChatInfo chatInfo) throws WeixinException {
49  		String chatId = chatInfo.getChatId();
50  		JSONObject obj = (JSONObject) JSON.toJSON(chatInfo);
51  		if (StringUtil.isBlank(chatId)) {
52  			chatId = ObjectId.get().toHexString();
53  			obj.put("chatid", chatId);
54  		}
55  		String message_chat_create_uri = getRequestUri("message_chat_create_uri");
56  		Token token = tokenManager.getCache();
57  		weixinExecutor.post(
58  				String.format(message_chat_create_uri, token.getAccessToken()),
59  				obj.toJSONString());
60  		return chatId;
61  	}
62  
63  	/**
64  	 * 获取会话
65  	 * 
66  	 * @param chatId
67  	 *            会话ID
68  	 * @return 会话信息
69  	 * @see com.foxinmy.weixin4j.qy.model.ChatInfo
70  	 * @see <a
71  	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.9A.E8.AF.9D">获取会话</a>
72  	 * @throws WeixinException
73  	 */
74  	public ChatInfo getChat(String chatId) throws WeixinException {
75  		String message_chat_get_uri = getRequestUri("message_chat_get_uri");
76  		Token token = tokenManager.getCache();
77  		WeixinResponse response = weixinExecutor.get(String.format(
78  				message_chat_get_uri, token.getAccessToken(), chatId));
79  		return response.getAsJson().getObject("chat_info", ChatInfo.class);
80  	}
81  
82  	/**
83  	 * 更新会话
84  	 * 
85  	 * @param chatInfo
86  	 *            会话信息 至少保持会话ID不能为空
87  	 * @param operator
88  	 *            操作人userid
89  	 * @param addUsers
90  	 *            会话新增成员列表
91  	 * @param deleteUsers
92  	 *            会话退出成员列表
93  	 * @return 处理结果
94  	 * @see com.foxinmy.weixin4j.qy.model.ChatInfo
95  	 * @see <a
96  	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E4.BF.AE.E6.94.B9.E4.BC.9A.E8.AF.9D.E4.BF.A1.E6.81.AF">修改会话信息</a>
97  	 * @throws WeixinException
98  	 */
99  	public ApiResult updateChat(ChatInfo chatInfo, String operator,
100 			List<String> addUsers, List<String> deleteUsers)
101 			throws WeixinException {
102 		JSONObject obj = (JSONObject) JSON.toJSON(chatInfo);
103 		obj.remove("userlist");
104 		obj.put("op_user", operator);
105 		obj.put("add_user_list", addUsers);
106 		obj.put("del_user_list", deleteUsers);
107 		String message_chat_update_uri = getRequestUri("message_chat_update_uri");
108 		Token token = tokenManager.getCache();
109 		WeixinResponse response = weixinExecutor.post(
110 				String.format(message_chat_update_uri, token.getAccessToken()),
111 				obj.toJSONString());
112 		return response.getAsResult();
113 	}
114 
115 	/**
116 	 * 退出会话
117 	 * 
118 	 * @param chatId
119 	 *            会话ID
120 	 * @param operator
121 	 *            操作人userid
122 	 * @return 处理结果
123 	 * @see <a
124 	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E9.80.80.E5.87.BA.E4.BC.9A.E8.AF.9D">退出会话</a>
125 	 * @throws WeixinException
126 	 */
127 	public ApiResult quitChat(String chatId, String operator)
128 			throws WeixinException {
129 		JSONObject obj = new JSONObject();
130 		obj.put("chatid", chatId);
131 		obj.put("op_user", operator);
132 		String message_chat_quit_uri = getRequestUri("message_chat_quit_uri");
133 		Token token = tokenManager.getCache();
134 		WeixinResponse response = weixinExecutor.post(
135 				String.format(message_chat_quit_uri, token.getAccessToken()),
136 				obj.toJSONString());
137 		return response.getAsResult();
138 	}
139 
140 	/**
141 	 * 清除会话未读状态
142 	 * 
143 	 * @param targetId
144 	 *            会话值,为userid|chatid,分别表示:成员id|会话id
145 	 * @param owner
146 	 *            会话所有者的userid
147 	 * @param chatType
148 	 *            会话类型:single|group,分别表示:群聊|单聊
149 	 * @return 处理结果
150 	 * @see <a
151 	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E6.B8.85.E9.99.A4.E4.BC.9A.E8.AF.9D.E6.9C.AA.E8.AF.BB.E7.8A.B6.E6.80.81">清除会话未读状态</a>
152 	 * @throws WeixinException
153 	 */
154 	public ApiResult clearChatNotify(String targetId, String owner,
155 			ChatType chatType) throws WeixinException {
156 		JSONObject chat = new JSONObject();
157 		chat.put("type", chatType.name());
158 		chat.put("id", targetId);
159 		String message_chat_clearnotify_uri = getRequestUri("message_chat_clearnotify_uri");
160 		Token token = tokenManager.getCache();
161 		WeixinResponse response = weixinExecutor.post(
162 				String.format(message_chat_clearnotify_uri,
163 						token.getAccessToken()),
164 				String.format("{\"op_user\": \"%s\",\"chat\":%s", owner,
165 						chat.toJSONString()));
166 		return response.getAsResult();
167 	}
168 
169 	/**
170 	 * 设置成员接收到的消息是否提醒。主要场景是用于对接企业im的在线状态,如成员处于在线状态时,可以设置该成员的消息免打扰。当成员离线时,关闭免打扰状态
171 	 * ,对微信端进行提醒。
172 	 * 
173 	 * @param chatMutes
174 	 *            提醒参数
175 	 * @see com.foxinmy.weixin4j.qy.model.ChatMute
176 	 * @see <a href=
177 	 *      "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.AE.BE.E7.BD.AE.E6.88.90.E5.91.98.E6.96.B0.E6.B6.88.E6.81.AF.E5.85.8D.E6.89.93.E6.89.B0"
178 	 *      >设置成员新消息免打扰</a>
179 	 * @return 列表中不存在的成员,剩余合法成员会继续执行。
180 	 * @throws WeixinException
181 	 */
182 	public List<String> setChatMute(List<ChatMute> chatMutes)
183 			throws WeixinException {
184 		JSONObject mute = new JSONObject();
185 		mute.put("user_mute_list", chatMutes);
186 		String message_chat_setmute_uri = getRequestUri("message_chat_setmute_uri");
187 		Token token = tokenManager.getCache();
188 		WeixinResponse response = weixinExecutor
189 				.post(String.format(message_chat_setmute_uri,
190 						token.getAccessToken()), mute.toJSONString());
191 		return JSON.parseArray(response.getAsJson().getString("invaliduser"),
192 				String.class);
193 	}
194 
195 	/**
196 	 * 发送消息
197 	 * 
198 	 * @param message
199 	 *            消息对象
200 	 * @return 处理结果
201 	 * @see com.foxinmy.weixin4j.qy.message.ChatMessage
202 	 * @see <a
203 	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E5.8F.91.E6.B6.88.E6.81.AF">发送消息</a>
204 	 * @throws WeixinException
205 	 */
206 	public ApiResult sendChatMessage(ChatMessage message)
207 			throws WeixinException {
208 		ChatTuple tuple = message.getChatTuple();
209 		String msgtype = tuple.getMessageType();
210 		JSONObject msg = new JSONObject();
211 		JSONObject receiver = new JSONObject();
212 		receiver.put("id", message.getTargetId());
213 		receiver.put("type", message.getChatType().name());
214 		msg.put("receiver", receiver);
215 		msg.put("sender", message.getSenderId());
216 		msg.put("msgtype", msgtype);
217 		msg.put(msgtype, tuple);
218 		String message_chat_send_uri = getRequestUri("message_chat_send_uri");
219 		Token token = tokenManager.getCache();
220 		WeixinResponse response = weixinExecutor.post(
221 				String.format(message_chat_send_uri, token.getAccessToken()),
222 				msg.toJSONString());
223 		return response.getAsResult();
224 	}
225 }