Java对接腾讯IM即时通话

一:注册腾讯IM账号

基本配置 - 即时通信 IM - 控制台 (tencent.com)

获得必要的参数:SDKAppIDkey(密钥)

 二:参考管饭API文档进行操作:

即时通信 IM 单发单聊消息-服务端 API-文档中心-腾讯云 (tencent.com)

 三:java中应用:

依赖:


<!--        腾讯云即时对话pom-->
        <dependency>
            <groupId>com.github.tencentyun</groupId>
            <artifactId>tls-sig-api-v2</artifactId>
            <version>2.0</version>
        </dependency>

yml配置:

#腾讯云IM即时通话配置
IMConfig:
  sdkAppId: 140??????
  secretKey: 59c826???你自己的

HttpUtils封装

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtil {
    private static final CloseableHttpClient httpclient = HttpClients.createDefault();

    /**
     * 发送HttpGet请求
     * @param url
     * @return
     */
    public static String sendGet(String url) {

        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 发送HttpPost请求,参数为map
     * @param url
     * @param map
     * @return
     */
    public static String sendPost(String url, Map<String, String> map) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity1 = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发送不带参数的HttpPost请求
     * @param url
     * @return
     */
    public static String sendPost(String url) {
        HttpPost httppost = new HttpPost(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String doPost2(String url, JSONObject param) {
        HttpPost httpPost = null;
        String result = null;
        try {
            HttpClient client = new DefaultHttpClient();
            httpPost = new HttpPost(url);
            if (param != null) {
                StringEntity se = new StringEntity(param.toString(), "utf-8");
                httpPost.setEntity(se); // post方法中,加入json数据
                httpPost.setHeader("Content-Type", "application/json");
                httpPost.setHeader("Authorization", param.getString("authorization"));
            }

            HttpResponse response = client.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}

Redis封装:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisServiceUtil {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 写入缓存设置时效时间
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 设置指定键的过期时间
     * @param key 键
     * @param expireTime 过期时间
     * @param timeUnit 时间单位
     * @return true:成功
     */
    public boolean setExpire(final String key, Long expireTime, TimeUnit timeUnit) {
        return redisTemplate.expire(key, expireTime, timeUnit);
    }

    /**
     * 批量删除对应的value
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 删除对应的value
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }
    /**
     * 判断缓存中是否有对应的value
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
    /**
     * 读取缓存
     * @param key
     * @return
     */
    public <T> T get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        if (null == result) {
            return null;
        }
        return (T)result;
    }
    /**
     * 哈希 添加
     * @param key
     * @param hashKey
     * @param value
     */
    public void hmSet(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key,hashKey,value);
    }

    /**
     * 以map集合的形式添加键值对
     * @param key 键
     * @param map Map
     */
    public void hPutAll(String key, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     * 哈希获取数据
     * @param key
     * @param hashKey
     * @return
     */
    public Object hmGet(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key,hashKey);
    }

    /**
     * 根据键获取到redis中存储的map
     * @param key 键
     * @return 键对应的map
     */
    public Map<String, Object> hGetAll(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 自增/自减key对应map中的指定字段的long型数值
     * @param key 键
     * @param field key对应map中存储的字段
     * @param increment 正数-自增;负数-自减
     * @return 自增/自减后的数值
     */
    public Long hashIncrBy(String key, Object field, long increment) {
        return redisTemplate.opsForHash().increment(key, field, increment);
    }

    /**
     * 自增/自减key对应map中的指定字段的double型数值
     * @param key 键
     * @param field key对应map中存储的字段
     * @param delta 正数-自增;负数-自减
     * @return 自增/自减后的数值
     */
    public Double hIncrByDouble(String key, Object field, double delta) {
        return redisTemplate.opsForHash().increment(key, field, delta);
    }

    /**
     * 查询redis中指定字段是否存在
     * @param key 键
     * @param field key对应map中存储的字段
     * @return true:存在
     */
    public boolean hashExists(String key, String field) {
        return redisTemplate.opsForHash().hasKey(key, field);
    }

    /**
     * 列表添加
     * @param k
     * @param v
     */
    public void lPush(String k, Object v) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(k,v);
    }

    /**
     * 列表获取
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<Object> lRange(String k, long l, long l1) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k,l,l1);
    }

    /**
     * 集合添加
     * @param key
     * @param value
     */
    public void add(String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key,value);
    }

    /**
     * 集合获取
     * @param key
     * @return
     */
    public Set<Object> setMembers(String key) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 有序集合添加
     * @param key
     * @param value
     * @param scoure
     */
    public void zAdd(String key, Object value, double scoure) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key,value,scoure);
    }

    /**
     * 有序集合获取
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }

    /**
     * redis原子型自增
     * */
    public Long incr(String key){
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        Long increment = entityIdCounter.getAndIncrement();
        return increment;
    }
}

