All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hn.im.easemob.EasemobUtils Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.im.easemob;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hn.config.HnConfigUtils;
import com.hn.im.easemob.api.*;
import com.hn.im.easemob.comm.ClientContext;
import com.hn.im.easemob.comm.EasemobRestAPIFactory;
import com.hn.im.easemob.comm.body.*;
import com.hn.im.easemob.comm.wrapper.BodyWrapper;
import com.hn.im.easemob.comm.wrapper.ResponseWrapper;
import com.hn.im.easemob.exception.EasemobException;
import org.apache.http.conn.EofSensorInputStream;

import java.util.Map;

/**
 * 环信工具类
 */
public class EasemobUtils {

    private static final Log log = LogFactory.get();

    private static EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();

    /**
     * 用户体系
     */
    private static IMUserAPI user = (IMUserAPI) factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
    /**
     * 聊天记录
     */
    private static ChatMessageAPI chat = (ChatMessageAPI) factory.newInstance(EasemobRestAPIFactory.MESSAGE_CLASS);
    /**
     * 文件上传下载
     */
    private static FileAPI file = (FileAPI) factory.newInstance(EasemobRestAPIFactory.FILE_CLASS);
    /**
     * 发送消息
     */
    private static SendMessageAPI message = (SendMessageAPI) factory.newInstance(EasemobRestAPIFactory.SEND_MESSAGE_CLASS);
    /**
     * 群组管理
     */
    private static ChatGroupAPI chatgroup = (ChatGroupAPI) factory.newInstance(EasemobRestAPIFactory.CHATGROUP_CLASS);
    /**
     * 聊天室管理
     */
    private static ChatRoomAPI chatroom = (ChatRoomAPI) factory.newInstance(EasemobRestAPIFactory.CHATROOM_CLASS);

    /**
     * 环信用户Id后缀
     */
    public static String PROJECT_ID;

    static {
        String projectId = HnConfigUtils.getConfig("im.easemob.projectId");
        if (StrUtil.isNotBlank(projectId)) {
            PROJECT_ID = "-" + projectId;
        }
    }

    private static final Integer SUCCESS_CODE = 200;

