package com.yihu.wlyy.util; import org.json.JSONArray; import org.json.JSONObject; /** * Created by 卓 on 2017/1/13. */ public class ImUtill { /** * 发送消息给IM * * @param from 来自 * @param contentType 1文字 2图片消息 * @param content 内容 */ public static String sendImMsg(String from,String fromName, String sessionId, String contentType, String content,String businessType) { String imAddr = SystemConf.getInstance().getImListGet() + "/api/v2/sessions/"+sessionId+"/messages"; JSONObject params = new JSONObject(); params.put("sender_id", from); params.put("sender_name", fromName); params.put("content_type", contentType); params.put("content", content); params.put("session_id", sessionId); params.put("business_type", businessType); String response = HttpClientUtil.postBody(imAddr, params); return response; } /** * 更新会话状态 * * @param sessionId 会话ID * @param status 状态 */ public static String updateSessionStatus(String sessionId,String status) { String imAddr = SystemConf.getInstance().getImListGet() + "/api/v2/sessions/"+sessionId+"/status?status="+status+"&sessionId="+sessionId; JSONObject params = new JSONObject(); String response = HttpClientUtil.postBody(imAddr, params); return response; } /** * 发送消息给IM * * @param from 来自 * @param contentType 1文字 2图片消息 * @param content 内容 */ public static String sendTopicIM(String from,String fromName, String topicId, String contentType, String content) { String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get") + ("api/v2/sessions/topic/"+topicId+"/messages"); JSONObject params = new JSONObject(); params.put("sender_id", from); params.put("sender_name", fromName); params.put("content_type", contentType); params.put("content", content); params.put("topic_id", topicId);; String response = HttpClientUtil.postBody(url, params); return response; } /** * 结束议题 * * @param topicId 议题ID * @param endUser 结束人 * @param endUserName 结束人名字 * @param sessionId 会话ID */ public static JSONObject endTopics(String sessionId,String endUser, String endUserName,String topicId) { String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+sessionId+"/topics/"+topicId+"/ended"; JSONObject params = new JSONObject(); params.put("session_id", sessionId); params.put("end_user", endUser); params.put("end_user_name",endUserName); params.put("topic_id", topicId); String ret = HttpClientUtil.postBody(imAddr,params); JSONObject obj = null; try{ obj = new JSONObject(ret); }catch (Exception e){ return null; } return obj; } /** * 议题邀请人员 * @param user 结束人名字 * @param sessionId 会话ID */ public static void updateTopicUser(String sessionId,String user) { String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+sessionId+"/participants/"+user; JSONObject params = new JSONObject(); params.put("user", user+":"+0); HttpClientUtil.putBody(imAddr,params); } /** * 创建议题 * * @param topicId 议题ID * @param topicName 议题名称 * @param participants 成员 */ public static JSONObject createTopics(String sessionId, String topicId, String topicName, JSONObject participants, JSONObject messages, String sessionType) { String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions/"+topicId+"/topics"; JSONObject params = new JSONObject(); params.put("topic_id", topicId); params.put("topic_name", topicName); params.put("participants", participants.toString()); params.put("messages", messages.toString()); params.put("session_id", sessionId); params.put("session_type", sessionType); String ret = HttpClientUtil.postBody(imAddr,params); JSONObject obj = null; try{ obj = new JSONObject(ret); }catch (Exception e){ return null; } return obj; } /** * 创建会话(system) * */ public static JSONObject createSession(JSONObject participants,String sessionType,String sessionName,String sessionId) { String imAddr = SystemConf.getInstance().getImListGet() + "api/v2/sessions"; JSONObject params = new JSONObject(); params.put("participants", participants.toString()); params.put("session_name", sessionName); params.put("session_type", sessionType); params.put("session_id", sessionId); String ret = HttpClientUtil.postBody(imAddr,params); JSONObject obj = null; try{ obj = new JSONObject(ret); }catch (Exception e){ return null; } return obj; } /** * 获取会话实例的消息对象 * @param senderId * @param senderName * @param title * @param description * @param images * @return */ public static JSONObject getCreateTopicMessage(String senderId,String senderName,String title,String description,String images){ JSONObject messages = new JSONObject(); messages.put("description",description); messages.put("title",title); messages.put("img",images); messages.put("sender_id",senderId); messages.put("sender_name",senderName); return messages; } public static JSONObject getTopicMessage(String topicId,String startMsgId,String endMsgId,int page,int pagesize,String uid){ String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get") + "api/v2/sessions/topic/"+topicId+"/messages?topic_id="+topicId+"&end="+startMsgId +"&start="+(endMsgId==null?"":endMsgId)+"&page="+page+"&pagesize="+pagesize+"&user="+uid; try{ String ret = HttpClientUtil.get(url, "UTF-8"); JSONObject obj = new JSONObject(ret); if(obj.getInt("status")==-1){ throw new RuntimeException(obj.getString("message")); }else{ return obj.getJSONObject("data"); } }catch (Exception e){ return null; } } public static JSONArray getSessionMessage(String sessionId,String startMsgId,String endMsgId,int page,int pagesize,String uid){ String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get") + "api/v2/sessions/"+sessionId+"/messages?session_id="+sessionId+"&user="+uid+"&start_message_id="+startMsgId+"&end_message_id="+endMsgId+"&page="+page+"&pagesize="+pagesize; try{ String ret = HttpClientUtil.get(url, "UTF-8"); JSONArray obj = new JSONArray(ret); return obj; }catch (Exception e){ return null; } } /** * 删除对应的成员信息在MUC模式中 * @param userId * @param oldUserId * @param sessionId * @return */ public static JSONObject deleteMucUser(String userId,String oldUserId,String sessionId) throws Exception{ String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get") + "api/v2/sessions/"+sessionId+"/participant/update"; try{ JSONObject params = new JSONObject(); params.put("user_id", userId); params.put("old_user_id", oldUserId); params.put("session_id", sessionId); String ret = HttpClientUtil.postBody(url,params); JSONObject obj = new JSONObject(ret); if(obj.getInt("status")==-1){ throw new RuntimeException("人员更换失败!"); }else{ return obj; } }catch (Exception e){ throw new RuntimeException("人员更换失败!"); } } /** * 获取议题 * @param topicId * @return */ public static JSONObject getTopic(String topicId) throws Exception{ String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get") + "api/v2/sessions/topics/"+topicId+"?topic_id="+topicId; try{ String ret = HttpClientUtil.get(url,"utf-8"); JSONObject obj = new JSONObject(ret); if(obj.getInt("status")==-1){ throw new RuntimeException("获取议题失败!"); }else{ return obj; } }catch (Exception e){ throw new RuntimeException("获取议题失败!"); } } /** * 获取会话成员 * @param sessionId * @return * @throws Exception */ public static JSONArray getParticipants(String sessionId){ String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get") + "api/v2/sessions/"+sessionId+"/participants?session_id="+sessionId; try{ String ret = HttpClientUtil.get(url,"utf-8"); return new JSONArray(ret); }catch (Exception e){ throw new RuntimeException("获取议题失败!"); } } /** * 获取会话成员 * @param sessionId * @return * @throws Exception */ public static JSONArray getSessions(String sessionId){ String url = SystemConf.getInstance().getSystemProperties().getProperty("im_list_get") + "api/v2/sessions/"+sessionId+"/participants?session_id="+sessionId; try{ String ret = HttpClientUtil.get(url,"utf-8"); return new JSONArray(ret); }catch (Exception e){ throw new RuntimeException("获取议题失败!"); } } public static final String SESSION_TYPE_MUC = "1"; public static final String SESSION_TYPE_P2P = "2"; public static final String SESSION_TYPE_GROUP = "3"; public static final String SESSION_TYPE_SYSTEM = "0"; public static final String SESSION_STATUS_PROCEEDINGS= "0"; public static final String SESSION_STATUS_END= "1"; }