Api接口封装:

package com.furnish.web.wn.enums.txim;


/**
 * @description: 腾讯im相关API接口
 */
public class TencentCloudImApiConstant {
    /**
     * 账号管理
     */
    public static class AccountManage {
        /**导入单个帐号*/
        public static final String ACCOUNT_IMPORT = "v4/im_open_login_svc/account_import";
        /**导入多个帐号*/
        public static final String MULTI_ACCOUNT_IMPORT = "v4/im_open_login_svc/account_import";
        /**删除帐号*/
        public static final String ACCOUNT_DELETE = "v4/im_open_login_svc/account_delete";
        /**查询帐号*/
        public static final String ACCOUNT_CHECK = "v4/im_open_login_svc/account_check";
        /**失效帐号登录状态*/
        public static final String ACCOUNT_KICK = "v4/im_open_login_svc/kick";
        /**查询账号在线状态*/
        public static final String ACCOUNT_QUERY_STATE = "v4/openim/querystate";
    }

    /**
     * 单聊消息
     */
    public static class SingleChatManage {
        /**单发单聊消息*/
        public static final String SEND_MSG = "v4/openim/sendmsg";
        /**批量发单聊消息*/
        public static final String BATCH_SEND_MSG = "v4/openim/batchsendmsg";
        /**导入单聊消息*/
        public static final String IMPORT_MSG = "v4/openim/importmsg";
        /**查询单聊消息*/
        public static final String ADMIN_GET_ROAM_MSG = "v4/openim/admin_getroammsg";
        /**撤回单聊消息*/
        public static final String ADMIN_MSG_WITH_DRAW = "v4/openim/admin_msgwithdraw";
        /**设置单聊消息已读*/
        public static final String ADMIN_SET_MSG_READ = "v4/openim/admin_set_msg_read";
    }

    /**
     * 全员推送
     */
    public static class AllPushManage {
        /**全员推送*/
        public static final String IM_PUSH = "v4/all_member_push/im_push";
        /**设置应用属性名称*/
        public static final String IM_SET_ATTR_NAME = "v4/all_member_push/im_set_attr_name";
        /**获取应用属性名称*/
        public static final String IM_GET_ATTR_NAME = "v4/all_member_push/im_get_attr_name";
        /**获取用户属性*/
        public static final String IM_GET_ATTR = "v4/all_member_push/im_get_attr";
        /**设置用户属性*/
        public static final String IM_SET_ATTR = "v4/all_member_push/im_set_attr";
        /**删除用户属性*/
        public static final String IM_REMOVE_ATTR = "v4/all_member_push/im_remove_attr";
        /**获取用户标签*/
        public static final String IM_GET_TAG = "v4/all_member_push/im_get_tag";
        /**添加用户标签*/
        public static final String IM_ADD_TAG = "v4/all_member_push/im_add_tag";
        /**删除用户标签*/
        public static final String IM_REMOVE_TAG = "v4/all_member_push/im_remove_tag";
        /**删除用户所有标签*/
        public static final String IM_REMOVE_ALL_TAGS = "v4/all_member_push/im_remove_all_tags";
    }

    /**
     * 资料管理
     */
    public static class PortraitManage {
        /**设置资料*/
        public static final String PORTRAIT_SET = "v4/profile/portrait_set";
        /**拉取资料*/
        public static final String PORTRAIT_GET = "v4/profile/portrait_get";
    }

