ImUtill.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.yihu.wlyy.util;
  2. import org.json.JSONArray;
  3. import org.json.JSONObject;
  4. /**
  5. * Created by 卓 on 2017/1/13.
  6. */
  7. public class ImUtill {
  8. /**
  9. * 发送消息给IM
  10. *
  11. * @param from 来自
  12. * @param contentType 1文字 2图片消息
  13. * @param content 内容
  14. */
  15. public static String sendImMsg(String from,String fromName, String sessionId, String contentType, String content,String businessType) {
  16. String imAddr = SystemConf.getInstance().getImListGet() + "/api/v2/sessions/"+sessionId+"/messages";
  17. JSONObject params = new JSONObject();
  18. params.put("sender_id", from);
  19. params.put("sender_name", fromName);
  20. params.put("content_type", contentType);
  21. params.put("content", content);
  22. params.put("session_id", sessionId);
  23. params.put("business_type", businessType);
  24. String response = HttpClientUtil.postBody(imAddr, params);
  25. return response;
  26. }
  27. /**
  28. * 更新会话状态
  29. *
  30. * @param sessionId 会话ID
  31. * @param status 状态
  32. */
  33. public static String updateSessionStatus(String sessionId,String status) {
  34. String imAddr = SystemConf.getInstance().getImListGet() + "/api/v2/sessions/"+sessionId+"/status?status="+status+"&sessionId="+sessionId;
  35. JSONObject params = new JSONObject();
  36. String response = HttpClientUtil.postBody(imAddr, params);
  37. return response;
  38. }
  39. /**
  40. * 发送消息给IM
  41. *
  42. * @param from 来自
  43. * @param contentType 1文字 2图片消息
  44. * @param content 内容
  45. */
  46. public static String sendTopicIM(String from,String fromName, String topicId, String contentType, String content) {
  47. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  48. + ("api/v2/sessions/topic/"+topicId+"/messages");
  49. JSONObject params = new JSONObject();
  50. params.put("sender_id", from);
  51. params.put("sender_name", fromName);
  52. params.put("content_type", contentType);
  53. params.put("content", content);
  54. params.put("topic_id", topicId);;
  55. String response = HttpClientUtil.postBody(url, params);
  56. return response;
  57. }
  58. /**
  59. * 结束议题
  60. *
  61. * @param topicId 议题ID
  62. * @param endUser 结束人
  63. * @param endUserName 结束人名字
  64. * @param sessionId 会话ID
  65. */
  66. public static JSONObject endTopics(String sessionId,String endUser, String endUserName,String topicId) {
  67. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+sessionId+"/topics/"+topicId+"/ended";
  68. JSONObject params = new JSONObject();
  69. params.put("session_id", sessionId);
  70. params.put("end_user", endUser);
  71. params.put("end_user_name",endUserName);
  72. params.put("topic_id", topicId);
  73. String ret = HttpClientUtil.postBody(imAddr,params);
  74. JSONObject obj = null;
  75. try{
  76. obj = new JSONObject(ret);
  77. }catch (Exception e){
  78. return null;
  79. }
  80. return obj;
  81. }
  82. /**
  83. * 议题邀请人员
  84. * @param user 结束人名字
  85. * @param sessionId 会话ID
  86. */
  87. public static void updateTopicUser(String sessionId,String user) {
  88. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+sessionId+"/participants/"+user;
  89. JSONObject params = new JSONObject();
  90. params.put("user", user+":"+0);
  91. HttpClientUtil.putBody(imAddr,params);
  92. }
  93. /**
  94. * 创建议题
  95. *
  96. * @param topicId 议题ID
  97. * @param topicName 议题名称
  98. * @param participants 成员
  99. */
  100. public static JSONObject createTopics(String sessionId, String topicId, String topicName, JSONObject participants, JSONObject messages, String sessionType) {
  101. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+topicId+"/topics";
  102. JSONObject params = new JSONObject();
  103. params.put("topic_id", topicId);
  104. params.put("topic_name", topicName);
  105. params.put("participants", participants.toString());
  106. params.put("messages", messages.toString());
  107. params.put("session_id", sessionId);
  108. params.put("session_type", sessionType);
  109. String ret = HttpClientUtil.postBody(imAddr,params);
  110. JSONObject obj = null;
  111. try{
  112. obj = new JSONObject(ret);
  113. }catch (Exception e){
  114. return null;
  115. }
  116. return obj;
  117. }
  118. /**
  119. * 创建会话(system)
  120. *
  121. */
  122. public static JSONObject createSession(JSONObject participants,String sessionType,String sessionName,String sessionId) {
  123. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions";
  124. JSONObject params = new JSONObject();
  125. params.put("participants", participants.toString());
  126. params.put("session_name", sessionName);
  127. params.put("session_type", sessionType);
  128. params.put("session_id", sessionId);
  129. String ret = HttpClientUtil.postBody(imAddr,params);
  130. JSONObject obj = null;
  131. try{
  132. obj = new JSONObject(ret);
  133. }catch (Exception e){
  134. return null;
  135. }
  136. return obj;
  137. }
  138. /**
  139. * 获取会话实例的消息对象
  140. * @param senderId
  141. * @param senderName
  142. * @param title
  143. * @param description
  144. * @param images
  145. * @return
  146. */
  147. public static JSONObject getCreateTopicMessage(String senderId,String senderName,String title,String description,String images){
  148. JSONObject messages = new JSONObject();
  149. messages.put("description",description);
  150. messages.put("title",title);
  151. messages.put("img",images);
  152. messages.put("sender_id",senderId);
  153. messages.put("sender_name",senderName);
  154. return messages;
  155. }
  156. public static JSONObject getTopicMessage(String topicId,String startMsgId,String endMsgId,int page,int pagesize,String uid){
  157. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  158. + "api/v2/sessions/topic/"+topicId+"/messages?topic_id="+topicId+"&end="+startMsgId
  159. +"&start="+(endMsgId==null?"":endMsgId)+"&page="+page+"&pagesize="+pagesize+"&user="+uid;
  160. try{
  161. String ret = HttpClientUtil.get(url, "UTF-8");
  162. JSONObject obj = new JSONObject(ret);
  163. if(obj.getInt("status")==-1){
  164. throw new RuntimeException(obj.getString("message"));
  165. }else{
  166. return obj.getJSONObject("data");
  167. }
  168. }catch (Exception e){
  169. return null;
  170. }
  171. }
  172. public static JSONArray getSessionMessage(String sessionId,String startMsgId,String endMsgId,int page,int pagesize,String uid){
  173. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  174. + "api/v2/sessions/"+sessionId+"/messages?session_id="+sessionId+"&user="+uid+"&start_message_id="+startMsgId+"&end_message_id="+endMsgId+"&page="+page+"&pagesize="+pagesize;
  175. try{
  176. String ret = HttpClientUtil.get(url, "UTF-8");
  177. JSONArray obj = new JSONArray(ret);
  178. return obj;
  179. }catch (Exception e){
  180. return null;
  181. }
  182. }
  183. /**
  184. * 删除对应的成员信息在MUC模式中
  185. * @param userId
  186. * @param oldUserId
  187. * @param sessionId
  188. * @return
  189. */
  190. public static JSONObject deleteMucUser(String userId,String oldUserId,String sessionId) throws Exception{
  191. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  192. + "api/v2/sessions/"+sessionId+"/participant/update";
  193. try{
  194. JSONObject params = new JSONObject();
  195. params.put("user_id", userId);
  196. params.put("old_user_id", oldUserId);
  197. params.put("session_id", sessionId);
  198. String ret = HttpClientUtil.postBody(url,params);
  199. JSONObject obj = new JSONObject(ret);
  200. if(obj.getInt("status")==-1){
  201. throw new RuntimeException("人员更换失败!");
  202. }else{
  203. return obj;
  204. }
  205. }catch (Exception e){
  206. throw new RuntimeException("人员更换失败!");
  207. }
  208. }
  209. /**
  210. * 获取议题
  211. * @param topicId
  212. * @return
  213. */
  214. public static JSONObject getTopic(String topicId) throws Exception{
  215. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  216. + "api/v2/sessions/topics/"+topicId+"?topic_id="+topicId;
  217. try{
  218. String ret = HttpClientUtil.get(url,"utf-8");
  219. JSONObject obj = new JSONObject(ret);
  220. if(obj.getInt("status")==-1){
  221. throw new RuntimeException("获取议题失败!");
  222. }else{
  223. return obj;
  224. }
  225. }catch (Exception e){
  226. throw new RuntimeException("获取议题失败!");
  227. }
  228. }
  229. /**
  230. * 获取会话成员
  231. * @param sessionId
  232. * @return
  233. * @throws Exception
  234. */
  235. public static JSONArray getParticipants(String sessionId){
  236. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  237. + "api/v2/sessions/"+sessionId+"/participants?session_id="+sessionId;
  238. try{
  239. String ret = HttpClientUtil.get(url,"utf-8");
  240. return new JSONArray(ret);
  241. }catch (Exception e){
  242. throw new RuntimeException("获取议题失败!");
  243. }
  244. }
  245. /**
  246. * 获取会话成员
  247. * @param sessionId
  248. * @return
  249. * @throws Exception
  250. */
  251. public static JSONArray getSessions(String sessionId){
  252. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  253. + "api/v2/sessions/"+sessionId+"/participants?session_id="+sessionId;
  254. try{
  255. String ret = HttpClientUtil.get(url,"utf-8");
  256. return new JSONArray(ret);
  257. }catch (Exception e){
  258. throw new RuntimeException("获取议题失败!");
  259. }
  260. }
  261. public static final String SESSION_TYPE_MUC = "1";
  262. public static final String SESSION_TYPE_P2P = "2";
  263. public static final String SESSION_TYPE_GROUP = "3";
  264. public static final String SESSION_TYPE_SYSTEM = "0";
  265. public static final String SESSION_STATUS_PROCEEDINGS= "0";
  266. public static final String SESSION_STATUS_END= "1";
  267. }