ImUtill.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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) {
  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. 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 from,String fromName, String topicId, String contentType, String content) {
  34. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  35. + ("api/v2/sessions/topic/"+topicId+"/messages");
  36. JSONObject params = new JSONObject();
  37. params.put("sender_id", from);
  38. params.put("sender_name", fromName);
  39. params.put("content_type", contentType);
  40. params.put("content", content);
  41. params.put("topic_id", topicId);;
  42. String response = HttpClientUtil.postBody(url, params);
  43. return response;
  44. }
  45. /**
  46. * 结束议题
  47. *
  48. * @param topicId 议题ID
  49. * @param endUser 结束人
  50. * @param endUserName 结束人名字
  51. * @param sessionId 会话ID
  52. */
  53. public static JSONObject endTopics(String sessionId,String endUser, String endUserName,String topicId) {
  54. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+sessionId+"/topics/"+topicId+"/ended";
  55. JSONObject params = new JSONObject();
  56. params.put("session_id", sessionId);
  57. params.put("end_user", endUser);
  58. params.put("end_user_name",endUserName);
  59. params.put("topic_id", topicId);
  60. String ret = HttpClientUtil.postBody(imAddr,params);
  61. JSONObject obj = null;
  62. try{
  63. obj = new JSONObject(ret);
  64. }catch (Exception e){
  65. return null;
  66. }
  67. return obj;
  68. }
  69. /**
  70. * 议题邀请人员
  71. * @param user 结束人名字
  72. * @param sessionId 会话ID
  73. */
  74. public static void updateTopicUser(String sessionId,String user) {
  75. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+sessionId+"/participants/"+user;
  76. JSONObject params = new JSONObject();
  77. params.put("user", user+":"+0);
  78. HttpClientUtil.putBody(imAddr,params);
  79. }
  80. /**
  81. * 创建议题
  82. *
  83. * @param topicId 议题ID
  84. * @param topicName 议题名称
  85. * @param participants 成员
  86. */
  87. public static JSONObject createTopics(String sessionId, String topicId, String topicName, JSONObject participants, JSONObject messages, String sessionType) {
  88. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+topicId+"/topics";
  89. JSONObject params = new JSONObject();
  90. params.put("topic_id", topicId);
  91. params.put("topic_name", topicName);
  92. params.put("participants", participants.toString());
  93. params.put("messages", messages.toString());
  94. params.put("session_id", sessionId);
  95. params.put("session_type", sessionType);
  96. String ret = HttpClientUtil.postBody(imAddr,params);
  97. JSONObject obj = null;
  98. try{
  99. obj = new JSONObject(ret);
  100. }catch (Exception e){
  101. return null;
  102. }
  103. return obj;
  104. }
  105. /**
  106. * 创建会话(system)
  107. *
  108. */
  109. public static JSONObject createSession(JSONObject participants,String sessionType,String sessionName,String sessionId) {
  110. String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions";
  111. JSONObject params = new JSONObject();
  112. params.put("participants", participants);
  113. params.put("session_name", sessionName);
  114. params.put("session_type", sessionType);
  115. params.put("session_id", sessionId);
  116. String ret = HttpClientUtil.postBody(imAddr,params);
  117. JSONObject obj = null;
  118. try{
  119. obj = new JSONObject(ret);
  120. }catch (Exception e){
  121. return null;
  122. }
  123. return obj;
  124. }
  125. /**
  126. * 获取会话实例的消息对象
  127. * @param senderId
  128. * @param senderName
  129. * @param title
  130. * @param description
  131. * @param images
  132. * @return
  133. */
  134. public static JSONObject getCreateTopicMessage(String senderId,String senderName,String title,String description,String images){
  135. JSONObject messages = new JSONObject();
  136. messages.put("description",description);
  137. messages.put("title",title);
  138. messages.put("img",images);
  139. messages.put("sender_id",senderId);
  140. messages.put("sender_name",senderName);
  141. return messages;
  142. }
  143. public static JSONArray getTopicMessage(String topicId,String startMsgId,String endMsgId,int page,int pagesize,String uid){
  144. String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get")
  145. + "api/v2/sessions/topic/"+topicId+"/messages?topic_id="+topicId+"&end="+startMsgId
  146. +"&start="+(endMsgId==null?"":endMsgId)+"&page="+page+"&pagesize="+pagesize+"&user="+uid;
  147. try{
  148. String ret = HttpClientUtil.get(url, "UTF-8");
  149. JSONObject obj = new JSONObject(ret);
  150. if(obj.getInt("status")==-1){
  151. throw new RuntimeException(obj.getString("message"));
  152. }else{
  153. return obj.getJSONArray("data");
  154. }
  155. }catch (Exception e){
  156. return null;
  157. }
  158. }
  159. }