    /**
     * 关系链管理
     */
    public static class RelationManage {
        /**添加好友*/
        public static final String FRIEND_ADD = "v4/sns/friend_add";
        /**导入好友*/
        public static final String FRIEND_IMPORT = "v4/sns/friend_import";
        /**更新好友*/
        public static final String FRIEND_UPDATE = "v4/sns/friend_update";
        /**删除好友*/
        public static final String FRIEND_DELETE = "v4/sns/friend_delete";
        /**删除所有好友*/
        public static final String FRIEND_DELETE_ALL = "v4/sns/friend_delete_all";
        /**校验好友*/
        public static final String FRIEND_CHECK = "v4/sns/friend_check";
        /**拉取好友*/
        public static final String FRIEND_GET = "v4/sns/friend_get";
        /**拉取指定好友*/
        public static final String FRIEND_GET_LIST = "v4/sns/friend_get_list";
        /**添加黑名单*/
        public static final String BLACK_LIST_ADD = "v4/sns/black_list_add";
        /**删除黑名单*/
        public static final String BLACK_LIST_DELETE = "v4/sns/black_list_delete";
        /**拉取黑名单*/
        public static final String BLACK_LIST_GET = "v4/sns/black_list_get";
        /**校验黑名单*/
        public static final String BLACK_LIST_CHECK = "v4/sns/black_list_check";
        /**添加分组*/
        public static final String GROUP_ADD = "v4/sns/group_add";
        /**删除分组*/
        public static final String GROUP_DELETE = "v4/sns/group_delete";
        /**拉取分组*/
        public static final String GROUP_GET = "v4/sns/group_get";
    }

    /**
     * 群组管理
     */
    public static class GroupManage {
        /**创建群组*/
        public static final String CREATE_GROUP = "v4/group_open_http_svc/create_group";
        /**获取群详细资料*/
        public static final String GET_GROUP_INFO = "v4/group_open_http_svc/get_group_info";
        /**获取群成员详细资料*/
        public static final String GET_GROUP_MEMBER_INFO = "v4/group_open_http_svc/get_group_member_info";
        /**修改群基础资料*/
        public static final String MODIFY_GROUP_BASE_INFO = "v4/group_open_http_svc/modify_group_base_info";
        /**增加群成员*/
        public static final String ADD_GROUP_MEMBER = "v4/group_open_http_svc/add_group_member";
        /**删除群成员*/
        public static final String DELETE_GROUP_MEMBER = "v4/group_open_http_svc/delete_group_member";
        /**修改群成员资料*/
        public static final String MODIFY_GROUP_MEMBER_INFO = "v4/group_open_http_svc/modify_group_member_info";
        /**解散群组*/
        public static final String DESTROY_GROUP = "v4/group_open_http_svc/destroy_group";
        /**获取用户所加入的群组*/
        public static final String GET_JOINED_GROUP_LIST = "v4/group_open_http_svc/get_joined_group_list";
        /**查询用户在群组中的身份*/
        public static final String GET_ROLE_IN_GROUP = "v4/group_open_http_svc/get_role_in_group";
        /**批量禁言和取消禁言*/
        public static final String FORBID_SEND_MSG = "v4/group_open_http_svc/forbid_send_msg";
        /**获取被禁言群成员列表*/
        public static final String GET_GROUP_SHUT_UIN = "v4/group_open_http_svc/get_group_shutted_uin";
        /**在群组中发送普通消息*/
        public static final String SEND_GROUP_MSG = "v4/group_open_http_svc/send_group_msg";
        /**在群组中发送系统通知*/
        public static final String SEND_GROUP_SYSTEM_NOTIFICATION = "v4/group_open_http_svc/send_group_system_notification";
        /**撤回群消息*/
        public static final String GROUP_MSG_RECALL = "v4/group_open_http_svc/group_msg_recall";
        /**转让群主*/
        public static final String CHANGE_GROUP_OWNER = "v4/group_open_http_svc/change_group_owner";
        /**导入群基础资料*/
        public static final String IMPORT_GROUP = "v4/group_open_http_svc/import_group";
        /**导入群消息*/
        public static final String IMPORT_GROUP_MSG = "v4/group_open_http_svc/import_group_msg";
        /**导入群成员*/
        public static final String IMPORT_GROUP_MEMBER = "v4/group_open_http_svc/import_group_member";
        /**设置成员未读消息计数*/
        public static final String SET_UNREAD_MSG_NUM = "v4/group_open_http_svc/set_unread_msg_num";
        /**删除指定用户发送的消息*/
        public static final String DELETE_GROUP_MSG_BY_SENDER = "v4/group_open_http_svc/delete_group_msg_by_sender";
        /**拉取群历史消息*/
        public static final String GROUP_MSG_GET_SIMPLE = "v4/group_open_http_svc/group_msg_get_simple";
        /**获取直播群在线人数*/
        public static final String GET_ONLINE_MEMBER_NUM = "v4/group_open_http_svc/get_online_member_num";
    }

    /**
     * 全局禁言管理
     */
    public static class AllSinentManage {
        /**设置全局禁言*/
        public static final String SET_NO_SPEAKING = "v4/openconfigsvr/setnospeaking";
        /**查询全局禁言*/
        public static final String GET_NO_SPEAKING = "v4/openconfigsvr/getnospeaking";
    }

    /**
     * 运营管理
     */
    public static class OperationManage {
        /**拉取运营数据*/
        public static final String GET_APP_INFO = "v4/openconfigsvr/getappinfo";
        /**下载消息记录*/
        public static final String GET_HISTORY = "v4/open_msg_svc/get_history";
        /**获取服务器 IP 地址*/
        public static final String GET_IP_LIST = "v4/ConfigSvc/GetIPList";
    }
}

腾讯Im相关常量:


/**
 * @description: 腾讯im相关常量
 */
public class TencentCloudImConstant {
    /**
     * IM请求处理结果
     */
    public final static String ACTION_STATUS_OK = "OK";
    public final static String ACTION_STATUS_FAIL = "FAIL";

    /**
     * IM发消息是否同步到发送方(1-同步,2-不同步)
     */
    public final static Integer SYNC_OTHER_MACHINE_YES = 1;
    public final static Integer SYNC_OTHER_MACHINE_NO = 2;

    /**
     * IM消息对象类型:
     * TIMTextElem	文本消息。
     * TIMLocationElem	地理位置消息。
     * TIMFaceElem	表情消息。
     * TIMCustomElem	自定义消息,当接收方为 iOS 系统且应用处在后台时,此消息类型可携带除文本以外的字段到 APNs。一条组合消息中只能包含一个 TIMCustomElem 自定义消息元素。
     * TIMSoundElem	语音消息。
     * TIMImageElem	图像消息。
     * TIMFileElem	文件消息。
     * TIMVideoFileElem	视频消息。
     */
    public final static String TIM_TEXT_ELEM = "TIMTextElem";
    public final static String TIM_LOCATION_ELEM = "TIMLocationElem";
    public final static String TIM_FACE_ELEM = "TIMFaceElem";
    public final static String TIM_CUSTOM_ELEM = "TIMCustomElem";
    public final static String TIM_SOUND_ELEM = "TIMSoundElem";
    public final static String TIM_IMAGE_ELEM = "TIMImageElem";
    public final static String TIM_FILE_ELEM = "TIMFileElem";
    public final static String TIM_VIDEOFILE_ELEM = "TIMVideoFileElem";

    /**
     * 微信响应消息类型
     * WX_MSG_TYPE_EVENT:事件类型,事件类型对应"user_enter_tempsession"表示建立会话
     * WX_MSG_TYPE_TEXT:文本类型
     * WX_MSG_TYPE_TEXT:图片类型
     * WX_MSG_TYPE_TEXT:小程序卡片
     */
    public final static String WX_MSG_TYPE_EVENT = "event";
    public final static String WX_MSG_TYPE_TEXT = "text";
    public final static String WX_MSG_TYPE_IMAGE = "image";
    public final static String WX_MSG_TYPE_APPLET_CARD = "miniprogrampage";
}

功能配置:

import com.alibaba.fastjson.JSONObject;
import com.tencentyun.TLSSigAPIv2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @description: 腾讯im基础配置
 * @author: zyb
 * @date: 2021/5/27 17:32
 */
@Slf4j
@Component
public class TencentCloudImUtil {
    private static final String HTTPS_URL_PREFIX = "https://console.tim.qq.com/";
    private static final String APP_MANAGER = "administrator";
    private static final String REDIS_IM_USER_SIG = "silence:im_user_sig:";

    @Value("${IMConfig.sdkAppId}")
    private long sdkAppId;
    @Value("${IMConfig.secretKey}")
    private String secretKey;

    @Autowired
    private RedisServiceUtil redisServiceUtil;

    /**
     * 获取腾讯云用户签名
     */
    public String getTxCloudUserSig() {
        String userSig = redisServiceUtil.get(REDIS_IM_USER_SIG + APP_MANAGER);
        if (StringUtils.isEmpty(userSig)) {
            TLSSigAPIv2 tlsSigApi = new TLSSigAPIv2(sdkAppId, secretKey);
            userSig = tlsSigApi.genUserSig(APP_MANAGER, 86400);
            redisServiceUtil.set(REDIS_IM_USER_SIG + APP_MANAGER, userSig, 86400L);
        }
        return userSig;
    }

    /**
     * 获取腾讯im请求路径
     */
    private String getHttpsUrl(String imServiceApi, Integer random) {
        return String.format("%s%s?sdkappid=%s&identifier=%s&usersig=%s&random=%s&contenttype=json",
                HTTPS_URL_PREFIX, imServiceApi, sdkAppId, APP_MANAGER, this.getTxCloudUserSig(), random);
    }

    /**
     * 导入单个账号
     * @param userId 用户id
     */
    public void accountImport(String userId) {
        accountImport(userId, null);
    }

    public void accountImport(String userId, String userName) {
        accountImport(userId, userName, null);
    }

    public void accountImport(String userId, String userName, String faceUrl) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_IMPORT, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Identifier", userId);
        if (StringUtils.isNotEmpty(userName)) {
            jsonObject.put("Nick", userName);
        }
        if (StringUtils.isNotEmpty(faceUrl)) {
            jsonObject.put("FaceUrl", faceUrl);
        }
        log.info("腾讯云im导入单个账号,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im导入单个账号,返回结果:{}", result);
    }

    /**
     * 导入多个账号
     * @param userIds 用户id集合
     */
    public void multiAccountImport(List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.MULTI_ACCOUNT_IMPORT, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Accounts", userIds);
        log.info("腾讯云im导入多个账号,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im导入单个账户,返回结果:{}", result);
    }

    /**
     * 删除账号
     * @param userIds 用户id集合
     */
    public void accountDelete(List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_DELETE, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("DeleteItem", getUserIdJsonList(userIds));
        log.info("腾讯云im删除账号,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im删除账户,返回结果:{}", result);
    }

    /**
     * 查询账号是否已经导入im
     * @param userIds 用户id集合
     */
    public String accountCheck(List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_CHECK, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("CheckItem", getUserIdJsonList(userIds));
        log.info("腾讯云im查询账号,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im查询账号,返回结果:{}", result);
        return result;
    }

    private List<JSONObject> getUserIdJsonList(List<String> userIds) {
        return userIds.stream().map(v -> {
            JSONObject userIdJson = new JSONObject();
            userIdJson.put("UserID", v);
            return userIdJson;
        }).collect(Collectors.toList());
    }

    /**
     * 单发单聊消息
     * @param syncOtherMachine 是否同步消息到发送方(1-同步,2-不同步)
     * @param fromUserId 发送方用户id
     * @param toUserId 接收方用户id
     * @param msgType 消息对象类型
     * @param msgContent 消息内容
     */
    public String sendMsg(Integer syncOtherMachine, String fromUserId, String toUserId, String msgType, String msgContent) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.SEND_MSG, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SyncOtherMachine", syncOtherMachine);
        if (StringUtils.isNotEmpty(fromUserId)) {
            // 发送方不为空表示指定发送用户,为空表示为管理员发送消息
            jsonObject.put("From_Account", fromUserId);
        }
        jsonObject.put("To_Account", toUserId);
        jsonObject.put("MsgRandom", random);
        List<JSONObject> msgBody = getMsgBody(msgType, msgContent);
        jsonObject.put("MsgBody", msgBody);
        log.info("腾讯云im单发单聊消息,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im单发单聊消息,返回结果:{}", result);
        return result;
    }

    /**
     * 批量发单聊消息
     * @param syncOtherMachine 是否同步消息到发送方(1-同步,2-不同步)
     * @param fromUserId 发送方用户id
     * @param toUserIds 接收方用户id集合
     * @param msgType 消息对象类型
     * @param msgContent 消息内容
     */
    public String batchSendMsg(Integer syncOtherMachine, String fromUserId, List<String> toUserIds, String msgType, String msgContent) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.BATCH_SEND_MSG, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SyncOtherMachine", syncOtherMachine);
        if (StringUtils.isNotEmpty(fromUserId)) {
            // 发送方不为空表示指定发送用户,为空表示为管理员发送消息
            jsonObject.put("From_Account", fromUserId);
        }
        jsonObject.put("To_Account", toUserIds);
        jsonObject.put("MsgRandom", random);
        List<JSONObject> msgBody = getMsgBody(msgType, msgContent);
        jsonObject.put("MsgBody", msgBody);
        log.info("腾讯云im批量发单聊消息,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im批量发单聊消息,返回结果:{}", result);
        return result;
    }


    /**
     * 拼接发送消息内容
     * @param msgType 消息类型
     * @param msgContent 发送消息内容
     * @return 消息内容
     */
    private List<JSONObject> getMsgBody(String msgType, String msgContent) {
        List<JSONObject> msgBody = new ArrayList<>();
        if (msgType.equals(TencentCloudImConstant.TIM_TEXT_ELEM)) {
            // 文本类型
            JSONObject msgBodyJson = new JSONObject();
            msgBodyJson.put("MsgType", msgType);
            JSONObject msgContentObj = new JSONObject();
            msgContentObj.put("Text", msgContent);
            msgBodyJson.put("MsgContent", msgContentObj);
            msgBody.add(msgBodyJson);
        }
        return msgBody;
    }

    /**
     * 查询单聊消息
     * @param fromUserId 发送方用户id
     * @param toUserId 接收方用户id
     * @param maxCnt 查询条数
     * @param startTime 起始时间(单位:秒)
     * @param endTime 结束时间(单位:秒)
     * @param lastMsgKey 最后一条消息的 MsgKey
     * @return 单聊消息列表
     */
    public String adminGetRoamMsg(String fromUserId, String toUserId, Integer maxCnt, Long startTime, Long endTime, String lastMsgKey) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.ADMIN_GET_ROAM_MSG, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("From_Account", fromUserId);
        jsonObject.put("To_Account", toUserId);
        jsonObject.put("MaxCnt", maxCnt);
        jsonObject.put("MinTime", startTime);
        jsonObject.put("MaxTime", endTime);
        if (StringUtils.isNotEmpty(lastMsgKey)){
            jsonObject.put("LastMsgKey", lastMsgKey);
        }
        log.info("腾讯云im查询单聊消息,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im查询单聊消息,返回结果:{}", result);
        return result;
    }

    /**
     * 撤回单聊消息
     * @param fromUserId 发送方用户id
     * @param toUserId 接收方用户id
     * @param msgKey MsgKey
     */
    public void adminMsgWithDraw(String fromUserId, String toUserId, String msgKey) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.ADMIN_MSG_WITH_DRAW, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("From_Account", fromUserId);
        jsonObject.put("To_Account", toUserId);
        jsonObject.put("MsgKey", msgKey);
        log.info("腾讯云im撤回单聊消息,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im撤回单聊消息,返回结果:{}", result);
    }

    /**
     * 设置单聊消息已读
     * @param reportUserId 读取消息的用户
     * @param peerUserId 发送消息的用户
     */
    public void adminSetMsgRead(String reportUserId, String peerUserId) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.ADMIN_SET_MSG_READ, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Report_Account", reportUserId);
        jsonObject.put("Peer_Account", peerUserId);
        log.info("腾讯云im设置单聊消息已读,请求参数:{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im设置单聊消息已读,返回结果:{}", result);
    }
}

接口调用:

import com.furnish.web.wn.enums.txim.TencentCloudImUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/wnapp")
@Api(tags = "腾讯IM实时对话")
public class TXController {

    @Autowired
    private TencentCloudImUtil tencentCloudImUtil;

    @GetMapping("/contextLoads")
    @ApiOperation("获取userSig")
    public String contextLoads() {
        String userSig = tencentCloudImUtil.getTxCloudUserSig();
        System.out.println(userSig);
        return userSig;
    }

    /**
     * 查询单聊消息
     * @param fromUserId 发送方用户id
     * @param toUserId 接收方用户id
     * @param maxCnt 查询条数
     * @param startTime 起始时间(单位:秒)
     * @param endTime 结束时间(单位:秒)
     * @param lastMsgKey 最后一条消息的 MsgKey  可以为null
     * @return 单聊消息列表
     */
    @GetMapping("/testSendIMMsg")
    @ApiOperation("查询单聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromUserId", value = "发送方用户id", dataType = "String", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "toUserId", value = "接收方用户id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "maxCnt", value = "查询条数", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "startTime", value = "起始时间(单位:秒)", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "endTime", value = "结束时间(单位:秒)", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "lastMsgKey", value = "最后一条消息的 MsgKey  可以为null", dataType = "String", dataTypeClass = String.class)
    })
    public String testSendIMMsg(String fromUserId,String toUserId,Integer maxCnt,Long startTime,Long endTime,String lastMsgKey){
        Calendar cale = Calendar.getInstance();
        cale.setTime(new Date());
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
//        Date beginTime = cale.getTime();
        String s = tencentCloudImUtil.adminGetRoamMsg(fromUserId, toUserId, maxCnt, startTime, endTime, lastMsgKey);
        return s;
    }

    /**
     * 查询账号是否已经导入im
     * @param userIds 用户id集合
     */
    @GetMapping("/testAccount")
    @ApiOperation("查询账号是否已经导入im")
    @ApiImplicitParam(name = "userIds",value = "用户id集合")
    public String testAccount(List<String> userIds){

        String accountCheck = tencentCloudImUtil.accountCheck(userIds);
        System.out.println(accountCheck);
        return accountCheck;
    }

    /**
     * 单发单聊消息
     * @param syncOtherMachine 是否同步消息到发送方(1-同步,2-不同步)
     * @param fromUserId 发送方用户id
     * @param toUserId 接收方用户id
     * @param msgType 消息对象类型
     * @param msgContent 消息内容
     */
    @GetMapping("/sendMsg")
    @ApiOperation("单发单聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "syncOtherMachine", value = "是否同步消息到发送方(1-同步,2-不同步)", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "fromUserId", value = "发送方用户id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "toUserId", value = "接收方用户id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "msgType", value = "消息对象类型", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "msgContent", value = "消息内容", dataType = "String", dataTypeClass = String.class)
    })
    public String sendMsg(Integer syncOtherMachine,String fromUserId,String toUserId,String msgType,String msgContent){

        String sendMsg = tencentCloudImUtil.sendMsg(syncOtherMachine,fromUserId,toUserId,msgType,msgContent);
        System.out.println(sendMsg);
        return sendMsg;
    }

    /**
     * 撤回单聊消息
     * 发送方用户id
     * 接收方用户id
     * MsgKey
     */
    @GetMapping("/adminMsgWithDraw")
    @ApiOperation("撤回单聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromUserId", value = "发送方用户id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "toUserId", value = "接收方用户id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "msgKey", value = "当前信息key", dataType = "String", dataTypeClass = String.class)
    })
    public void adminMsgWithDraw(String fromUserId,String toUserId,String msgKey){
        tencentCloudImUtil.adminMsgWithDraw(fromUserId, toUserId, msgKey);
    }

    /**
     * 设置单聊消息已读
     * @param reportUserId 读取消息的用户
     * @param peerUserId 发送消息的用户
     */
    @GetMapping("/adminSetMsgRead")
    @ApiOperation("设置单聊消息已读")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportUserId", value = "读取消息的用户", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "peerUserId", value = "发送消息的用户", dataType = "String", dataTypeClass = String.class)
    })
    public void adminSetMsgRead(String reportUserId,String peerUserId){
        tencentCloudImUtil.adminSetMsgRead(reportUserId,peerUserId);
    }

    /**
     * 删除账号
     * @param userIds  账号集合
     */
    @GetMapping("/accountDelete")
    @ApiOperation("删除账号")
    @ApiImplicitParam(name = "userIds",value = "账号集合")
    public void accountDelete(List<String> userIds){
        tencentCloudImUtil.accountDelete(userIds);

    }
}

猜你喜欢

转载自blog.csdn.net/Java_Mr_Jin/article/details/126870142
今日推荐