ImUtill.java 7.5 KB

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