ImUtill.java 6.1 KB

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