    /**
     * 功能描述:注册用户信息
     *
     * @param userId   用户名
     * @param password 密码
     * @param nickName 昵称
     * @author yangliu  2016年7月26日 下午5:12:59
     */
    public static void createUser(String userId, String password, String nickName) {
        userId = userId + PROJECT_ID;
        BodyWrapper payload = new IMUserBody(userId, password, nickName);
        ResponseWrapper object = (ResponseWrapper) user.createNewIMUserBatch(payload);
        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("环信注册失败:" + object.getResponseStatus() + ",userId:" + userId);
            throw new EasemobException("用户注册失败");
        }
    }

    /**
     * 注释:获取用户登录状态
     * /{org_name}/{app_name}/users/{username}/status
     *
     * @param userId 用户ID
     * @return java.lang.String
     * @author nijian
     */
    public static String getUserStatus(String userId) {
        userId = userId + PROJECT_ID;
        ResponseWrapper object = (ResponseWrapper) user.getIMUserStatus(userId);
        JSONObject jsonObject = JSONUtil.parseObj(object.getResponseBody().toString());
        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("获取用户登录状态:" + object.getResponseStatus() + ",userId:" + userId);
            return "offline";
        }
        JSONObject data = jsonObject.getJSONObject("data");
        return data.getStr(userId);
    }

    /**
     * 修改环信昵称
     *
     * @param userId   用户id
     * @param nickName 昵称
     */
    public static void updateIMUserNickName(String userId, String nickName) {
        userId = userId + PROJECT_ID;
        BodyWrapper payload = new IMUserBody(userId, "123456", nickName);
        ResponseWrapper object = (ResponseWrapper) user.modifyIMUserNickNameWithAdminToken(userId, payload);
        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("环信昵称修改失败:" + object.getResponseStatus() + ",userId:" + userId);
            throw new EasemobException("昵称修改失败");
        }
    }


    /**
     * 功能描述:删除用户失败
     *
     * @param userNum 用户编号
     * @author yangliu  2016年7月26日 下午5:36:32
     */
    public static void deleteUser(String userNum) {
        userNum = userNum + PROJECT_ID;
        ResponseWrapper object = (ResponseWrapper) user.deleteIMUserByUserName(userNum);
        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("环信删除用户失败:" + object.getResponseStatus() + ",userName:" + userNum);
            throw new EasemobException("删除用户失败");
        }
    }

    /**
     * 强制下线
     *
     * @param userName 用户id
     */
    public static void disconnectIMUser(String userName) {
        userName = userName + PROJECT_ID;
        ResponseWrapper object = (ResponseWrapper) user.disconnectIMUser(userName);
        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("环信强制下线失败:" + object.getResponseStatus() + ",userName:" + userName);
            throw new EasemobException("环信强制下线失败");
        }
    }

    /**
     * 功能描述: 添加好友
     *
     * @param userNum   用户id
     * @param friendNum 好友id
     * @author yangliu  2016年7月26日 下午5:39:47
     */
    public static void addFriendSingle(String userNum, String friendNum) {
        userNum = userNum + PROJECT_ID;
        friendNum = friendNum + PROJECT_ID;
        ResponseWrapper object = (ResponseWrapper) user.addFriendSingle(userNum, friendNum);

        System.out.println(object.toString());
        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("环信添加好友:" + object.getResponseStatus() + ",userName:" + userNum + ",friendNum:" + friendNum);
            throw new EasemobException("添加好友失败");
        }
    }

    /**
     * 获取用户好友
     *
     * @param userNum 用户id
     * @return String String
     */
    public static String getFriends(String userNum) {
        userNum = userNum + PROJECT_ID;
        ResponseWrapper object = (ResponseWrapper) user.getFriends(userNum);

        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("环信获取好友:" + object.getResponseStatus() + ",userName:" + userNum);
            throw new EasemobException("获取好友失败");
        }

        JSONObject jsonObject = JSONUtil.parseObj(object.getResponseBody().toString());
        String data = jsonObject.get("data").toString();
        if (data == null || data == "") {
            return "";
        }
        return data.substring(1, data.length() - 1);
    }


    /**
     * 功能描述:删除好友
     *
     * @param userNum   用户ID
     * @param friendNum 好友ID
     * @author yangliu  2016年7月26日 下午5:39:47
     */
    public static void deleteFriendSingle(String userNum, String friendNum) {
        userNum = userNum + PROJECT_ID;
        friendNum = friendNum + PROJECT_ID;
        ResponseWrapper object = (ResponseWrapper) user.deleteFriendSingle(userNum, friendNum);
        if (!SUCCESS_CODE.equals(object.getResponseStatus())) {
            log.error("环信删除好友:" + object.getResponseStatus() + ",userName:" + userNum + ",friendNum:" + friendNum);
            throw new EasemobException("删除好友失败");
        }
    }

    /**
     * 往IM用户的黑名单中加人 
* POST * * @param userNum 用戶名或用戶ID * @param friendNums 好友ID {"usernames":["5cxhactgdj", "mh2kbjyop1"]} */ public static void addToBlackList(String userNum, String[] friendNums) { userNum = userNum + PROJECT_ID; for (int i = 0; i < friendNums.length; i++) { friendNums[i] = friendNums[i] + PROJECT_ID; } UserNamesBody payload = new UserNamesBody(friendNums); ResponseWrapper object = (ResponseWrapper) user.addToBlackList(userNum, payload); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信加入黑名单:" + object.getResponseStatus() + ",userName:" + userNum + ",friendNums:" + friendNums); throw new EasemobException("添加黑名单失败"); } } /** * 从IM用户的黑名单中减人
* DELETE * * @param userNum 用戶名或用戶ID * @param friendNum 黑名单用戶名或用戶ID */ public static void deleteFromBlackList(String userNum, String friendNum) { userNum = userNum + PROJECT_ID; friendNum = friendNum + PROJECT_ID; ResponseWrapper object = (ResponseWrapper) user.removeFromBlackList(userNum, friendNum); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信删除黑名单:" + object.getResponseStatus() + ",userName:" + userNum + ",friendNum:" + friendNum); throw new EasemobException("删除黑名单失败"); } } /** * 功能描述: 创建群组并返回群组编号 * * @param userNum 用户编号 * @param groupName 群名称 * @param otherUserNum 添加的其他组员编号 ["zhangsan","lisi"] * @return String * @author yangliu 2016年7月27日 上午9:34:22 */ public static String addGroup(String userNum, String groupName, String[] otherUserNum) { userNum = userNum + PROJECT_ID; if(otherUserNum!=null){ for (int i = 0; i < otherUserNum.length; i++) { otherUserNum[i] = otherUserNum[i] + PROJECT_ID; } } ChatGroupBody payload = new ChatGroupBody(groupName, groupName, false, 2000L, false, userNum, otherUserNum); ResponseWrapper object = (ResponseWrapper) chatgroup.createChatGroup(payload); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信创建群失败:" + object.getResponseStatus() + ",userName:" + userNum + ",groupName:" + groupName); throw new EasemobException(String.valueOf(object.getResponseBody())); } JSONObject jsonObject = JSONUtil.parseObj(object.getResponseBody().toString()); JSONObject data = jsonObject.getJSONObject("data"); return data.getStr("groupid"); } /** * 创建聊天室 * * @param userNum 用户名或id * @param roomName 聊天室名称 * @param otherUserNum 邀请用户id * @return String */ public static String addRoom(String userNum, String roomName, String[] otherUserNum) { userNum = userNum + PROJECT_ID; ChatRoomBody chatRoomBody = new ChatRoomBody(roomName, roomName, 5000L, userNum, otherUserNum); ResponseWrapper object = (ResponseWrapper) chatroom.createChatRoom(chatRoomBody); if (object.getResponseStatus() != 200) { log.error("环信创建聊天室失败:" + object.getResponseStatus() + ",userName" + userNum + ",roomName:" + roomName); throw new EasemobException(String.valueOf(object.getResponseBody())); } JSONObject jsonObject = JSONUtil.parseObj(object.getResponseBody().toString()); JSONObject data = jsonObject.getJSONObject("data"); return data.getStr("id"); } /** * 获取群详情 * * @param groupIds 群id */ public static void getChatGroupDetails(String[] groupIds) { ResponseWrapper object = (ResponseWrapper) chatgroup.getChatGroupDetails(groupIds); if (object.getResponseStatus() == 404) { log.error("环信获取群组详情失败:" + object.getResponseStatus() + ",groupId:" + groupIds); throw new EasemobException("环信获取群组详情失败"); } } /** * 获取一个聊天室详情 * * @param groupId 群id */ public static void getChatRoomDetails(String groupId) { ResponseWrapper object = (ResponseWrapper) chatroom.getChatRoomDetail(groupId); if (object.getResponseStatus() == 404) { log.error("环信获取聊天室详情失败:" + object.getResponseStatus() + ",groupId" + groupId); throw new EasemobException("环信获取聊天室详情失败"); } } /** * 功能描述: 修改群信息 * * @param groupNum 群编号 * @param groupName 群名称 * @author yangliu 2016年8月13日 下午5:14:35 */ public static void updateGroup(String groupNum, String groupName) { ModifyChatGroupBody groupBody = new ModifyChatGroupBody(groupName, groupName, 2000L); ResponseWrapper object = (ResponseWrapper) chatgroup.modifyChatGroup(groupNum, groupBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("修改群名片:" + object.getResponseStatus() + ",groupNum:" + groupNum + ",groupName:" + groupName); throw new EasemobException("修改群名片"); } } /** * 功能描述: 转让群组 * * @param groupNum 群编号 * @param userNum 群名称 * @author yangliu 2016年8月13日 下午5:14:35 */ public static void updateGroupOwner(String groupNum, String userNum) { userNum = userNum + PROJECT_ID; GroupOwnerTransferBody groupOwnerTransferBody = new GroupOwnerTransferBody(userNum); ResponseWrapper object = (ResponseWrapper) chatgroup.transferChatGroupOwner(groupNum, groupOwnerTransferBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("修改群名片:" + object.getResponseStatus() + ",groupNum:" + groupNum + ",userNum:" + userNum); throw new EasemobException("修改群名片"); } } /** * 修改聊天室信息 * * @param roomNum 聊天室ID * @param roomName 聊天室名称 */ public static void updateRoom(String roomNum, String roomName) { ModifyChatRoomBody roomBody = new ModifyChatRoomBody(roomName, roomName, 5000L); ResponseWrapper object = (ResponseWrapper) chatroom.modifyChatRoom(roomNum, roomBody); if (object.getResponseStatus() != 200) { log.error("修改聊天室名称:" + object.getResponseStatus() + ",roomNum:" + roomNum + ",roomName" + roomName); throw new EasemobException("修改聊天室名称"); } } /** * 功能描述:环信删除群组 * * @param groupId 群组ID * @author yangliu 2016年7月27日 上午10:12:10 */ public static void deleteGroup(String groupId) { ResponseWrapper object = (ResponseWrapper) chatgroup.deleteChatGroup(groupId); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信删除群组失败:" + object.getResponseStatus() + ",groupId:" + groupId); throw new EasemobException("删除群组失败"); } } /** * 功能描述: 群组 添加禁言 * * @param groupNum 群编号 * @param userNums 禁言用户 * @author yangliu 2016年8月13日 下午5:14:35 */ public static void addMute(String groupNum, String[] userNums) { for (int i = 0; i < userNums.length; i++) { userNums[i] = userNums[i] + PROJECT_ID; } ModifyChatGroupMuteBody modifyChatGroupMuteBody = new ModifyChatGroupMuteBody(userNums, -1000); ResponseWrapper object = (ResponseWrapper) chatgroup.mute(groupNum, modifyChatGroupMuteBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("修改群名片:" + object.getResponseStatus() + ",groupNum:" + groupNum + ",userNums:" + userNums); throw new EasemobException("修改群名片"); } } /** * 功能描述: 群组 移除禁言 * * @param groupNum 群编号 * @param userNums 移除禁言用户 * @author yangliu 2016年8月13日 下午5:14:35 */ public static void removeMute(String groupNum, String[] userNums) { for (int i = 0; i < userNums.length; i++) { userNums[i] = userNums[i] + PROJECT_ID; } ResponseWrapper object = (ResponseWrapper) chatgroup.removeMute(groupNum, userNums); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("修改群名片:" + object.getResponseStatus() + ",groupNum:" + groupNum + ",userNums:" + userNums); throw new EasemobException("修改群名片"); } } /** * 环信删除聊天室 * * @param groupId 聊天室ID */ public static void deleteRoom(String groupId) { ResponseWrapper object = (ResponseWrapper) chatroom.deleteChatRoom(groupId); if (object.getResponseStatus() != 200) { log.error("环信删除聊天室失败:" + object.getResponseStatus() + ",groupId:" + groupId); throw new EasemobException("删除聊天室失败"); } } /** * 功能描述: 给群组加人[单个] * * @param groupId 群组ID * @param userNum 用户id * @author yangliu 2016年7月27日 上午9:36:55 */ public static void addUserToGroup(String groupId, String userNum) { userNum = userNum + PROJECT_ID; ResponseWrapper object = (ResponseWrapper) chatgroup.addSingleUserToChatGroup(groupId, userNum); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给群组加人失败:" + object.getResponseStatus() + ",userName:" + userNum + ",groupId:" + groupId); throw new EasemobException("群组群组加人失败"); } else { log.info("环信群组加人成功!"); } } /** * 给聊天室添加人 单个 * * @param userNum 用户ID * @param groupId 聊天室ID */ public static void addUserToRoom(String userNum, String groupId) { userNum = userNum + PROJECT_ID; ResponseWrapper object = (ResponseWrapper) chatroom.addSingleUserToChatRoom(groupId, userNum); if (object.getResponseStatus() != 200) { log.error("环信给聊天室加人失败:" + object.getResponseStatus() + ",userName" + userNum + ",groupId:" + groupId); throw new EasemobException("环信聊天室加人失败"); } else { log.info("环信聊天室加人成功!"); } } /** * 功能描述:给群组加人[多个] * * @param userNums 用户集合 * @param groupId 群组id * @author yangliu 2016年7月27日 上午9:59:50 */ public static void addUserToGroup(String[] userNums, String groupId) { if (userNums == null || userNums.length == 0) { return; } for (int i = 0; i < userNums.length; i++) { String userNum = userNums[i]; userNums[i] = userNum + PROJECT_ID; } UserNamesBody userNames = new UserNamesBody(userNums); ResponseWrapper object = (ResponseWrapper) chatgroup.addBatchUsersToChatGroup(groupId, userNames); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给群组添加好友失败:" + object.getResponseStatus() + ",userName:" + JSONUtil.toJsonStr(userNames) + ",groupId:" + groupId); throw new EasemobException("选择用户不符合要求"); } } /** * 给聊天室加多个人 * * @param userNums 用户集合 * @param groupId 聊天室ID */ public static void addUserToRoom(String[] userNums, String groupId) { if (userNums == null) { return; } for (int i = 0; i < userNums.length; i++) { String userNum = userNums[i]; userNums[i] = userNum + PROJECT_ID; } UserNamesBody userNames = new UserNamesBody(userNums); ResponseWrapper object = (ResponseWrapper) chatroom.addBatchUsersToChatRoom(groupId, userNames); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给聊天室添加好友失败:" + object.getResponseStatus() + ",userName:" + JSONUtil.toJsonStr(userNames) + ",groupId:" + groupId); throw new EasemobException("选择用户不符合要求"); } } /** * 功能描述: 把用户从群组里面删除 * * @param userNum 用户编号 * @param groupId 群组ID * @author yangliu 2016年7月27日 上午10:04:11 */ public static void deleteUserFromGroup(String userNum, String groupId) { userNum = userNum + PROJECT_ID; ResponseWrapper object = (ResponseWrapper) chatgroup.removeSingleUserFromChatGroup(groupId, userNum); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信删除群组成员失败:" + object.getResponseStatus() + ",userName:" + userNum + ",groupId:" + groupId); throw new EasemobException("删除群组成员失败"); } } /** * 把用户从聊天室删除 * * @param userNum 用户ID * @param groupId 群组ID */ public static void deleteUserFromRoom(String userNum, String groupId) { userNum = userNum + PROJECT_ID; ResponseWrapper object = (ResponseWrapper) chatroom.removeSingleUserFromChatRoom(groupId, userNum); if (object.getResponseStatus() != 200) { log.error("环信删除聊天室成员失败:" + object.getResponseStatus() + ",userName:" + userNum + ",groupId:" + groupId); throw new EasemobException("删除聊天室成员失败"); } } /** * 功能描述: 把用户从群组里面删除[多个] * * @param userNum 用户编号 * @param groupId 群组ID * @author yangliu 2016年7月27日 上午10:04:11 */ public static void deleteUserFromGroup(String[] userNum, String groupId) { ResponseWrapper object = (ResponseWrapper) chatgroup.removeBatchUsersFromChatGroup(groupId, userNum); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信删除群组成员批量失败:" + object.getResponseStatus() + ",userName:" + userNum + ",groupId:" + groupId); throw new EasemobException("删除群组成员批量失败"); } } /** * 把用户从聊天室删除[多个] * * @param userNums 用户集合 * @param groupId 群组ID */ public static void deleteUserFromRoom(String[] userNums, String groupId) { ResponseWrapper object = (ResponseWrapper) chatroom.removeBatchUsersFromChatRoom(groupId, userNums); if (object.getResponseStatus() != 200) { log.error("环信删除聊天室成员批量失败:" + object.getResponseStatus() + ",userName:" + userNums + ",groupId:" + groupId); throw new EasemobException("删除聊天室成员批量失败"); } } /** * 功能描述:给用户发文本消息[单个] * * @param userNum 发送者 环信如果不存在,客户端则显示userNum名称 * @param msg 文本 * @param toUser 用户[单个] * @author yangliu 2016年7月27日 上午11:20:46 */ public static void sendUserTextMessage(String userNum, String msg, String toUser) { userNum = userNum + PROJECT_ID; toUser = toUser + PROJECT_ID; TextMessageBody msgBody = new TextMessageBody("users", new String[]{toUser}, userNum, null, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给用户发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("用户发送消息失败"); } } /** * 功能描述:给用户发文本消息 带扩展字段的 * * @param userNum 发送者 * @param msg 文本 * @param toUser 用户[单个] * @param ext 扩展字段 * @author yangliu 2016年7月27日 上午11:20:46 */ public static void sendUserTextMessage(String userNum, String msg, String toUser, Map ext) { toUser = toUser + PROJECT_ID; userNum = userNum + PROJECT_ID; TextMessageBody msgBody = new TextMessageBody("users", new String[]{toUser}, userNum, ext, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给用户发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("用户发送消息失败"); } } /** * 给用户发送透传消息【单个】 * * @param userNum 发送者 * @param msg 文本 * @param toUser 用户[单个] * @param ext 扩展 */ public static void sendUserCmdMessage(String userNum, String msg, String toUser, Map ext) { toUser = toUser + PROJECT_ID; CmdMessageBody msgBody = new CmdMessageBody("users", new String[]{toUser}, userNum, ext, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给用户发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("用户发送消息失败"); } } /** * 功能描述:给用户发文本消息[多个] * * @param userNum 发送者 * @param msg 文本 * @param toUsers 用户[多个] * @author yangliu 2016年7月27日 上午11:20:46 */ public static void sendUserTextMessage(String userNum, String msg, String[] toUsers) { TextMessageBody msgBody = new TextMessageBody("users", toUsers, userNum, null, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给用户发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("用户发送消息失败"); } } public static void sendUserTextMessage(String userNum, String msg, String[] toUsers, Map ext) { TextMessageBody msgBody = new TextMessageBody("users", toUsers, userNum, ext, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给用户发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("用户发送消息失败"); } } /** * 功能描述:给群发文本消息[单个] * * @param userNum 发送者 * @param msg 文本 * @param groupId 用户[单个] * @author yangliu 2016年7月27日 上午11:20:46 */ public static void sendGroupTextMessage(String userNum, String msg, String groupId) { userNum = userNum + PROJECT_ID; TextMessageBody msgBody = new TextMessageBody("chatgroups", new String[]{groupId}, userNum, null, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给用户发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("群发送消息失败"); } } /** * 功能描述:给群发文本消息[单个] * * @param userNum 发送者 * @param msg 文本 * @param groupId 用户[单个] * @param ext 扩展字段 * @author yangliu 2016年7月27日 上午11:20:46 */ public static void sendGroupTextMessage(String userNum, String msg, String groupId, Map ext) { ext.put("message", msg); userNum = userNum + PROJECT_ID; TextMessageBody msgBody = new TextMessageBody("chatrooms", new String[]{groupId}, userNum, ext, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给用户发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("群发送消息失败"); } } /** * 功能描述:给群发文本消息[多个] * * @param userNum 发送者 * @param msg 文本 * @param groupIds 用户[多个] * @author yangliu 2016年7月27日 上午11:20:46 */ public static void sendGroupTextMessage(String userNum, String msg, String[] groupIds) { TextMessageBody msgBody = new TextMessageBody("chatgroups", groupIds, userNum, null, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给群组发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("批量群组发送消息失败"); } } /** * 功能描述:给群发文本消息[多个] * * @param userNum 发送者 * @param msg 文本 * @param groupIds 用户[多个] * @param ext 扩展字段 * @author yangliu 2016年7月27日 上午11:20:46 */ public static void sendGroupTextMessage(String userNum, String msg, String[] groupIds, Map ext) { TextMessageBody msgBody = new TextMessageBody("chatrooms", groupIds, userNum, ext, msg); ResponseWrapper object = (ResponseWrapper) message.sendMessage(msgBody); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信给群组发送消息失败:" + object.getResponseStatus() + ",userName:" + userNum + ",msg:" + msg); throw new EasemobException("批量群组发送消息失败"); } } /** * 功能描述:获取消息聊天记录 * * @param timestamp 时间 * @return JSONArray */ public static JSONArray getChatMessages(long timestamp) { String sql = String.format("timestamp>%s", timestamp); ResponseWrapper object = (ResponseWrapper) chat.exportChatMessages(1000L, "", sql); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信获取消息失败" + object.getResponseStatus() + ",timestamp:" + timestamp); throw new EasemobException("获取消息失败"); } JSONArray jsonObject = JSONUtil.parseObj(object.getResponseBody().toString()).getJSONArray("entities"); System.out.println(jsonObject.toString()); return jsonObject; } /** * 功能描述:下载文件 * * @param fileUUID 文件唯一ID * @param shareSecret 文件访问秘钥,从上传Response-entities-share-secret中获取 * @param isThumbnail 如果下载图片,是否为缩略图 * @return EofSensorInputStream * @author yangliu 2016年8月10日 上午10:49:57 */ public static EofSensorInputStream loadFile(String fileUUID, String shareSecret, Boolean isThumbnail) { ResponseWrapper object = (ResponseWrapper) file.downloadFile(fileUUID, shareSecret, isThumbnail); if (!SUCCESS_CODE.equals(object.getResponseStatus())) { log.error("环信下载文件失败" + object.getResponseStatus() + ",fileUUID:" + fileUUID + ",shareSecret:" + shareSecret); throw new EasemobException("下载文件失败"); } EofSensorInputStream eof = (EofSensorInputStream) object.getResponseBody(); return eof; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy