├── src ├── test │ ├── resources │ │ └── config.test.example.yml │ └── java │ │ └── com │ │ └── sevlow │ │ └── sdk │ │ └── tim │ │ └── api │ │ ├── test │ │ └── TestModule.java │ │ └── impl │ │ ├── TIMServiceImplTest.java │ │ ├── TIMOnlineStatusServiceImplTest.java │ │ ├── TIMAccountServiceImplTest.java │ │ ├── TIMProfileServiceImplTest.java │ │ ├── TIMChatServiceImplTest.java │ │ ├── TIMGroupServiceImplTest.java │ │ └── TIMRelationServiceImplTest.java └── main │ └── java │ └── com │ └── sevlow │ └── sdk │ └── tim │ ├── api │ ├── TIMNoSpeakService.java │ ├── TIMDirtyWordService.java │ ├── TIMOperationalService.java │ ├── TIMOnlineStatusService.java │ ├── TIMAccountService.java │ ├── impl │ │ ├── TIMOnlineStatusServiceImpl.java │ │ ├── TIMAccountServiceImpl.java │ │ ├── TIMProfileServiceImpl.java │ │ ├── TIMChatServiceImpl.java │ │ ├── TIMGroupServiceImpl.java │ │ ├── TIMServiceImpl.java │ │ └── TIMRelationServiceImpl.java │ ├── TIMService.java │ ├── TIMChatService.java │ ├── TIMProfileService.java │ ├── TIMGroupService.java │ └── TIMRelationService.java │ ├── bean │ ├── group │ │ ├── SilenceEnum.java │ │ ├── CreateGroupResult.java │ │ ├── MemberAccount.java │ │ ├── TIMTextElem.java │ │ ├── AddGroupResult.java │ │ ├── AllGroupResult.java │ │ ├── ShuttedMemberResult.java │ │ ├── ImportMember.java │ │ ├── GroupMemberInfo.java │ │ └── GroupInfo.java │ ├── DeleteGroupsResult.java │ ├── ImportFriendsResult.java │ ├── UpdateFriendsResult.java │ ├── DeleteFriendsResult.java │ ├── RemoveBlockAccountsResult.java │ ├── chat │ │ ├── ChatMsgEnum.java │ │ ├── MsgBody.java │ │ ├── MsgContent.java │ │ └── MsgCustomContent.java │ ├── AddFriendsResult.java │ ├── AddBlockAccountsResult.java │ ├── ResultItem.java │ ├── relation │ │ ├── ListFriendsDirectivityResult.java │ │ ├── UserDataItem.java │ │ ├── UserDataInfoItem.java │ │ ├── InfoItemDetail.java │ │ ├── DeleteAllResult.java │ │ ├── TIMFriendProfile.java │ │ └── ListFriendsResult.java │ ├── AddGroupsResult.java │ ├── ResultStruct.java │ ├── CheckFriendsResult.java │ ├── SnsItem.java │ ├── profile │ │ ├── AdminForbidTypeEnum.java │ │ ├── UserProfileResult.java │ │ ├── AllowTypeEnum.java │ │ ├── GenderEnum.java │ │ ├── UserProfileItem.java │ │ └── TIMProfile.java │ ├── ImportAccountsResult.java │ ├── BatchUpdateResult.java │ ├── OnlineStatusResult.java │ ├── account │ │ ├── TIMAccount.java │ │ └── TIMFriend.java │ ├── ListBlockAccountsResult.java │ └── CheckBlockAccountsResult.java │ ├── utils │ └── JsonUtils.java │ ├── common │ ├── error │ │ ├── TIMException.java │ │ └── TIMError.java │ ├── Base64Url.java │ └── TLSSigature.java │ ├── config │ └── TIMConfig.java │ └── constant │ └── TIMErrorConstant.java ├── .gitignore ├── README.md ├── pom.xml └── LICENSE /src/test/resources/config.test.example.yml: -------------------------------------------------------------------------------- 1 | appId: 应用SDKAPPID 2 | adminIdentifier: 管理员Identifier 3 | accountType: accountType 4 | privateKeyPath: privateKey文件绝对路径,如果放在resource内加上classpath:项目相对路径 -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMNoSpeakService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | /** 4 | * @author Element 5 | * 6 | * 禁言管理 7 | * API Doc : https://cloud.tencent.com/document/product/269/4228 8 | */ 9 | public interface TIMNoSpeakService { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMDirtyWordService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | /** 4 | * @author Element 5 | * 6 | * 脏字管理 7 | * API Doc : https://cloud.tencent.com/document/product/269/2395 8 | */ 9 | public interface TIMDirtyWordService { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMOperationalService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | /** 4 | * @author Element 5 | * 6 | * 运营数据 7 | * API Doc : https://cloud.tencent.com/document/product/269/4192 8 | */ 9 | public interface TIMOperationalService { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/SilenceEnum.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import lombok.Getter; 4 | 5 | /** 6 | * @author pengshiqing 7 | * @Date: 2019/11/19 8 | * @Description: 9 | */ 10 | @Getter 11 | public enum SilenceEnum { 12 | 13 | QUIET(1), 14 | NOTICE(0) 15 | ; 16 | private Integer type ; 17 | 18 | SilenceEnum(Integer type) { 19 | this.type = type; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/DeleteGroupsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | /** 7 | * @author Element 8 | * @Package com.sevlow.sdk.tim.bean 9 | * @date 2019-05-27 23:05 10 | * @Description: 11 | */ 12 | @Data 13 | public class DeleteGroupsResult { 14 | 15 | @SerializedName("CurrentSequence") 16 | private Integer currentSequence ; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/ImportFriendsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author Element 9 | * @Package com.sevlow.sdk.tim.bean 10 | * @date 2019-05-27 19:40 11 | * @Description: 12 | */ 13 | @Data 14 | public class ImportFriendsResult extends BatchUpdateResult implements Serializable { 15 | private static final long serialVersionUID = 5046326641740563329L; 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/UpdateFriendsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author Element 9 | * @Package com.sevlow.sdk.tim.bean 10 | * @date 2019-05-28 00:12 11 | * @Description: TODO 12 | */ 13 | @Data 14 | public class UpdateFriendsResult extends BatchUpdateResult implements Serializable { 15 | 16 | private static final long serialVersionUID = 1084310377363123264L; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/DeleteFriendsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author Element 10 | * @Package com.sevlow.sdk.tim.bean 11 | * @date 2019-05-27 19:40 12 | * @Description: 13 | */ 14 | @Data 15 | public class DeleteFriendsResult { 16 | 17 | 18 | @SerializedName("ResultItem") 19 | private List resultItems; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/RemoveBlockAccountsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author Element 9 | * @Package com.sevlow.sdk.tim.bean 10 | * @date 2019-05-27 19:40 11 | * @Description: TODO 12 | */ 13 | 14 | @Data 15 | public class RemoveBlockAccountsResult extends BatchUpdateResult implements Serializable { 16 | 17 | private static final long serialVersionUID = -1090220142249303974L; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/chat/ChatMsgEnum.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.chat; 2 | 3 | import lombok.Getter; 4 | 5 | /** 6 | * @author pengshiqing 7 | * @Date: 2019/7/26 8 | * @Description: 9 | */ 10 | 11 | @Getter 12 | public enum ChatMsgEnum { 13 | 14 | /** 15 | * 消息同步发送 16 | */ 17 | SYNC(1), 18 | 19 | /** 20 | * 消息不同步 21 | */ 22 | NO_SYNC(2) 23 | 24 | ; 25 | 26 | private Integer type ; 27 | 28 | ChatMsgEnum(Integer type){ 29 | this.type = type ; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/CreateGroupResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author pengshiqing 10 | * @Date: 2019/11/19 11 | * @Description: 12 | */ 13 | @Data 14 | public class CreateGroupResult implements Serializable { 15 | 16 | private static final long serialVersionUID = 2869190403582320201L; 17 | 18 | @SerializedName("GroupId") 19 | private String groupId ; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/AddFriendsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import lombok.Data; 4 | import lombok.EqualsAndHashCode; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author Element 10 | * @Package com.sevlow.sdk.tim.bean 11 | * @date 2019-05-27 18:04 12 | * @Description: 13 | */ 14 | @Data 15 | @EqualsAndHashCode(callSuper = false) 16 | public class AddFriendsResult extends BatchUpdateResult implements Serializable { 17 | 18 | private static final long serialVersionUID = 8152624238915124107L; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/AddBlockAccountsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import lombok.Data; 4 | import lombok.EqualsAndHashCode; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author Element 10 | * @Package com.sevlow.sdk.tim.bean 11 | * @date 2019-05-27 19:40 12 | * @Description: 13 | */ 14 | @Data 15 | @EqualsAndHashCode(callSuper = false) 16 | public class AddBlockAccountsResult extends BatchUpdateResult implements Serializable { 17 | 18 | private static final long serialVersionUID = -8119104657667342684L; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/ResultItem.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author Element 10 | * @Package com.sevlow.sdk.tim.bean 11 | * @date 2019-05-27 18:08 12 | * @Description: TODO 13 | */ 14 | @Data 15 | public class ResultItem extends ResultStruct implements Serializable { 16 | 17 | private static final long serialVersionUID = -1825495492551857861L; 18 | 19 | @SerializedName("To_Account") 20 | private String account; 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/relation/ListFriendsDirectivityResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.relation; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * 指向性拉取好友结果 11 | * 12 | * @author element 13 | */ 14 | @Data 15 | public class ListFriendsDirectivityResult implements Serializable { 16 | 17 | private static final long serialVersionUID = -2636511198234645476L; 18 | 19 | @SerializedName("InfoItem") 20 | private List infoItems; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/AddGroupsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author Element 10 | * @Package com.sevlow.sdk.tim.bean 11 | * @date 2019-05-27 23:05 12 | * @Description: 13 | */ 14 | 15 | @Data 16 | public class AddGroupsResult extends BatchUpdateResult implements Serializable { 17 | private static final long serialVersionUID = -5437234846342240878L; 18 | 19 | @SerializedName("CurrentSequence") 20 | private int currentSequence; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/chat/MsgBody.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.chat; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author pengshiqing 10 | * @Date: 2019/7/16 11 | * @Description: 12 | */ 13 | @Data 14 | public class MsgBody implements Serializable { 15 | 16 | private static final long serialVersionUID = -3683204615133585051L; 17 | 18 | @SerializedName("MsgType") 19 | private String msgType ; 20 | 21 | @SerializedName("MsgContent") 22 | private Object msgContent ; 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/utils/JsonUtils.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.utils; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.GsonBuilder; 5 | 6 | /** 7 | * @author Element 8 | * @Package com.sevlow.sdk.tim.utils 9 | * @date 2019-05-27 15:08 10 | * @Description: TODO 11 | */ 12 | public class JsonUtils { 13 | 14 | private final static Gson GSON = new GsonBuilder().create(); 15 | 16 | public static String toJson(Object obj){ 17 | return GSON.toJson(obj); 18 | } 19 | 20 | public static T fromJson(String json,Class clazz){ 21 | return GSON.fromJson(json, clazz); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/ResultStruct.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author Element 10 | * @Package com.sevlow.sdk.tim.bean 11 | * @date 2019-05-28 00:21 12 | * @Description: TODO 13 | */ 14 | @Data 15 | public class ResultStruct implements Serializable { 16 | private static final long serialVersionUID = 353393257485002138L; 17 | 18 | @SerializedName("ResultCode") 19 | private int resultCode; 20 | 21 | @SerializedName("ResultInfo") 22 | private String resultInfo; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/CheckFriendsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import com.sevlow.sdk.tim.bean.relation.InfoItemDetail; 5 | import lombok.Data; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author Element 11 | * @Package com.sevlow.sdk.tim.bean 12 | * @date 2019-05-27 19:40 13 | * @Description: 14 | */ 15 | @Data 16 | public class CheckFriendsResult { 17 | 18 | @SerializedName("InfoItem") 19 | private List infoItems ; 20 | 21 | @SerializedName("Fail_Account") 22 | private List failAccounts ; 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/chat/MsgContent.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.chat; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author pengshiqing 12 | * @Date: 2019/7/16 13 | * @Description: 14 | */ 15 | 16 | @Data 17 | @NoArgsConstructor 18 | @AllArgsConstructor 19 | public class MsgContent implements Serializable { 20 | 21 | private static final long serialVersionUID = 8874434318533463826L; 22 | 23 | @SerializedName("Text") 24 | private String text ; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/MemberAccount.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author pengshiqing 10 | * @Date: 2019/11/19 11 | * @Description: 12 | */ 13 | @Data 14 | public class MemberAccount implements Serializable { 15 | 16 | private static final long serialVersionUID = -6127906010511506336L; 17 | 18 | @SerializedName("Member_Account") 19 | private String memberAccount ; 20 | 21 | public MemberAccount(String memberAccount) { 22 | this.memberAccount = memberAccount; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/relation/UserDataItem.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.relation; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import com.sevlow.sdk.tim.bean.SnsItem; 5 | import lombok.Data; 6 | 7 | import java.io.Serializable; 8 | import java.util.List; 9 | 10 | /** 11 | * 好友对象都包含一个 To_Account 字段和一个 ValueItem 数组 12 | * @author element 13 | */ 14 | @Data 15 | public class UserDataItem implements Serializable { 16 | 17 | private static final long serialVersionUID = -5954814814829812163L; 18 | 19 | @SerializedName("To_Account") 20 | private String account; 21 | 22 | @SerializedName("ValueItem") 23 | private List valueItems; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/TIMTextElem.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author pengshiqing 11 | * @Date: 2019/11/19 12 | * @Description: 13 | */ 14 | @Data 15 | public class TIMTextElem { 16 | 17 | @SerializedName("MsgType") 18 | private String msgType ; 19 | 20 | @SerializedName("MsgContent") 21 | private Map msgContent = new HashMap<>(1); 22 | 23 | public TIMTextElem(String message) { 24 | this.msgType = "TIMTextElem"; 25 | msgContent.put("Text",message); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/SnsItem.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author Element 12 | * @Package com.sevlow.sdk.tim.bean 13 | * @date 2019-05-28 00:11 14 | * @Description: 15 | */ 16 | @Data 17 | @NoArgsConstructor 18 | @AllArgsConstructor 19 | public class SnsItem implements Serializable { 20 | private static final long serialVersionUID = -2164515049612843515L; 21 | 22 | @SerializedName("Tag") 23 | private String tag; 24 | 25 | @SerializedName("Value") 26 | private T value; 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/relation/UserDataInfoItem.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.relation; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import com.sevlow.sdk.tim.bean.ResultStruct; 5 | import com.sevlow.sdk.tim.bean.SnsItem; 6 | import lombok.Data; 7 | 8 | import java.io.Serializable; 9 | import java.util.List; 10 | 11 | /** 12 | * @author element 13 | */ 14 | @Data 15 | public class UserDataInfoItem extends ResultStruct implements Serializable { 16 | 17 | private static final long serialVersionUID = 5720197350739860347L; 18 | 19 | @SerializedName("To_Account") 20 | private String account; 21 | 22 | @SerializedName("SnsProfileItem") 23 | private List snsProfileItems; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMOnlineStatusService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | import com.sevlow.sdk.tim.bean.OnlineStatusResult; 4 | import com.sevlow.sdk.tim.common.error.TIMException; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author Element 10 | * 11 | * 在线状态 12 | * API Doc : https://cloud.tencent.com/document/product/269/2565 13 | */ 14 | public interface TIMOnlineStatusService { 15 | 16 | /** 17 | * 获取用户在线状态 18 | * 19 | * 获取用户当前的登录状态。 20 | * 21 | * API Doc : https://cloud.tencent.com/document/product/269/2566 22 | * @param accounts 23 | * @return 24 | * @throws TIMException 25 | */ 26 | OnlineStatusResult queryState(List accounts) throws TIMException; 27 | 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/profile/AdminForbidTypeEnum.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.profile; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author element 7 | */ 8 | 9 | public enum AdminForbidTypeEnum { 10 | 11 | /** 12 | * 默认值,允许加好友 13 | */ 14 | @SerializedName("AdminForbid_Type_None") 15 | NONE("AdminForbid_Type_None"), 16 | 17 | /** 18 | * 禁止该用户发起加好友请求 19 | */ 20 | @SerializedName("AdminForbid_Type_SendOut") 21 | SEND_OUT("AdminForbid_Type_SendOut"); 22 | 23 | private String type; 24 | private AdminForbidTypeEnum(String type){ 25 | this.type = type; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return this.type; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/relation/InfoItemDetail.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.relation; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author pengshiqing 10 | * @Date: 2019/6/3 11 | * @Description: 12 | */ 13 | 14 | @Data 15 | public class InfoItemDetail implements Serializable { 16 | 17 | private static final long serialVersionUID = -6532938731305216895L; 18 | 19 | @SerializedName("To_Account") 20 | private String toAccount ; 21 | 22 | @SerializedName("Relation") 23 | private String relation ; 24 | 25 | @SerializedName("ResultCode") 26 | private Integer resultCode ; 27 | 28 | @SerializedName("ResultInfo") 29 | private String resultInfo ; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/ImportAccountsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * @author Element 12 | * @Package com.sevlow.sdk.tim.bean 13 | * @date 2019-05-27 19:44 14 | * @Description: 15 | */ 16 | @Data 17 | public class ImportAccountsResult implements Serializable { 18 | 19 | @SerializedName("ResultItem") 20 | private List resultItems = new ArrayList<>(); 21 | 22 | @SerializedName("Fail_Account") 23 | private List failAccounts = new ArrayList<>(); 24 | 25 | @SerializedName("Invalid_Account") 26 | private List invalidaccount = new ArrayList<>(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/profile/UserProfileResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.profile; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import com.sevlow.sdk.tim.bean.ResultStruct; 5 | import lombok.Data; 6 | 7 | import java.io.Serializable; 8 | import java.util.List; 9 | 10 | /** 11 | * @author element 12 | */ 13 | @Data 14 | public class UserProfileResult extends ResultStruct implements Serializable { 15 | 16 | private static final long serialVersionUID = -2616197016212501604L; 17 | 18 | @SerializedName("UserProfileItem") 19 | private List userProfileItems; 20 | 21 | @SerializedName("Fail_Account") 22 | private List failAccounts; 23 | 24 | @SerializedName("ActionStatus") 25 | private String actionStatus; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/common/error/TIMException.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.common.error; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @author Element 7 | * @Package com.sevlow.sdk.tim.common.error 8 | * @date 2019-05-27 11:45 9 | * @Description: TODO 10 | */ 11 | public class TIMException extends Exception implements Serializable { 12 | 13 | private static final long serialVersionUID = 3469414966034151505L; 14 | 15 | private TIMError error; 16 | 17 | public TIMException(TIMError error) { 18 | super(error.toString()); 19 | this.error = error; 20 | } 21 | 22 | public TIMException(TIMError error, Throwable cause) { 23 | super(error.toString(), cause); 24 | this.error = error; 25 | } 26 | 27 | public TIMError getError() { 28 | return this.error; 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/config/TIMConfig.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.config; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.NonNull; 7 | 8 | /** 9 | * @author Element 10 | * @Package com.sevlow.sdk.tim.config 11 | * @date 2019-05-27 10:21 12 | * @Description: 13 | */ 14 | @Data 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class TIMConfig { 18 | 19 | /** 20 | * 腾讯IM appId 21 | */ 22 | @NonNull 23 | private Long appId; 24 | 25 | /** 26 | * privateKey 文件地址(基于Project的相对目录地址) 27 | * 如果在包内需要加上classpath: 28 | */ 29 | @NonNull 30 | private String privateKeyPath; 31 | 32 | @NonNull 33 | private String adminIdentifier; 34 | 35 | private Long accountType; 36 | 37 | private int reqMaxRetry = 3; 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/relation/DeleteAllResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.relation; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author pengshiqing 10 | * @Date: 2019/6/3 11 | * @Description: 关系链管理-删除所有 12 | */ 13 | @Data 14 | public class DeleteAllResult implements Serializable { 15 | 16 | private static final long serialVersionUID = 8924765103859833374L; 17 | 18 | 19 | @SerializedName("ActionStatus") 20 | private String actionStatus ; 21 | 22 | @SerializedName("ErrorCode") 23 | private Integer errorCode ; 24 | 25 | @SerializedName("ErrorInfo") 26 | private String errorInfo ; 27 | 28 | @SerializedName("ErrorDisplay") 29 | private String errorDisplay ; 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/AddGroupResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * @author pengshiqing 11 | * @Date: 2019/11/19 12 | * @Description: 13 | */ 14 | @Data 15 | public class AddGroupResult implements Serializable { 16 | 17 | private static final long serialVersionUID = 7535394555315687317L; 18 | 19 | 20 | @SerializedName("MemberList") 21 | private List memberList ; 22 | 23 | 24 | @Data 25 | private class MemberAccount{ 26 | 27 | @SerializedName("Member_Account") 28 | private String memberAccount ; 29 | 30 | @SerializedName("Result") 31 | private Integer result ; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/AllGroupResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * @author pengshiqing 11 | * @Date: 2019/11/19 12 | * @Description: 13 | */ 14 | @Data 15 | public class AllGroupResult implements Serializable { 16 | 17 | private static final long serialVersionUID = 5330395292714348875L; 18 | 19 | @SerializedName("TotalCount") 20 | private Integer totalCount ; 21 | 22 | @SerializedName("Next") 23 | private Long next ; 24 | 25 | @SerializedName("GroupIdList") 26 | private List groupIdList ; 27 | 28 | @Data 29 | private class GroupId{ 30 | 31 | @SerializedName("GroupId") 32 | private String groupId ; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/BatchUpdateResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * @author Element 12 | * @Package com.sevlow.sdk.tim.bean 13 | * @date 2019-05-28 00:13 14 | * @Description: 15 | */ 16 | @Data 17 | public class BatchUpdateResult implements Serializable { 18 | 19 | private static final long serialVersionUID = -2729886288572231679L; 20 | 21 | @SerializedName("ResultItem") 22 | private List resultItems = new ArrayList<>(); 23 | 24 | @SerializedName("Fail_Account") 25 | private List failAccounts = new ArrayList<>(); 26 | 27 | @SerializedName("Invalid_Account") 28 | private List invalidAccounts = new ArrayList<>(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/profile/AllowTypeEnum.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.profile; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | public enum AllowTypeEnum { 6 | 7 | /** 8 | * 需要经过自己确认才能添加自己为好友 9 | */ 10 | @SerializedName("AllowType_Type_NeedConfirm") 11 | NEED_CONFIRM("AllowType_Type_NeedConfirm"), 12 | 13 | /** 14 | * 允许任何人添加自己为好友 15 | */ 16 | @SerializedName("AllowType_Type_AllowAny") 17 | ALLOW_ANY("AllowType_Type_AllowAny"), 18 | 19 | /** 20 | * 不允许任何人添加自己为好友 21 | */ 22 | @SerializedName("AllowType_Type_DenyAny") 23 | DENY_ANY("AllowType_Type_DenyAny"); 24 | 25 | private String type; 26 | private AllowTypeEnum(String type){ 27 | this.type = type; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return this.type; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/test/TestModule.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.test; 2 | 3 | import com.google.inject.Binder; 4 | import com.google.inject.Module; 5 | import com.sevlow.sdk.tim.api.TIMService; 6 | import com.sevlow.sdk.tim.api.impl.TIMServiceImpl; 7 | import com.sevlow.sdk.tim.config.TIMConfig; 8 | import org.yaml.snakeyaml.Yaml; 9 | 10 | /** 11 | * 测试模块 12 | * @author element 13 | */ 14 | public class TestModule implements Module { 15 | 16 | @Override 17 | public void configure(Binder binder) { 18 | 19 | Yaml yaml = new Yaml(); 20 | TIMConfig config = yaml.loadAs(TestModule.class.getClassLoader().getResourceAsStream("config.test.yml"), TIMConfig.class); 21 | 22 | TIMService timService = new TIMServiceImpl(config); 23 | 24 | binder.bind(TIMConfig.class).toInstance(config); 25 | binder.bind(TIMService.class).toInstance(timService); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/chat/MsgCustomContent.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.chat; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | /** 7 | * @author pengshiqing 8 | * @Date: 2019/7/19 9 | * @Description: 10 | */ 11 | @Data 12 | public class MsgCustomContent { 13 | 14 | /** 15 | * 自定义消息数据。 不作为 APNs 的 payload 字段下发,故从 payload 中无法获取 Data 字段。 16 | */ 17 | @SerializedName("Data") 18 | private String data ; 19 | 20 | /** 21 | * 自定义消息描述信息;当接收方为 iOS 或 Android 后台在线时,做离线推送文本展示。 22 | */ 23 | @SerializedName("Desc") 24 | private String desc ; 25 | 26 | /** 27 | * 扩展字段;当接收方为 iOS 系统且应用处在后台时,此字段作为 APNs 请求包 Payloads 中的 ext 键值下发 28 | */ 29 | @SerializedName("Ext") 30 | private String ext ; 31 | 32 | /** 33 | * 自定义 APNs 推送铃音。 34 | */ 35 | @SerializedName("Sound") 36 | private String sound ; 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/profile/GenderEnum.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.profile; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Getter; 5 | 6 | /** 7 | * @author pengshiqing 8 | * @Date: 2019/7/26 9 | * @Description: 10 | */ 11 | @Getter 12 | public enum GenderEnum { 13 | 14 | /** 15 | * 女性 16 | */ 17 | @SerializedName("Gender_Type_Female") 18 | Female("Gender_Type_Female"), 19 | 20 | /** 21 | * 男性 22 | */ 23 | @SerializedName("Gender_Type_Male") 24 | MALE("Gender_Type_Male"), 25 | 26 | /** 27 | * 未知 28 | */ 29 | @SerializedName("Gender_Type_Unknown") 30 | Unknown("Gender_Type_Unknown"); 31 | 32 | private String type ; 33 | 34 | GenderEnum(String type){ 35 | this.type = type ; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return this.type; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/ShuttedMemberResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * @author pengshiqing 11 | * @Date: 2019/11/19 12 | * @Description: 13 | */ 14 | @Data 15 | public class ShuttedMemberResult implements Serializable { 16 | 17 | private static final long serialVersionUID = 6292088611087946322L; 18 | 19 | @SerializedName("GroupId") 20 | private String GroupId ; 21 | 22 | @SerializedName("ShuttedUinList") 23 | private List shuttedUinList ; 24 | 25 | 26 | @Data 27 | private class ShuttedUin{ 28 | 29 | @SerializedName("Member_Account") 30 | private String memberAccount ; 31 | 32 | @SerializedName("ShuttedUntil") 33 | private Integer shuttedUntil ; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | test-output 3 | 4 | # Mobile Tools for Java (J2ME) 5 | .mtj.tmp/ 6 | 7 | # Package Files # 8 | *.jar 9 | *.war 10 | *.ear 11 | 12 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 13 | hs_err_pid* 14 | 15 | target 16 | bin 17 | .project 18 | .classpath 19 | .settings 20 | 21 | sw-pom.xml 22 | *.iml 23 | test-config.xml 24 | .idea 25 | /.gradle/ 26 | /gradle/ 27 | *.bat 28 | /gradlew 29 | **/build/ 30 | 31 | # OSX 32 | # Icon must end with two \r 33 | Icon 34 | 35 | 36 | ._* 37 | .DS_Store 38 | .AppleDouble 39 | .LSOverride 40 | .DocumentRevisions-V100 41 | .fseventsd 42 | .Spotlight-V100 43 | .TemporaryItems 44 | .Trashes 45 | .vscode 46 | .VolumeIcon.icns 47 | .AppleDB 48 | .AppleDesktop 49 | Network Trash Folder 50 | Temporary Items 51 | .apdisk 52 | /.sonar/ 53 | sonar-project.properties 54 | 55 | !/.mvn/wrapper/maven-wrapper.jar 56 | *.versionsBackup 57 | 58 | # STS 59 | .factorypath 60 | 61 | private_key.example.txt 62 | config.test.yml -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/OnlineStatusResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * @author Element 11 | * @Package com.sevlow.sdk.tim.bean 12 | * @date 2019-05-27 17:37 13 | * @Description: TIM Account 在线状态 14 | */ 15 | @Data 16 | public class OnlineStatusResult implements Serializable { 17 | 18 | private static final long serialVersionUID = 1687104704170171011L; 19 | 20 | @SerializedName("QueryResult") 21 | private List queryResult; 22 | 23 | 24 | @Data 25 | public static class AccountState implements Serializable{ 26 | 27 | private static final long serialVersionUID = 8540690600680582034L; 28 | 29 | @SerializedName("To_Account") 30 | private String account; 31 | 32 | @SerializedName("State") 33 | private State state; 34 | } 35 | 36 | public static enum State { 37 | Online(), PushOnline(), Offline() 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/impl/TIMServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMService; 4 | import com.sevlow.sdk.tim.api.test.TestModule; 5 | import com.sevlow.sdk.tim.common.error.TIMException; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.testng.annotations.Guice; 8 | import org.testng.annotations.Test; 9 | 10 | import javax.inject.Inject; 11 | 12 | /** 13 | * TIMService 测试用例 14 | * 15 | * @author element 16 | */ 17 | @Slf4j 18 | @Guice(modules = {TestModule.class}) 19 | public class TIMServiceImplTest { 20 | 21 | @Inject 22 | private TIMService timService; 23 | 24 | @Test 25 | public void testGetUserSig() { 26 | 27 | String identifier = "test"; 28 | 29 | try { 30 | String userSig = timService.getUserSig(identifier); 31 | 32 | log.debug("userSig"); 33 | log.debug(userSig); 34 | 35 | } catch (TIMException e) { 36 | log.error(e.getMessage(), e); 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/account/TIMAccount.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.account; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | import lombok.NonNull; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @author Element 11 | * @Package com.sevlow.sdk.tim.bean 12 | * @date 2019-05-27 14:11 13 | * @Description: 14 | */ 15 | @Data 16 | public class TIMAccount implements Serializable { 17 | 18 | private static final long serialVersionUID = -7237058769283644580L; 19 | 20 | /** 21 | * 用户名,长度不超过32字节 22 | */ 23 | @NonNull 24 | @SerializedName("Identifier") 25 | private String identifier; 26 | 27 | /** 28 | * 用户昵称 29 | */ 30 | @SerializedName("Nick") 31 | private String nick; 32 | 33 | /** 34 | * 用户头像 URL 35 | */ 36 | @SerializedName("FaceUrl") 37 | private String faceUrl; 38 | 39 | /** 40 | * 帐号类型,开发者默认无需填写,值0表示普通帐号,1表示机器人帐号 41 | */ 42 | @SerializedName("Type") 43 | private Integer type = 0; 44 | 45 | public TIMAccount(String identifier) { 46 | this.identifier = identifier; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/relation/TIMFriendProfile.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.relation; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * @author element 11 | */ 12 | @Data 13 | public class TIMFriendProfile implements Serializable { 14 | 15 | private static final long serialVersionUID = 1417280732333418415L; 16 | 17 | public static final String TAG_SNS_IM_GROUP = "Tag_SNS_IM_Group"; 18 | public static final String TAG_SNS_IM_REMARK = "Tag_SNS_IM_Remark"; 19 | public static final String TAG_SNS_IM_ADD_SOURCE = "Tag_SNS_IM_AddSource"; 20 | public static final String TAG_SNS_IM_ADD_WORDING = "Tag_SNS_IM_AddWording"; 21 | 22 | @SerializedName("Tag_SNS_IM_Group") 23 | private List groups; 24 | 25 | @SerializedName("Tag_SNS_IM_Remark") 26 | private String remark; 27 | 28 | @SerializedName("Tag_SNS_IM_AddSource") 29 | private String addSource; 30 | 31 | @SerializedName("Tag_SNS_IM_AddWording") 32 | private String addWording; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/account/TIMFriend.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.account; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import com.sevlow.sdk.tim.bean.SnsItem; 5 | import lombok.Data; 6 | import lombok.NonNull; 7 | 8 | import java.io.Serializable; 9 | import java.util.List; 10 | 11 | /** 12 | * @author Element 13 | * @Package com.sevlow.sdk.tim.bean.account 14 | * @date 2019-05-28 11:42 15 | * @Description: 16 | */ 17 | @Data 18 | public class TIMFriend implements Serializable { 19 | 20 | private static final long serialVersionUID = 7273818295789187835L; 21 | 22 | @NonNull 23 | @SerializedName("To_Account") 24 | private String account; 25 | 26 | @SerializedName("Remark") 27 | private String remark; 28 | 29 | @SerializedName("RemarkTime") 30 | private Long remarkTime; 31 | 32 | @SerializedName("GroupName") 33 | private List groupNames ; 34 | 35 | @NonNull 36 | @SerializedName("AddSource") 37 | private String addSource; 38 | 39 | @SerializedName("AddWording") 40 | private String addWoring; 41 | 42 | @SerializedName("AddTime") 43 | private Long addTime; 44 | 45 | @SerializedName("CustomItem") 46 | private List customItems; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/ListBlockAccountsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * @author Element 11 | * @Package com.sevlow.sdk.tim.bean 12 | * @date 2019-05-27 19:40 13 | * @Description: TODO 14 | */ 15 | 16 | @Data 17 | public class ListBlockAccountsResult implements Serializable { 18 | private static final long serialVersionUID = 8014241940291761266L; 19 | 20 | @SerializedName("BlackListItem") 21 | private List blackListItems ; 22 | 23 | @SerializedName("StartIndex") 24 | private Integer startIndex ; 25 | 26 | @SerializedName("CurruentSequence") 27 | private Integer curruentSequence ; 28 | 29 | 30 | 31 | @Data 32 | private class BlackListItemDetail implements Serializable { 33 | 34 | private static final long serialVersionUID = -2470013868300393921L; 35 | 36 | @SerializedName("To_Account") 37 | private String toAccount ; 38 | 39 | @SerializedName("AddBlackTimeStamp") 40 | private Integer addBlackTimeStamp ; 41 | 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/CheckBlockAccountsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | /** 10 | * @author Element 11 | * @Package com.sevlow.sdk.tim.bean 12 | * @date 2019-05-27 23:05 13 | * @Description: 14 | */ 15 | @Data 16 | public class CheckBlockAccountsResult extends ResultStruct implements Serializable { 17 | 18 | private static final long serialVersionUID = -7979547907000718945L; 19 | 20 | @SerializedName("BlackListCheckItem") 21 | private List blackListCheckItems ; 22 | 23 | @SerializedName("Fail_Account") 24 | private List failAccounts ; 25 | 26 | @Data 27 | private class BlockListCheckItem implements Serializable { 28 | 29 | private static final long serialVersionUID = 3866516028140354372L; 30 | 31 | @SerializedName("To_Account") 32 | private String toAccount ; 33 | 34 | @SerializedName("Relation") 35 | private String relation ; 36 | 37 | @SerializedName("ResultCode") 38 | private Integer resultCode ; 39 | 40 | @SerializedName("ResultInfo") 41 | private String resultInfo ; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/ImportMember.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | import java.util.Date; 10 | 11 | /** 12 | * @author pengshiqing 13 | * @Date: 2019/11/19 14 | * @Description: 15 | */ 16 | @Data 17 | @AllArgsConstructor 18 | @NoArgsConstructor 19 | public class ImportMember implements Serializable { 20 | 21 | private static final long serialVersionUID = -7697775807568780852L; 22 | 23 | 24 | @SerializedName("Member_Account") 25 | private String MemberAccount ; 26 | 27 | @SerializedName("Role") 28 | private Role role ; 29 | 30 | @SerializedName("JoinTime") 31 | private Date joinTime ; 32 | 33 | @SerializedName("UnreadMsgNum") 34 | private Integer unreadMsgNum ; 35 | 36 | public enum Role{ 37 | Admin, 38 | Member 39 | } 40 | 41 | public ImportMember(String memberAccount) { 42 | MemberAccount = memberAccount; 43 | } 44 | 45 | public ImportMember(String memberAccount, Role role) { 46 | MemberAccount = memberAccount; 47 | this.role = role; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/common/Base64Url.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.common; 2 | 3 | import org.bouncycastle.util.encoders.Base64; 4 | import org.bouncycastle.util.encoders.DecoderException; 5 | 6 | /** 7 | * @author Element 8 | * @Package com.sevlow.sdk.tim.common 9 | * @date 2019-05-27 11:33 10 | * @Description: TODO 11 | */ 12 | public class Base64Url { 13 | 14 | //int base64_encode_url(const unsigned char *in_str, int length, char *out_str,int *ret_length) 15 | public static byte[] base64EncodeUrl(byte[] in_str) { 16 | byte[] base64 = Base64.encode(in_str); 17 | base64 = base64ArrFix(base64); 18 | return base64; 19 | } 20 | 21 | //int base64_decode_url(const unsigned char *in_str, int length, char *out_str, int *ret_length) 22 | public static byte[] base64DecodeUrl(byte[] in_str) throws DecoderException { 23 | byte[] base64 = in_str.clone(); 24 | base64 = base64ArrFix(base64); 25 | return Base64.decode(base64); 26 | } 27 | 28 | private static byte[] base64ArrFix(byte[] base64) { 29 | for (int i = 0; i < base64.length; ++i) { 30 | 31 | switch (base64[i]) { 32 | case '*': 33 | base64[i] = '+'; 34 | break; 35 | case '-': 36 | base64[i] = '/'; 37 | break; 38 | case '_': 39 | base64[i] = '='; 40 | break; 41 | default: 42 | break; 43 | } 44 | 45 | } 46 | 47 | return base64; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/relation/ListFriendsResult.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.relation; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | 11 | /** 12 | * 好友列表返回值 13 | * 14 | * @author element 15 | */ 16 | @Data 17 | public class ListFriendsResult implements Serializable { 18 | 19 | private static final long serialVersionUID = -4489242246189349542L; 20 | 21 | @SerializedName("UserDataItem") 22 | private List userDataItems = new ArrayList<>(); 23 | 24 | /** 25 | * 标配好友数据的 Sequence,客户端可以保存该 Sequence,下次请求时通过请求的 StandardSequence 字段返回给后台 26 | */ 27 | @SerializedName("StandardSequence") 28 | private Integer standardSequence; 29 | 30 | /** 31 | * 自定义好友数据的 Sequence,客户端可以保存该 Sequence,下次请求时通过请求的 CustomSequence 字段返回给后台 32 | */ 33 | @SerializedName("CustomSequence") 34 | private Integer customSequence; 35 | 36 | /** 37 | * 好友总数 38 | */ 39 | @SerializedName("FriendNum") 40 | private Integer friendNum; 41 | 42 | /** 43 | * 分页的结束标识,非0值表示已完成全量拉取 44 | */ 45 | @SerializedName("CompleteFlag") 46 | private Integer completeFlag; 47 | 48 | /** 49 | * 分页接口下一页的起始位置 50 | */ 51 | @SerializedName("NextStartIndex") 52 | private Integer nextStartIndex; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMAccountService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | import com.sevlow.sdk.tim.bean.ImportAccountsResult; 4 | import com.sevlow.sdk.tim.bean.account.TIMAccount; 5 | import com.sevlow.sdk.tim.common.error.TIMException; 6 | import lombok.NonNull; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author Element 12 | * 13 | * 账号管理 14 | * API Doc : https://cloud.tencent.com/document/product/269/1607 15 | */ 16 | public interface TIMAccountService { 17 | 18 | /** 19 | * 账号导入接口 20 | * 21 | * 该接口的功能是在云通信 IM 创建一个内部 ID,使没有登录云通信 IM 的 App 自有帐号能够使用云通信 IM 服务。 22 | * 23 | * API Doc : https://cloud.tencent.com/document/product/269/1608 24 | * @param account 25 | */ 26 | void singleImport(TIMAccount account) throws TIMException; 27 | 28 | /** 29 | * 帐号批量导入接口 30 | * 31 | * 该接口的功能是在云通信 IM 创建一个内部 ID,使没有登录云通信 IM 的 App 自有帐号能够使用云通信 IM 服务。 32 | * 33 | * API Doc : https://cloud.tencent.com/document/product/269/4919 34 | * @param accounts 35 | * @return 36 | */ 37 | ImportAccountsResult multiImport(List accounts) throws TIMException; 38 | 39 | /** 40 | * 帐号登录态失效接口 41 | * 42 | * 本接口适用于将 App 用户帐号的登录态(如 UserSig)失效。 43 | * 使用该接口将用户登录态失效后,用户如果使用重新生成的UserSig可以成功登录云通信 IM,接口支持一次失效一个帐号。 44 | * 45 | * API Doc : https://cloud.tencent.com/document/product/269/3853 46 | * @param identifier 47 | */ 48 | void kick(@NonNull String identifier) throws TIMException; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/GroupMemberInfo.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author pengshiqing 10 | * @Date: 2019/11/19 11 | * @Description: 12 | */ 13 | @Data 14 | public class GroupMemberInfo implements Serializable { 15 | 16 | private static final long serialVersionUID = -7860881397655972971L; 17 | 18 | /** 19 | * 要操作的群组(必填) 20 | */ 21 | @SerializedName("GroupId") 22 | private String GroupId ; 23 | 24 | /** 25 | * 要操作的群成员(必填) 26 | */ 27 | @SerializedName("Member_Account") 28 | private String memberAccount ; 29 | 30 | /** 31 | * 设置角色 32 | */ 33 | @SerializedName("Role") 34 | private Role role ; 35 | 36 | /** 37 | * 群名片(选填) 38 | */ 39 | @SerializedName("NameCard") 40 | private String nameCard ; 41 | 42 | /** 43 | * AcceptAndNotify、Discard或者AcceptNotNotify,消息屏蔽类型 44 | */ 45 | @SerializedName("MsgFlag") 46 | private MsgFlag msgFlag ; 47 | 48 | /** 49 | * 指定群成员的禁言时间,单位为秒 50 | */ 51 | @SerializedName("ShutUpTime") 52 | private Integer shutUpTime ; 53 | 54 | 55 | 56 | public enum MsgFlag{ 57 | AcceptAndNotify, 58 | Discard, 59 | AcceptNotNotify 60 | } 61 | 62 | public enum Role{ 63 | Admin, 64 | Member 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/common/error/TIMError.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.common.error; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import com.sevlow.sdk.tim.constant.TIMErrorConstant; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import java.io.Serializable; 10 | 11 | 12 | /** 13 | * TIM 错误值 14 | * 15 | * @author element 16 | */ 17 | @Data 18 | @AllArgsConstructor 19 | @NoArgsConstructor 20 | public class TIMError implements Serializable { 21 | 22 | private static final long serialVersionUID = -906834256793589790L; 23 | 24 | @SerializedName("ErrorCode") 25 | private int errorCode = 0; 26 | 27 | @SerializedName("ErrorInfo") 28 | private String errorInfo = ""; 29 | 30 | @SerializedName("ActionStatus") 31 | private String actionStatus = "OK"; 32 | 33 | @SerializedName("ErrorDisplay") 34 | private String errorDisplay = ""; 35 | 36 | public TIMError(int errorCode, String errorInfo) { 37 | this.errorCode = errorCode; 38 | this.errorInfo = errorInfo; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "错误: ErrorCode=" + this.errorCode + 44 | ", ErrorInfo=" + this.errorInfo + 45 | ", ErrorDisplay=" + this.errorDisplay + 46 | ", ActionStatus=" + this.actionStatus + 47 | "\n" + 48 | "Description=" + TIMErrorConstant.getErrorInfo(this.errorCode); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/profile/UserProfileItem.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.profile; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import com.sevlow.sdk.tim.bean.ResultStruct; 5 | import com.sevlow.sdk.tim.bean.SnsItem; 6 | import lombok.Data; 7 | import lombok.NonNull; 8 | 9 | import java.io.Serializable; 10 | import java.util.List; 11 | 12 | /** 13 | * @author element 14 | */ 15 | @Data 16 | public class UserProfileItem extends ResultStruct implements Serializable { 17 | private static final long serialVersionUID = -6019144745649301532L; 18 | 19 | private static final String CUSTOM_PROFILE_PREFIX = "Tag_Profile_Custom_"; 20 | 21 | @SerializedName("To_Account") 22 | private String toAccount; 23 | 24 | @SerializedName("ProfileItem") 25 | private List profileItems; 26 | 27 | public Object getProfile(@NonNull String key){ 28 | if(profileItems == null || profileItems.isEmpty()){ 29 | return null; 30 | } 31 | 32 | return profileItems.stream() 33 | .filter((item)->key.equals(item.getTag())) 34 | .findFirst() 35 | .orElse(null); 36 | } 37 | 38 | public Object getCustom(@NonNull String key){ 39 | if(profileItems == null || profileItems.isEmpty()){ 40 | return null; 41 | } 42 | 43 | String completeKey = CUSTOM_PROFILE_PREFIX.concat(key); 44 | return profileItems.stream() 45 | .filter((item)->completeKey.equals(item.getTag())) 46 | .findFirst() 47 | .orElse(null); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/impl/TIMOnlineStatusServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMOnlineStatusService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.bean.OnlineStatusResult; 6 | import com.sevlow.sdk.tim.common.error.TIMError; 7 | import com.sevlow.sdk.tim.common.error.TIMException; 8 | import com.sevlow.sdk.tim.utils.JsonUtils; 9 | import lombok.NonNull; 10 | import lombok.extern.slf4j.Slf4j; 11 | 12 | import java.util.HashMap; 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | 17 | /** 18 | * @author element 19 | */ 20 | @Slf4j 21 | public class TIMOnlineStatusServiceImpl implements TIMOnlineStatusService { 22 | 23 | private TIMService timService; 24 | 25 | public TIMOnlineStatusServiceImpl(TIMService timService) { 26 | this.timService = timService; 27 | } 28 | 29 | @Override 30 | public OnlineStatusResult queryState(@NonNull List accounts) throws TIMException { 31 | 32 | if (accounts == null || accounts.size() < 1) { 33 | throw new RuntimeException("accounts 不能为空集"); 34 | } 35 | 36 | if (accounts.size() > 500) { 37 | throw new TIMException(new TIMError(90011, "批量发消息目标帐号超过500,请减少 To_Account 中目标帐号数量")); 38 | } 39 | 40 | String api = "v4/openim/querystate"; 41 | 42 | Map> queryStateBody = new HashMap<>(); 43 | queryStateBody.put("To_Account", accounts); 44 | 45 | String jsonResult = this.timService.post(api, queryStateBody); 46 | 47 | OnlineStatusResult result = JsonUtils.fromJson(jsonResult, OnlineStatusResult.class); 48 | 49 | return result; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/impl/TIMOnlineStatusServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMOnlineStatusService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.api.test.TestModule; 6 | import com.sevlow.sdk.tim.bean.OnlineStatusResult; 7 | import com.sevlow.sdk.tim.common.error.TIMException; 8 | import com.sevlow.sdk.tim.utils.JsonUtils; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.testng.Assert; 11 | import org.testng.annotations.Guice; 12 | import org.testng.annotations.Test; 13 | 14 | import javax.inject.Inject; 15 | import java.util.Arrays; 16 | import java.util.List; 17 | 18 | /** 19 | * @author Element 20 | * @Package com.sevlow.sdk.tim.api.impl 21 | * @date 2019-05-27 17:50 22 | * @Description: 23 | */ 24 | @Slf4j 25 | @Guice(modules = {TestModule.class}) 26 | public class TIMOnlineStatusServiceImplTest { 27 | 28 | @Inject 29 | private TIMService timService; 30 | 31 | @Test 32 | public void testQueryState() throws TIMException { 33 | 34 | TIMOnlineStatusService onlineStatusService = timService.getOnlineStatusService(); 35 | 36 | String[] arr = new String[]{ 37 | "test_1","test_2","test_3" 38 | }; 39 | 40 | List accounts = Arrays.asList(arr); 41 | 42 | OnlineStatusResult onlineStatus = onlineStatusService.queryState(accounts); 43 | 44 | List results = onlineStatus.getQueryResult(); 45 | 46 | OnlineStatusResult.State test1State = null ; 47 | 48 | for(OnlineStatusResult.AccountState result : results){ 49 | if("test_1".equals(result.getAccount())){ 50 | test1State = result.getState(); 51 | break; 52 | } 53 | } 54 | 55 | Object p = OnlineStatusResult.State.Offline ; 56 | Assert.assertEquals(OnlineStatusResult.State.Offline, test1State); 57 | 58 | log.debug(JsonUtils.toJson(onlineStatus)); 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/impl/TIMAccountServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMAccountService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.api.test.TestModule; 6 | import com.sevlow.sdk.tim.bean.ImportAccountsResult; 7 | import com.sevlow.sdk.tim.bean.account.TIMAccount; 8 | import com.sevlow.sdk.tim.common.error.TIMException; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.testng.Assert; 11 | import org.testng.annotations.BeforeTest; 12 | import org.testng.annotations.Guice; 13 | import org.testng.annotations.Test; 14 | 15 | import javax.inject.Inject; 16 | import java.util.Arrays; 17 | 18 | /** 19 | * @author Element 20 | * @Package com.sevlow.sdk.tim.api.impl 21 | * @date 2019-05-27 15:30 22 | * @Description: 23 | */ 24 | @Slf4j 25 | @Guice(modules = {TestModule.class}) 26 | public class TIMAccountServiceImplTest { 27 | 28 | @Inject 29 | private TIMService timService; 30 | 31 | private TIMAccountService accountService; 32 | 33 | @BeforeTest 34 | public void before() { 35 | this.accountService = timService.getAccountService(); 36 | } 37 | 38 | @Test 39 | public void testSingleImport() throws TIMException { 40 | 41 | TIMAccount account = new TIMAccount("10004"); 42 | account.setNick("群通知"); 43 | account.setFaceUrl("https://res.7billion.cn/common/default/meseage_icon_group_notice.png"); 44 | 45 | accountService.singleImport(account); 46 | 47 | } 48 | 49 | @Test 50 | public void testMultiImport() throws TIMException { 51 | 52 | ImportAccountsResult result = accountService.multiImport(Arrays.asList("74940051523371008", "74897564679274496", "test_4", "test_5")); 53 | Assert.assertEquals(0, result.getFailAccounts().size()); 54 | 55 | } 56 | 57 | @Test 58 | public void testKick() throws TIMException { 59 | 60 | accountService.kick("test_2"); 61 | } 62 | 63 | 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | 4 | import com.sevlow.sdk.tim.common.error.TIMException; 5 | import com.sevlow.sdk.tim.config.TIMConfig; 6 | import lombok.NonNull; 7 | 8 | import java.util.Map; 9 | 10 | /** 11 | * @author Element 12 | * 13 | * TIMService是所有Service模块的入口 14 | */ 15 | public interface TIMService { 16 | 17 | TIMConfig getConfig(); 18 | 19 | /** 20 | * 获取UserSig信息 (默认过期时间为30天) 21 | * 22 | * @param identifier 23 | * @return 24 | * @throws TIMException 25 | */ 26 | String getUserSig(@NonNull String identifier) throws TIMException; 27 | 28 | /** 29 | * 获取UserSig信息 30 | * 31 | * @param identifier 32 | * @param expireOfDay 过期天数 33 | * @return 34 | * @throws TIMException 35 | */ 36 | String getUserSig(@NonNull String identifier, Integer expireOfDay) throws TIMException; 37 | 38 | /** 39 | * 通用GET方法 40 | * 41 | * @param api TIM API 地址 42 | * @param queryParams API 地址所需参数(不包含公共参数) 43 | * @return 44 | * @throws TIMException 45 | */ 46 | String get(String api, Map queryParams) throws TIMException; 47 | 48 | /** 49 | * 通用POST方法 50 | * 51 | * @param api TIM API 地址 52 | * @param body API 请求体 53 | * @return 54 | * @throws TIMException 55 | */ 56 | String post(String api, Object body) throws TIMException; 57 | 58 | TIMAccountService getAccountService(); 59 | 60 | TIMChatService getChatService(); 61 | 62 | TIMDirtyWordService getDirtyWordService(); 63 | 64 | TIMGroupService getGroupService(); 65 | 66 | TIMNoSpeakService getNoSpeakService(); 67 | 68 | TIMOnlineStatusService getOnlineStatusService(); 69 | 70 | TIMOperationalService getOperationalService(); 71 | 72 | TIMProfileService getProfileService(); 73 | 74 | TIMRelationService getRelationService(); 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMChatService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | import com.sevlow.sdk.tim.bean.chat.ChatMsgEnum; 4 | import com.sevlow.sdk.tim.bean.chat.MsgCustomContent; 5 | import com.sevlow.sdk.tim.common.error.TIMException; 6 | import lombok.NonNull; 7 | 8 | import java.util.List; 9 | 10 | 11 | /** 12 | * @author Element 13 | * 14 | * 单聊消息 15 | * API Doc : https://cloud.tencent.com/document/product/269/1610 16 | * 17 | */ 18 | public interface TIMChatService { 19 | 20 | /** 21 | * 批量文本消息 22 | * @param fromAccount 指定发送账号 23 | * @param toAccounts 群发接收账号集合 24 | * @param msgList 消息集合 25 | */ 26 | void batchSendTextMsg(String fromAccount , List toAccounts,List msgList) throws TIMException; 27 | void batchSendTextMsg(String fromAccount , List toAccounts, List msgList, ChatMsgEnum msgEnum) throws TIMException; 28 | 29 | 30 | /** 31 | * 批量自定义消息 32 | * @param fromAccount 指定发送账号 33 | * @param toAccounts 群发接收账号集合 34 | * @param msgCustomContent 消息集合 35 | */ 36 | void batchSendCustomMsg(String fromAccount , List toAccounts,@NonNull MsgCustomContent msgCustomContent) throws TIMException; 37 | void batchSendCustomMsg(String fromAccount , List toAccounts,@NonNull MsgCustomContent msgCustomContent,ChatMsgEnum msgEnum) throws TIMException; 38 | 39 | 40 | /** 41 | * 发送单聊文本消息 42 | * @param fromAccount 指定发送账号 43 | * @param toAccount 群发接收账号 44 | * @param msgList 消息集合 45 | */ 46 | void sendTextMsg(String fromAccount , String toAccount,List msgList) throws TIMException; 47 | void sendTextMsg(String fromAccount , String toAccount,List msgList,ChatMsgEnum msgEnum) throws TIMException; 48 | 49 | /** 50 | * 发送单聊自定义消息 51 | * @param fromAccount 指定发送账号 52 | * @param toAccount 群发接收账号集合 53 | * @param msgCustomContent 消息 54 | */ 55 | void sendCustomMsg(@NonNull String fromAccount , @NonNull String toAccount, @NonNull MsgCustomContent msgCustomContent) throws TIMException; 56 | void sendCustomMsg(@NonNull String fromAccount , @NonNull String toAccount, @NonNull MsgCustomContent msgCustomContent,ChatMsgEnum msgEnum) throws TIMException; 57 | 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TIM-java-sdk [![LICENSE](https://img.shields.io/badge/License-Anti%20996-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 2 | 3 | 本项目是 云通讯(腾讯) 服务 Java 版本 开发工具包 4 | 5 | ## 进展 - (停止维护) 6 | 7 | - 账号管理 ✅💯 8 | - 单聊消息管理 9 | - 在线状态管理 ✅💯 10 | - 关系链管理 ✅💯 11 | - 资料管理 ✅💯 12 | - 群组管理 `进行中 80%` 13 | - 脏字管理 14 | - 全局禁言管理 15 | - 运营相关管理 16 | 17 | ## 准备工作 18 | 19 | - IDE 上配置lombok支持插件 20 | - JDK >= 1.8 21 | - Maven 环境 22 | 23 | ## 配置 24 | *TIMConfig* 25 | ``` 26 | appId : 腾讯云腾讯SDKID 27 | adminIdentifier : 管理员账号 28 | privateKeyPath : 管理员私钥文件地址 29 | accountType : 管理员accountType 30 | reqMaxRetry : 请求最大重试数 31 | ``` 32 | 33 | ## 使用 34 | ``` 35 | TIMConfig config = new TIMConfig(...); 36 | TIMService timService = new TIMServiceImpl(config); 37 | 38 | // 导入好友 39 | try{ 40 | timService.getRelationService().importFriends(...) 41 | }catch(TIMException e){ 42 | if(e.getError().getErrorCode() == 30010){ 43 | // 好友已达系统上限 44 | } 45 | } 46 | 47 | 48 | // 更多接口请参照文档或者com.sevlow.sdk.tim.api接口 49 | ``` 50 | 51 | ## 开发 52 | `com.sevlow.sdk.tim.api`下定义接口 53 | `com.sevlow.sdk.tim.api.impl`下实现接口方法 54 | `com.sevlow.sdk.tim.bean`下存放接口所需的Req和Resp结构化参数 55 | 56 | ### 编译 57 | ``` 58 | mvn clean install 59 | ``` 60 | 61 | ### 测试 62 | 63 | #### 配置参数 64 | 复制 /src/test/resources 下的 `config.test.example.yml` 并重命名为 `config.test.yml` 65 | 66 | 在 `config.test.yml` 下输入 腾讯 云通讯对应的配置,`privateKeyPath`项可以写文件的绝对路径,亦可以将该文件放到 /src/test/resources 下并重命名为`private_key.example.txt` 67 | 68 | 在 `.gitignore` 文件下对 `private_key.example.txt` 和 `config.test.yml` 文件做了忽略处理,这样不用担心提交到开源仓库 69 | 70 | #### 测试用例 71 | 72 | 编写完代码后,在 /src/test/java 下有对应的测试用例,可以按照里面的方式进行编写 73 | 74 | 测试套件使用`TestNG` 75 | 76 | 测试套件的注入工具使用`com.google.inject:guice` 77 | 78 | ## 贡献代码 79 | 80 | fork 本项目 81 | 82 | clone自己fork的仓库 83 | 84 | 添加本仓库地址为远程仓库 85 | ```shell 86 | git remote add upstream https://github.com/forfuns/TIMJava.git 87 | ``` 88 | 89 | 定期保持自己仓库和此项目的内容 90 | 91 | ```shell 92 | git fetch upstream 93 | git checkout develop 94 | git rebase upstream/develop 95 | git push origin develop 96 | 97 | ``` 98 | 99 | 切换到`develop`分支 100 | 101 | 在自己的仓库和develop的分支上开发代码,并且编写测试用例 102 | 103 | 最后在此项目中提交`PR`(Pull request) 104 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/profile/TIMProfile.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.profile; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author element 10 | */ 11 | @Data 12 | public class TIMProfile implements Serializable { 13 | 14 | private static final long serialVersionUID = -5970285192843170414L; 15 | 16 | public static final String TAG_PROFILE_IM_NICK = "Tag_Profile_IM_Nick"; 17 | public static final String TAG_PROFILE_IM_GENDER = "Tag_Profile_IM_Gender"; 18 | public static final String TAG_PROFILE_IM_BIRTHDAY = "Tag_Profile_IM_BirthDay"; 19 | public static final String TAG_PROFILE_IM_LOCATION = "Tag_Profile_IM_Location"; 20 | public static final String TAG_PROFILE_IM_SELFSIGNATURE = "Tag_Profile_IM_SelfSignature"; 21 | public static final String TAG_PROFILE_IM_ALLOWTYPE = "Tag_Profile_IM_AllowType"; 22 | public static final String TAG_PROFILE_IM_LANGUAGE = "Tag_Profile_IM_Language"; 23 | public static final String TAG_PROFILE_IM_IMAGE = "Tag_Profile_IM_Image"; 24 | public static final String TAG_PROFILE_IM_MSGSETTINGS = "Tag_Profile_IM_MsgSettings"; 25 | public static final String TAG_PROFILE_IM_ADMINFORBIDTYPE = "Tag_Profile_IM_AdminForbidType"; 26 | public static final String TAG_PROFILE_IM_LEVEL = "Tag_Profile_IM_Level"; 27 | public static final String TAG_PROFILE_IM_ROLE = "Tag_Profile_IM_Role"; 28 | 29 | @SerializedName(TAG_PROFILE_IM_NICK) 30 | private String nick; 31 | 32 | @SerializedName(TAG_PROFILE_IM_GENDER) 33 | private GenderEnum gender; 34 | 35 | @SerializedName(TAG_PROFILE_IM_BIRTHDAY) 36 | private String birthday; 37 | 38 | @SerializedName(TAG_PROFILE_IM_LOCATION) 39 | private String location; 40 | 41 | @SerializedName(TAG_PROFILE_IM_SELFSIGNATURE) 42 | private String selfSignature; 43 | 44 | @SerializedName(TAG_PROFILE_IM_ALLOWTYPE) 45 | private AllowTypeEnum allowType; 46 | 47 | @SerializedName(TAG_PROFILE_IM_LANGUAGE) 48 | private String language; 49 | 50 | @SerializedName(TAG_PROFILE_IM_IMAGE) 51 | private String image; 52 | 53 | @SerializedName(TAG_PROFILE_IM_MSGSETTINGS) 54 | private Integer msgSettings; 55 | 56 | @SerializedName(TAG_PROFILE_IM_ADMINFORBIDTYPE) 57 | private AdminForbidTypeEnum adminForbidType; 58 | 59 | @SerializedName(TAG_PROFILE_IM_LEVEL) 60 | private Integer level; 61 | 62 | @SerializedName(TAG_PROFILE_IM_ROLE) 63 | private Integer role; 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMProfileService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | import com.sevlow.sdk.tim.bean.profile.GenderEnum; 4 | import com.sevlow.sdk.tim.bean.profile.TIMProfile; 5 | import com.sevlow.sdk.tim.bean.profile.UserProfileResult; 6 | import com.sevlow.sdk.tim.common.error.TIMException; 7 | import lombok.NonNull; 8 | 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | /** 13 | * @author Element 14 | * 15 | * 资料管理 16 | * API Doc : https://cloud.tencent.com/document/product/269/1638 17 | */ 18 | public interface TIMProfileService { 19 | 20 | 21 | /** 22 | * 设置资料 23 | *

24 | * 支持 标配资料字段 和 自定义资料字段 的设置。 25 | *

26 | * API Doc : https://cloud.tencent.com/document/product/269/1500 27 | * 28 | * @param identifier 29 | * @param imProfile 标配资料 30 | * @param customProfile 自定义资料 31 | * @throws TIMException 32 | */ 33 | void setPortrait(@NonNull String identifier, TIMProfile imProfile, Map customProfile) throws TIMException; 34 | 35 | /** 36 | * 设置资料 37 | *

38 | * 支持 标配资料字段 和 自定义资料字段 的设置。 39 | *

40 | * API Doc : https://cloud.tencent.com/document/product/269/1500 41 | * 42 | * @param identifier 43 | * @param imProfile 标配资料 44 | * @throws TIMException 45 | */ 46 | void setPortrait(@NonNull String identifier, TIMProfile imProfile) throws TIMException; 47 | 48 | /** 49 | * 设置资料 50 | *

51 | * 支持 标配资料字段 和 自定义资料字段 的设置。 52 | *

53 | * API Doc : https://cloud.tencent.com/document/product/269/1500 54 | * 55 | * @param identifier 56 | * @param customProfile 自定义资料 57 | * @throws TIMException 58 | */ 59 | void setPortrait(@NonNull String identifier, Map customProfile) throws TIMException; 60 | 61 | /** 62 | * 拉取资料 63 | *

64 | * 支持拉取好友和非好友的资料字段。 65 | * 支持拉取 标配资料字段 和 自定义资料字段。 66 | * 建议每次拉取的用户数不超过100,避免因回包数据量太大导致回包失败。 67 | * 请确保请求中的所有帐号都已导入即时通信 IM,如果请求中含有未导入即时通信 IM 的帐号,即时通信 IM 后台将会提示错误。 68 | *

69 | * API Doc : https://cloud.tencent.com/document/product/269/1639 70 | * 71 | * @param accounts 账号集合,不超过100个 72 | * @param profiles 标配资料字段 @see com.sevlow.sdk.tim.bean.profile.TIMProfile 73 | * @param customProfiles 自定义资料字段,无需输入Tag_Profile_Custom_,自动补充 74 | * @throws TIMException 75 | */ 76 | UserProfileResult getPortrait(@NonNull List accounts, List profiles, List customProfiles) throws TIMException; 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/bean/group/GroupInfo.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.bean.group; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | /** 9 | * @author pengshiqing 10 | * @Date: 2019/11/19 11 | * @Description: 12 | */ 13 | @Data 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | public class GroupInfo { 17 | 18 | /** 19 | * 群主的 UserId(选填) 20 | */ 21 | @SerializedName("Owner_Account") 22 | private String ownerAccount ; 23 | 24 | /** 25 | * 群id 选填 26 | */ 27 | @SerializedName("GroupId") 28 | private String groupId ; 29 | 30 | /** 31 | * 群组类型:Private/Public/ChatRoom/AVChatRoom/BChatRoom(必填) 32 | */ 33 | @SerializedName("Type") 34 | private Type type ; 35 | 36 | /** 37 | * 最大群成员数量(选填) 38 | */ 39 | @SerializedName("MaxMemberNum") 40 | private Integer maxMemberNum ; 41 | 42 | /** 43 | * 群名称(必填) 44 | */ 45 | @SerializedName("Name") 46 | private String name ; 47 | 48 | /** 49 | * 群简介(选填) 50 | */ 51 | @SerializedName("Introduction") 52 | private String introduction ; 53 | 54 | /** 55 | * 群公告(选填) 56 | */ 57 | @SerializedName("Notification") 58 | private String notification ; 59 | 60 | /** 61 | * 群头像 URL(选填) 62 | */ 63 | @SerializedName("FaceUrl") 64 | private String faceUrl ; 65 | 66 | /** 67 | * 申请加群处理方式(选填) 68 | */ 69 | @SerializedName("ApplyJoinOption") 70 | private ApplyJoinOption applyJoinOption ; 71 | 72 | 73 | /** 74 | * 设置全员禁言(选填):"On"开启,"Off"关闭 75 | */ 76 | @SerializedName("ShutUpAllMember") 77 | private ShutUpAllMember shutUpAllMember ; 78 | 79 | 80 | public enum Type { 81 | /** 82 | * 公开群 83 | */ 84 | Public, 85 | 86 | /** 87 | * 私有群 88 | */ 89 | Private, 90 | 91 | /** 92 | * 聊天室 93 | */ 94 | ChatRoom, 95 | 96 | /** 97 | * 音视频聊天室 98 | */ 99 | AVChatRoom, 100 | 101 | /** 102 | * 在线成员广播大群 103 | */ 104 | BChatRoom 105 | } 106 | 107 | public enum ApplyJoinOption{ 108 | FreeAccess, 109 | NeedPermission, 110 | DisableApply, 111 | } 112 | 113 | public enum ShutUpAllMember { 114 | On, 115 | Off 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/impl/TIMAccountServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMAccountService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.bean.ImportAccountsResult; 6 | import com.sevlow.sdk.tim.bean.account.TIMAccount; 7 | import com.sevlow.sdk.tim.common.error.TIMError; 8 | import com.sevlow.sdk.tim.common.error.TIMException; 9 | import com.sevlow.sdk.tim.utils.JsonUtils; 10 | import lombok.NonNull; 11 | import lombok.extern.slf4j.Slf4j; 12 | 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | 18 | /** 19 | * @author element 20 | */ 21 | @Slf4j 22 | public class TIMAccountServiceImpl implements TIMAccountService { 23 | 24 | private TIMService timService; 25 | 26 | public TIMAccountServiceImpl(TIMService timService) { 27 | this.timService = timService; 28 | } 29 | 30 | @Override 31 | public void singleImport(@NonNull TIMAccount account) throws TIMException { 32 | 33 | try { 34 | 35 | this.doSingleImport(account); 36 | 37 | } catch (TIMException e) { 38 | 39 | TIMError error = e.getError(); 40 | int errorCode = error.getErrorCode(); 41 | if (errorCode == 70169) { 42 | log.warn("出现 TIM 内部错误 , 再重试一遍 (on TIMAccountService.singleImport method only try again)"); 43 | doSingleImport(account); 44 | return; 45 | } 46 | throw e; 47 | } 48 | 49 | } 50 | 51 | private void doSingleImport(TIMAccount account) throws TIMException { 52 | String api = "v4/im_open_login_svc/account_import"; 53 | this.timService.post(api, account); 54 | } 55 | 56 | @Override 57 | public ImportAccountsResult multiImport(List accounts) throws TIMException { 58 | 59 | if (accounts == null || accounts.size() < 1) { 60 | return new ImportAccountsResult(); 61 | } 62 | 63 | if(accounts.size() > 100){ 64 | throw new TIMException(new TIMError(70402, "单次用户导入不能超过100个,请分段导入")); 65 | } 66 | 67 | String api = "v4/im_open_login_svc/multiaccount_import"; 68 | 69 | Map> multiImportBody = new HashMap<>(); 70 | multiImportBody.put("Accounts", accounts); 71 | 72 | String jsonResult = this.timService.post(api, multiImportBody); 73 | 74 | ImportAccountsResult result = JsonUtils.fromJson(jsonResult, ImportAccountsResult.class); 75 | 76 | return result; 77 | } 78 | 79 | @Override 80 | public void kick(@NonNull String identifier) throws TIMException { 81 | String api = "v4/im_open_login_svc/kick"; 82 | 83 | Map kickBody = new HashMap<>(); 84 | kickBody.put("Identifier", identifier); 85 | 86 | String jsonResult = this.timService.post(api, kickBody); 87 | 88 | log.debug("【TIMJava】kick user( {} ) result : {}", identifier, jsonResult); 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/impl/TIMProfileServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMProfileService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.api.test.TestModule; 6 | import com.sevlow.sdk.tim.bean.profile.*; 7 | import com.sevlow.sdk.tim.common.error.TIMException; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.testng.annotations.BeforeTest; 10 | import org.testng.annotations.Guice; 11 | import org.testng.annotations.Test; 12 | import org.testng.collections.Lists; 13 | 14 | import javax.inject.Inject; 15 | import java.util.HashMap; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | /** 20 | * @author pengshiqing 21 | * @Date: 2019/7/26 22 | * @Description: 23 | */ 24 | 25 | @Slf4j 26 | @Guice(modules = {TestModule.class}) 27 | public class TIMProfileServiceImplTest { 28 | 29 | @Inject 30 | private TIMService timService; 31 | 32 | private TIMProfileService profileService; 33 | 34 | 35 | @BeforeTest 36 | public void before() { 37 | this.profileService = timService.getProfileService(); 38 | } 39 | 40 | @Test 41 | public void testSetPortrait() throws TIMException { 42 | TIMProfile imProfile = new TIMProfile(); 43 | imProfile.setGender(GenderEnum.Female); 44 | imProfile.setAdminForbidType(AdminForbidTypeEnum.NONE); 45 | imProfile.setAllowType(AllowTypeEnum.NEED_CONFIRM); 46 | 47 | Map customProfile = new HashMap<>(); 48 | customProfile.put("age", "28"); 49 | customProfile.put("college", "家里蹲"); 50 | 51 | profileService.setPortrait("10001", imProfile, customProfile); 52 | } 53 | 54 | @Test 55 | public void testSetInfoGender3() throws TIMException { 56 | Map map = new HashMap<>(); 57 | map.put("Tag_Profile_IM_Nick", "你好"); 58 | map.put("Tag_Profile_Custom_age", "10"); 59 | // profileService.setInfoGender("10001", map); 60 | 61 | } 62 | 63 | @Test 64 | public void testGetPortrait() throws TIMException { 65 | List accounts = Lists.newArrayList("129333528310579201"); 66 | List profileTags = Lists.newArrayList(TIMProfile.TAG_PROFILE_IM_NICK,TIMProfile.TAG_PROFILE_IM_GENDER,TIMProfile.TAG_PROFILE_IM_IMAGE,TIMProfile.TAG_PROFILE_IM_ALLOWTYPE); 67 | List customs = Lists.newArrayList("college","age"); 68 | 69 | UserProfileResult result = profileService.getPortrait(accounts,profileTags,customs); 70 | 71 | List userProfileItems = result.getUserProfileItems(); 72 | 73 | String nickName = null; 74 | for(UserProfileItem item : userProfileItems){ 75 | nickName = String.valueOf(item.getProfile(TIMProfile.TAG_PROFILE_IM_NICK)); 76 | log.info(item.getToAccount().concat(".nickName : ").concat(nickName)); 77 | } 78 | 79 | } 80 | } -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/constant/TIMErrorConstant.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.constant; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 腾讯 云通讯 错误码对照表 8 | * 9 | * @author element 10 | */ 11 | public class TIMErrorConstant { 12 | 13 | public final static Map ERROR_INFO_MAP = new HashMap<>(); 14 | 15 | static { 16 | 17 | ERROR_INFO_MAP.put(-1, "Unknow"); 18 | 19 | ERROR_INFO_MAP.put(30001, "请求参数错误,请根据错误描述检查请求参数"); 20 | ERROR_INFO_MAP.put(30002, "SDKAppID 不匹配"); 21 | ERROR_INFO_MAP.put(30003, "请求的用户帐号不存在"); 22 | ERROR_INFO_MAP.put(30004, "请求需要 App 管理员权限"); 23 | ERROR_INFO_MAP.put(30005, "关系链字段中包含敏感词"); 24 | ERROR_INFO_MAP.put(30006, "服务器内部错误,请重试"); 25 | ERROR_INFO_MAP.put(30007, "网络超时,请稍后重试"); 26 | ERROR_INFO_MAP.put(30008, "并发写导致写冲突,建议使用批量方式"); 27 | ERROR_INFO_MAP.put(30009, "后台禁止该用户发起加好友请求"); 28 | ERROR_INFO_MAP.put(30010, "自己的好友数已达系统上限"); 29 | ERROR_INFO_MAP.put(30011, "分组已达系统上限"); 30 | ERROR_INFO_MAP.put(30012, "未决数已达系统上限"); 31 | ERROR_INFO_MAP.put(30014, "对方的好友数已达系统上限"); 32 | ERROR_INFO_MAP.put(30015, "请求添加好友时,对方在自己的黑名单中,不允许加好友"); 33 | ERROR_INFO_MAP.put(30016, "请求添加好友时,对方的加好友验证方式是不允许任何人添加自己为好友"); 34 | ERROR_INFO_MAP.put(30525, "请求添加好友时,自己在对方的黑名单中,不允许加好友"); 35 | ERROR_INFO_MAP.put(30539, "A 请求加 B 为好友,B 的加好友验证方式被设置为“AllowType_Type_NeedConfirm”,这时 A 与 B 之间只能形成未决关系,这个返回码用于标识加未决成功,以便与加好友成功的返回码区分开,调用方可以捕捉该错误给用户一个合理的提示"); 36 | ERROR_INFO_MAP.put(30540, "添加好友请求被安全策略打击,请勿频繁发起添加好友请求"); 37 | 38 | ERROR_INFO_MAP.put(40001, "请求参数错误,请根据错误描述检查请求参数"); 39 | ERROR_INFO_MAP.put(40003, "请求的用户帐号不存在"); 40 | ERROR_INFO_MAP.put(40004, "请求需要 App 管理员权限"); 41 | ERROR_INFO_MAP.put(40005, "资料字段中包含敏感词"); 42 | ERROR_INFO_MAP.put(40006, "服务器内部错误,请稍后重试"); 43 | ERROR_INFO_MAP.put(40007, "没有资料字段的读权限"); 44 | ERROR_INFO_MAP.put(40008, "没有资料字段的写权限"); 45 | ERROR_INFO_MAP.put(40009, "资料字段的 Tag 不存在"); 46 | ERROR_INFO_MAP.put(40601, "资料字段的 Value 长度超过500字节"); 47 | ERROR_INFO_MAP.put(40605, "标配资料字段的 Value错误"); 48 | ERROR_INFO_MAP.put(40610, "资料字段的 Value 类型不匹配"); 49 | 50 | ERROR_INFO_MAP.put(50001, "请求的用户帐号不存在。"); 51 | ERROR_INFO_MAP.put(50002, "请求参数错误,请根据错误描述检查请求是否正确。"); 52 | ERROR_INFO_MAP.put(50003, "请求需要 App 管理员权限。"); 53 | ERROR_INFO_MAP.put(50004, "服务端内部错误,请重试。"); 54 | ERROR_INFO_MAP.put(50005, "网络超时,请稍后重试。"); 55 | 56 | ERROR_INFO_MAP.put(60008, "服务请求超时或 HTTP 请求格式错误,请检查并重试。"); 57 | ERROR_INFO_MAP.put(70020, "SDKAppID 未找到,请在云通信 IM 控制台确认应用信息。"); 58 | ERROR_INFO_MAP.put(70052, "UserSig 已经失效,请重新生成,再次尝试。"); 59 | ERROR_INFO_MAP.put(70107, "请求的用户帐号不存在。"); 60 | ERROR_INFO_MAP.put(70169, "服务端内部超时,请重试"); 61 | ERROR_INFO_MAP.put(70398, "创建帐号数量超过免费体验版数量限制,请升级为专业版"); 62 | ERROR_INFO_MAP.put(70402, "参数非法,请检查必填字段是否填充,或者字段的填充是否满足协议要求"); 63 | ERROR_INFO_MAP.put(70403, "请求需要 App 管理员权限"); 64 | ERROR_INFO_MAP.put(70500, "服务器内部错误,请重试"); 65 | 66 | ERROR_INFO_MAP.put(90001, "JSON 格式解析失败,请检查请求包是否符合 JSON 规范。或者 To_Account 是空数组"); 67 | ERROR_INFO_MAP.put(90003, "JSON 格式请求包中 To_Account 不符合消息格式描述,请检查 To_Account 类型是否为 String"); 68 | ERROR_INFO_MAP.put(90005, "JSON 格式请求包体中缺少 MsgRandom 字段或者 MsgRandom 字段不是 Integer 类型"); 69 | ERROR_INFO_MAP.put(90006, "JSON 格式请求包体中缺少 MsgTimeStamp 字段或者 MsgTimeStamp 字段不是 Integer 类型"); 70 | ERROR_INFO_MAP.put(90007, "JSON 格式请求包体中 MsgBody 类型不是 Array 类型,请将其修改为 Array 类型"); 71 | ERROR_INFO_MAP.put(90008, "JSON 格式请求包体中缺少 From_Account 字段,或者From_Account 不存在"); 72 | ERROR_INFO_MAP.put(90009, "请求需要 App 管理员权限"); 73 | ERROR_INFO_MAP.put(90010, "JSON 格式请求包不符合消息格式描述,请参考 TIMMsgElement 对象 的定义。"); 74 | ERROR_INFO_MAP.put(90011, "批量发消息目标帐号超过500,请减少 To_Account 中目标帐号数量"); 75 | ERROR_INFO_MAP.put(90012, "To_Account 没有注册或不存在,请确认 To_Account 是否导入即时通信 IM 或者是否拼写错误。"); 76 | ERROR_INFO_MAP.put(90026, "消息离线存储时间错误(最多不能超过7天)。"); 77 | ERROR_INFO_MAP.put(90992, "后端服务超时,请重试"); 78 | ERROR_INFO_MAP.put(90994, "服务内部错误,请重试"); 79 | ERROR_INFO_MAP.put(90995, "服务内部错误,请重试"); 80 | ERROR_INFO_MAP.put(91000, "服务内部错误,请重试"); 81 | 82 | } 83 | 84 | public static String getErrorInfo(Integer i) { 85 | return ERROR_INFO_MAP.get(i); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMGroupService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | import com.sevlow.sdk.tim.bean.chat.MsgCustomContent; 4 | import com.sevlow.sdk.tim.bean.group.*; 5 | import com.sevlow.sdk.tim.common.error.TIMException; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author Element 11 | *

12 | * 群组管理 13 | * API Doc : https://cloud.tencent.com/document/product/269/1613 14 | */ 15 | public interface TIMGroupService { 16 | 17 | /** 18 | * 获取所有的群 19 | * 20 | * @param type 群组类型 21 | * @return 22 | * @throws TIMException 23 | */ 24 | AllGroupResult getAllGroup(GroupInfo.Type type) throws TIMException; 25 | 26 | /** 27 | * 分页获取群信息 28 | * 29 | * @param next 30 | * @param limit 31 | * @param type 32 | * @return 33 | * @throws TIMException 34 | */ 35 | AllGroupResult pageGroup(int next, int limit, GroupInfo.Type type) throws TIMException; 36 | 37 | /** 38 | * 创建群 39 | * 40 | * @param groupInfo 41 | * @return 42 | * @throws TIMException 43 | */ 44 | CreateGroupResult createGroup(GroupInfo groupInfo) throws TIMException; 45 | 46 | 47 | /** 48 | * 修改群资料 49 | * 50 | * @param groupInfo 51 | * @throws TIMException 52 | */ 53 | void updateGroup(GroupInfo groupInfo) throws TIMException; 54 | 55 | 56 | /** 57 | * 增加群组成员 58 | * 59 | * @param groupId 60 | * @param members 61 | * @param silenceEnum 静默模式,设置加群后是否下发系统通知。 62 | * @return 63 | * @throws TIMException 64 | */ 65 | AddGroupResult addGroupMember(String groupId, List members, SilenceEnum silenceEnum) throws TIMException; 66 | 67 | /** 68 | * 增加群组成员 69 | * 默认静默模式 : SilenceEnum.QUIET 70 | * 71 | * @param groupId 72 | * @param members 73 | * @return 74 | * @throws TIMException 75 | */ 76 | AddGroupResult addGroupMember(String groupId, List members) throws TIMException; 77 | 78 | /** 79 | * 删除群成员 80 | * 81 | * @param groupId 82 | * @param members 83 | * @param silenceEnum 静默模式,设置离群后是否下发系统通知。 84 | * @param reason 85 | * @throws TIMException 86 | */ 87 | void deleteGroupMember(String groupId, List members, SilenceEnum silenceEnum, String reason) throws TIMException; 88 | 89 | 90 | /** 91 | * 设置群成员资料 92 | * 93 | * @param groupMemberInfo 94 | * @throws TIMException 95 | */ 96 | void modifyGroupMemberInfo(GroupMemberInfo groupMemberInfo) throws TIMException; 97 | 98 | 99 | /** 100 | * 解散群 101 | * 102 | * @param groupId 103 | * @throws TIMException 104 | */ 105 | void destroyGroup(String groupId) throws TIMException; 106 | 107 | 108 | /** 109 | * 批量禁言和取消禁言 110 | * 111 | * @param groupId 112 | * @param members 113 | * @param time 禁言时间 <=0:取消禁言 114 | * @throws TIMException 115 | */ 116 | void forbidSendMsg(String groupId, List members, int time) throws TIMException; 117 | 118 | 119 | /** 120 | * 获取群组被禁言用户列表 121 | * 122 | * @param groupId 123 | * @return 124 | * @throws TIMException 125 | */ 126 | ShuttedMemberResult getShuttedMember(String groupId) throws TIMException; 127 | 128 | /** 129 | * 发送群普通消息 130 | * 131 | * @param groupId 132 | * @param account 133 | * @param isSentOnline true 则消息表示只在线下发,不存离线和漫游(AVChatRoom 和 BChatRoom 不允许使用)。 134 | * @param message 文本消息 135 | * @throws TIMException 136 | */ 137 | void sendGroupMsg(String groupId, String account, Boolean isSentOnline, String message) throws TIMException; 138 | 139 | /** 140 | * 发送自定义消息 141 | * 142 | * @param groupId 143 | * @param account 144 | * @param isSentOnline true 则消息表示只在线下发,不存离线和漫游(AVChatRoom 和 BChatRoom 不允许使用)。 145 | * @param message 自定义消息内容 146 | */ 147 | void sentGroupCustomMsg(String groupId, String account, Boolean isSentOnline, MsgCustomContent message) throws TIMException; 148 | 149 | /** 150 | * 发送通知消息 151 | * 152 | * @param toMembers 如果为空或者null,发送所有人 153 | */ 154 | void sendGroupSystemNotification(String groupId, List toMembers, String message) throws TIMException; 155 | 156 | /** 157 | * 发送所有人通知消息 158 | * 159 | * @param groupId 160 | * @param message 161 | * @throws TIMException 162 | */ 163 | void sendGroupSystemNotification(String groupId, String message) throws TIMException; 164 | 165 | /** 166 | * 导入群成员 167 | * 168 | * @param groupId 169 | * @param members 170 | * @return 171 | * @throws TIMException 172 | */ 173 | AddGroupResult importGroupMember(String groupId, List members) throws TIMException; 174 | 175 | 176 | } 177 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/impl/TIMProfileServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMProfileService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.bean.SnsItem; 6 | import com.sevlow.sdk.tim.bean.profile.GenderEnum; 7 | import com.sevlow.sdk.tim.bean.profile.TIMProfile; 8 | import com.sevlow.sdk.tim.bean.profile.UserProfileResult; 9 | import com.sevlow.sdk.tim.common.error.TIMError; 10 | import com.sevlow.sdk.tim.common.error.TIMException; 11 | import com.sevlow.sdk.tim.constant.TIMErrorConstant; 12 | import com.sevlow.sdk.tim.utils.JsonUtils; 13 | import lombok.NonNull; 14 | import lombok.extern.slf4j.Slf4j; 15 | 16 | import java.util.ArrayList; 17 | import java.util.HashMap; 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | /** 22 | * @author pengshiqing 23 | * @Date: 2019/7/26 24 | * @Description: 25 | */ 26 | @Slf4j 27 | public class TIMProfileServiceImpl implements TIMProfileService { 28 | 29 | private TIMService timService; 30 | 31 | private static final String CUSTOM_PROFILE_PREFIX = "Tag_Profile_Custom_"; 32 | 33 | public TIMProfileServiceImpl(TIMService timService) { 34 | this.timService = timService; 35 | } 36 | 37 | @Override 38 | public void setPortrait(@NonNull String identifier, TIMProfile imProfile, Map customProfile) throws TIMException { 39 | 40 | if (identifier == null) { 41 | throw new TIMException(new TIMError(50001, TIMErrorConstant.getErrorInfo(50001))); 42 | } 43 | 44 | List profileItemList = new ArrayList<>(); 45 | 46 | if (imProfile != null) { 47 | try { 48 | String jsonStr = JsonUtils.toJson(imProfile); 49 | Map imProfileMap = JsonUtils.fromJson(jsonStr, new HashMap().getClass()); 50 | 51 | for (Map.Entry entity : imProfileMap.entrySet()) { 52 | profileItemList.add(new SnsItem(entity.getKey(), entity.getValue())); 53 | } 54 | } catch (Exception e) { 55 | throw new TIMException(new TIMError(40001, TIMErrorConstant.getErrorInfo(40001))); 56 | } 57 | } 58 | 59 | if (customProfile != null) { 60 | String key; 61 | for (Map.Entry entity : customProfile.entrySet()) { 62 | key = entity.getKey(); 63 | if (key.length() > 8) { 64 | throw new TIMException(new TIMError(-1, "自定义资料字段不能超过8个字符")); 65 | } 66 | key = CUSTOM_PROFILE_PREFIX.concat(key); 67 | profileItemList.add(new SnsItem(key, entity.getValue())); 68 | } 69 | } 70 | 71 | if (profileItemList.size() < 1) { 72 | log.warn("当前未设置任何资料,不进行接口请求"); 73 | return; 74 | } 75 | 76 | String api = "v4/profile/portrait_set"; 77 | Map body = new HashMap<>(2); 78 | body.put("From_Account", identifier); 79 | body.put("ProfileItem", profileItemList); 80 | 81 | this.timService.post(api, body); 82 | 83 | } 84 | 85 | @Override 86 | public void setPortrait(@NonNull String identifier, TIMProfile imProfile) throws TIMException { 87 | this.setPortrait(identifier, imProfile, null); 88 | } 89 | 90 | @Override 91 | public void setPortrait(@NonNull String identifier, Map customProfile) throws TIMException { 92 | this.setPortrait(identifier, null, customProfile); 93 | } 94 | 95 | @Override 96 | public UserProfileResult getPortrait(@NonNull List accounts, List profiles, List customProfiles) throws TIMException { 97 | if (profiles == null && customProfiles == null) { 98 | throw new TIMException(new TIMError(40001, TIMErrorConstant.getErrorInfo(40001))); 99 | } 100 | 101 | if (accounts.isEmpty()) { 102 | throw new TIMException(new TIMError(40002, TIMErrorConstant.getErrorInfo(40002))); 103 | } 104 | 105 | if (accounts.size() > 100) { 106 | throw new TIMException(new TIMError(-1, "拉取资料的账号不能超过100个")); 107 | } 108 | 109 | List tagList = new ArrayList<>(); 110 | 111 | if (profiles != null && !profiles.isEmpty()) { 112 | tagList.addAll(profiles); 113 | } 114 | 115 | if (customProfiles != null && !customProfiles.isEmpty()) { 116 | for (String custom : customProfiles) { 117 | if (custom.length() > 8) { 118 | throw new TIMException(new TIMError(-1, "自定义资料字段不能超过8个字符")); 119 | } 120 | tagList.add(CUSTOM_PROFILE_PREFIX.concat(custom)); 121 | } 122 | } 123 | 124 | if (tagList.isEmpty()) { 125 | throw new TIMException(new TIMError(40001, TIMErrorConstant.getErrorInfo(40001))); 126 | } 127 | 128 | Map body = new HashMap<>(); 129 | body.put("To_Account", accounts); 130 | body.put("TagList", tagList); 131 | 132 | String api = "v4/profile/portrait_get"; 133 | 134 | return JsonUtils.fromJson(this.timService.post(api, body), UserProfileResult.class); 135 | } 136 | 137 | } 138 | -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/impl/TIMChatServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMChatService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.api.test.TestModule; 6 | import com.sevlow.sdk.tim.bean.chat.ChatMsgEnum; 7 | import com.sevlow.sdk.tim.bean.chat.MsgCustomContent; 8 | import com.sevlow.sdk.tim.common.error.TIMException; 9 | import com.sevlow.sdk.tim.utils.JsonUtils; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.testng.annotations.BeforeTest; 12 | import org.testng.annotations.Guice; 13 | import org.testng.annotations.Test; 14 | 15 | import javax.inject.Inject; 16 | import java.util.Arrays; 17 | import java.util.HashMap; 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | 22 | /** 23 | * @author pengshiqing 24 | * @Date: 2019/7/16 25 | * @Description: 26 | */ 27 | 28 | @Slf4j 29 | @Guice(modules = {TestModule.class}) 30 | public class TIMChatServiceImplTest { 31 | 32 | @Inject 33 | private TIMService timService; 34 | 35 | private TIMChatService chatService; 36 | 37 | 38 | @BeforeTest 39 | public void before() { 40 | this.chatService = timService.getChatService(); 41 | } 42 | 43 | 44 | @Test 45 | public void testBatchSendMsg() throws TIMException { 46 | 47 | String fromAccount = "admin" ; 48 | 49 | List toAccount = Arrays.asList("test_211","test_3","sssddasdfewrfew"); 50 | 51 | List msg = Arrays.asList("你好"); 52 | 53 | chatService.batchSendTextMsg(fromAccount,toAccount,msg); 54 | 55 | } 56 | 57 | @Test 58 | public void testSendTextMsg() throws TIMException { 59 | String fromAccount = "69887072709640192" ; 60 | String toAccount = "119384433261281280";// 119384433261281280 73071536809967616 61 | List msg = Arrays.asList("你好"); 62 | chatService.sendTextMsg(fromAccount,toAccount,msg); 63 | 64 | } 65 | 66 | @Test 67 | public void testSendCustomMsg() throws TIMException { 68 | String fromAccount = "74897564679274496" ; 69 | String toAccount = "73071536809967616"; 70 | 71 | Map data = new HashMap<>(); 72 | data.put("type","2"); 73 | data.put("money","10"); 74 | data.put("money","10"); 75 | data.put("ext","www.qq.com"); 76 | data.put("desc","hello"); 77 | 78 | MsgCustomContent msg = new MsgCustomContent(); 79 | msg.setData(JsonUtils.toJson(data)); 80 | msg.setDesc("hello"); 81 | msg.setExt("www.qq.com"); 82 | msg.setSound("dingdong.aiff"); 83 | chatService.sendCustomMsg(fromAccount,toAccount,msg); 84 | } 85 | 86 | @Test 87 | public void testBatchSendCustomMsg() throws TIMException { 88 | String fromAccount = "admin" ; 89 | 90 | List toAccount = Arrays.asList("71243920540958720"); 91 | 92 | Map data = new HashMap<>(); 93 | data.put("money","10"); 94 | data.put("userId","123456789"); 95 | 96 | MsgCustomContent msg = new MsgCustomContent(); 97 | msg.setData(JsonUtils.toJson(data)); 98 | msg.setDesc("hello"); 99 | msg.setExt("www.qq.com"); 100 | msg.setSound("dingdong.aiff"); 101 | 102 | chatService.batchSendCustomMsg(fromAccount,toAccount,msg); 103 | } 104 | 105 | 106 | 107 | @Test 108 | public void testBatchSendMsg2() throws TIMException { 109 | 110 | String fromAccount = "admin" ; 111 | 112 | List toAccount = Arrays.asList("test_211","test_3","sssddasdfewrfew"); 113 | 114 | List msg = Arrays.asList("你好"); 115 | 116 | chatService.batchSendTextMsg(fromAccount,toAccount,msg, ChatMsgEnum.SYNC); 117 | 118 | } 119 | 120 | @Test 121 | public void testSendTextMsg2() throws TIMException { 122 | String fromAccount = "69887072709640192" ; 123 | String toAccount = "71243920540958720"; 124 | List msg = Arrays.asList("你好"); 125 | chatService.sendTextMsg(fromAccount,toAccount,msg, ChatMsgEnum.SYNC); 126 | 127 | } 128 | 129 | @Test 130 | public void testSendCustomMsg2() throws TIMException { 131 | String fromAccount = "74897564679274496" ; 132 | String toAccount = "73071536809967616"; 133 | 134 | Map data = new HashMap<>(); 135 | data.put("type","2"); 136 | data.put("money","10"); 137 | data.put("money","10"); 138 | data.put("ext","www.qq.com"); 139 | data.put("desc","hello"); 140 | 141 | MsgCustomContent msg = new MsgCustomContent(); 142 | msg.setData(JsonUtils.toJson(data)); 143 | msg.setDesc("hello"); 144 | msg.setExt("www.qq.com"); 145 | msg.setSound("dingdong.aiff"); 146 | chatService.sendCustomMsg(fromAccount,toAccount,msg, ChatMsgEnum.NO_SYNC); 147 | } 148 | 149 | @Test 150 | public void testBatchSendCustomMsg2() throws TIMException { 151 | String fromAccount = "admin" ; 152 | 153 | List toAccount = Arrays.asList("71243920540958720"); 154 | 155 | Map data = new HashMap<>(); 156 | data.put("money","10"); 157 | data.put("userId","123456789"); 158 | 159 | MsgCustomContent msg = new MsgCustomContent(); 160 | msg.setData(JsonUtils.toJson(data)); 161 | msg.setDesc("hello"); 162 | msg.setExt("www.qq.com"); 163 | msg.setSound("dingdong.aiff"); 164 | 165 | chatService.batchSendCustomMsg(fromAccount,toAccount,msg, ChatMsgEnum.NO_SYNC); 166 | } 167 | } -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/impl/TIMGroupServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMGroupService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.api.test.TestModule; 6 | import com.sevlow.sdk.tim.bean.chat.MsgCustomContent; 7 | import com.sevlow.sdk.tim.bean.group.*; 8 | import com.sevlow.sdk.tim.common.error.TIMException; 9 | import com.sevlow.sdk.tim.utils.JsonUtils; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.testng.Assert; 12 | import org.testng.annotations.BeforeTest; 13 | import org.testng.annotations.Guice; 14 | import org.testng.annotations.Test; 15 | 16 | import javax.inject.Inject; 17 | import java.util.*; 18 | 19 | /** 20 | * 群组服务测试用例 21 | * 22 | * @author element 23 | */ 24 | @Slf4j 25 | @Guice(modules = {TestModule.class}) 26 | public class TIMGroupServiceImplTest { 27 | 28 | @Inject 29 | private TIMService timService; 30 | 31 | private TIMGroupService groupService; 32 | 33 | @BeforeTest 34 | public void before() { 35 | this.groupService = timService.getGroupService(); 36 | } 37 | 38 | 39 | @Test 40 | public void testGetAllGroup() throws TIMException { 41 | AllGroupResult allGroup = groupService.getAllGroup(GroupInfo.Type.Public); 42 | Assert.assertNotNull(allGroup); 43 | } 44 | 45 | @Test 46 | public void testPageAllGroup() throws TIMException { 47 | AllGroupResult allGroup = groupService.pageGroup(0, 10, GroupInfo.Type.Public); 48 | Assert.assertNotNull(allGroup); 49 | } 50 | 51 | 52 | @Test 53 | public void testCreateGroup() throws TIMException { 54 | GroupInfo groupInfo = new GroupInfo(); 55 | groupInfo.setGroupId("119384433261281280"); 56 | groupInfo.setType(GroupInfo.Type.Public); 57 | groupInfo.setApplyJoinOption(GroupInfo.ApplyJoinOption.FreeAccess); 58 | groupInfo.setName("147"); 59 | log.debug(groupInfo.toString()); 60 | CreateGroupResult group = groupService.createGroup(groupInfo); 61 | Assert.assertNotNull(group); 62 | } 63 | 64 | @Test 65 | public void testUpdateGroup() throws TIMException { 66 | GroupInfo groupInfo = new GroupInfo(); 67 | groupInfo.setGroupId("@TGS#3XWYJA6FQ"); 68 | groupInfo.setType(GroupInfo.Type.Public); 69 | groupInfo.setApplyJoinOption(GroupInfo.ApplyJoinOption.FreeAccess); 70 | groupInfo.setName("147"); 71 | log.debug(groupInfo.toString()); 72 | groupService.updateGroup(groupInfo); 73 | } 74 | 75 | 76 | @Test 77 | public void testJoinGroup() throws TIMException { 78 | List list = Arrays.asList("69887072709640192", "74897564679274496"); 79 | AddGroupResult addGroupResult = groupService.addGroupMember("119384433261281280", list, SilenceEnum.QUIET); 80 | Assert.assertTrue(addGroupResult.getMemberList().size() > 0); 81 | } 82 | 83 | 84 | @Test 85 | public void testDeleteGroupMember() throws TIMException { 86 | List list = Arrays.asList("74897564679274496", "74897564679274496"); 87 | groupService.deleteGroupMember("11231", list, SilenceEnum.QUIET, null); 88 | 89 | } 90 | 91 | 92 | @Test 93 | public void testModifyGroupMemberInfo() throws TIMException { 94 | GroupMemberInfo info = new GroupMemberInfo(); 95 | info.setGroupId("@TGS#3XWYJA6FQ"); 96 | info.setMemberAccount("74940051523371008"); 97 | info.setRole(GroupMemberInfo.Role.Admin); 98 | info.setNameCard("你好"); 99 | info.setMsgFlag(GroupMemberInfo.MsgFlag.AcceptNotNotify); 100 | groupService.modifyGroupMemberInfo(info); 101 | 102 | } 103 | 104 | 105 | @Test 106 | public void testDestroyGroup() throws TIMException { 107 | groupService.destroyGroup("@TGS#3XWYJA6FQ"); 108 | } 109 | 110 | @Test 111 | public void testForbidSendMsg() throws TIMException { 112 | groupService.forbidSendMsg("11231", Arrays.asList("74940051523371008"), 100); 113 | } 114 | 115 | 116 | @Test 117 | public void testGetShuttedMember() throws TIMException { 118 | ShuttedMemberResult member = groupService.getShuttedMember("11231"); 119 | Assert.assertNotNull(member); 120 | } 121 | 122 | 123 | @Test 124 | public void testSendGroupMsg() throws TIMException { 125 | groupService.sendGroupMsg("119384433261281280", null, true, "你好11"); 126 | } 127 | 128 | 129 | @Test 130 | public void testSendGroupSystemNotification() throws TIMException { 131 | groupService.sendGroupSystemNotification("119384433261281280", null, "你好111"); 132 | } 133 | 134 | @Test 135 | public void testSentGroupCustomMsg() throws TIMException { 136 | Map map = new HashMap(); 137 | map.put("action", "9"); 138 | map.put("type", "8"); 139 | map.put("name", "你好"); 140 | 141 | MsgCustomContent msg = new MsgCustomContent(); 142 | msg.setSound("dingdong.aiff"); 143 | msg.setExt("您有一条系统消息"); 144 | msg.setDesc("您有一条系统消息"); 145 | 146 | msg.setData(JsonUtils.toJson(map)); 147 | groupService.sentGroupCustomMsg("119384433261281280", null, null, msg); 148 | } 149 | 150 | 151 | @Test 152 | public void testImportGroupMember() throws TIMException { 153 | // 117954688241893376 72779248246456320 69887072709640192 17612021831 154 | List members = new ArrayList<>(); 155 | ImportMember member1 = new ImportMember("72356624357916672"); 156 | ImportMember member2 = new ImportMember("69887072709640192"); 157 | ImportMember member3 = new ImportMember("17612021831"); 158 | members.add(member1); 159 | members.add(member2); 160 | members.add(member3); 161 | AddGroupResult result = groupService.importGroupMember("117954688241893376", members); 162 | Assert.assertNotNull(result); 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /src/test/java/com/sevlow/sdk/tim/api/impl/TIMRelationServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMRelationService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.api.test.TestModule; 6 | import com.sevlow.sdk.tim.bean.*; 7 | import com.sevlow.sdk.tim.bean.profile.TIMProfile; 8 | import com.sevlow.sdk.tim.bean.relation.ListFriendsDirectivityResult; 9 | import com.sevlow.sdk.tim.bean.relation.ListFriendsResult; 10 | import com.sevlow.sdk.tim.bean.relation.TIMFriendProfile; 11 | import com.sevlow.sdk.tim.common.error.TIMException; 12 | import lombok.extern.slf4j.Slf4j; 13 | import org.testng.Assert; 14 | import org.testng.annotations.BeforeTest; 15 | import org.testng.annotations.Guice; 16 | import org.testng.annotations.Test; 17 | import org.testng.collections.Lists; 18 | 19 | import javax.inject.Inject; 20 | import java.util.ArrayList; 21 | import java.util.Arrays; 22 | import java.util.List; 23 | 24 | /** 25 | * @author Element 26 | * @Package com.sevlow.sdk.tim.api.impl 27 | * @date 2019-05-27 23:39 28 | * @Description: 29 | */ 30 | @Slf4j 31 | @Guice(modules = {TestModule.class}) 32 | public class TIMRelationServiceImplTest { 33 | 34 | @Inject 35 | private TIMService timService; 36 | 37 | private TIMRelationService relationService; 38 | 39 | @BeforeTest 40 | public void before() { 41 | this.relationService = timService.getRelationService(); 42 | } 43 | 44 | @Test 45 | public void testAddFriends() throws TIMException { 46 | 47 | AddFriendsResult result = relationService.addFriends("69887072709640192", Arrays.asList("72356624357916672", "71046340049633280", "test_4"), "forTest"); 48 | 49 | Assert.assertEquals(3, result.getResultItems().size()); 50 | } 51 | 52 | @Test 53 | public void testImportFried() throws TIMException { 54 | ImportFriendsResult result = relationService.importFriends("test_1", Arrays.asList("test_2", "test_3", "test_4"), "forTest"); 55 | Assert.assertEquals(result.getResultItems().size(), 3); 56 | } 57 | 58 | @Test 59 | public void testUpdateFriends() throws TIMException { 60 | 61 | List snsItemList = new ArrayList<>(); 62 | SnsItem snsItem = new SnsItem(); 63 | 64 | snsItem.setTag("tag1"); 65 | snsItem.setValue("value1"); 66 | 67 | snsItemList.add(snsItem); 68 | 69 | snsItem = new SnsItem(); 70 | 71 | snsItem.setTag("tag2"); 72 | snsItem.setValue(2); 73 | 74 | snsItemList.add(snsItem); 75 | 76 | UpdateFriendsResult result = relationService.updateFriends("test_1", Arrays.asList("test_2", "test_3", "test_4"), null); 77 | 78 | Assert.assertEquals(result.getResultItems().size(), 3); 79 | 80 | } 81 | 82 | @Test 83 | public void testDeleteFriends() throws TIMException { 84 | 85 | DeleteFriendsResult result = relationService.deleteFriend("test_1", Arrays.asList("test_2", "test_3")); 86 | 87 | Assert.assertEquals(result.getResultItems().size(), 2); 88 | 89 | } 90 | 91 | 92 | @Test 93 | public void testEmptyFriends() throws TIMException { 94 | 95 | String identifier = "test_1"; 96 | relationService.emptyFriends(identifier); 97 | 98 | } 99 | 100 | 101 | @Test 102 | public void testCheckFriends() throws TIMException { 103 | String identifier = "74516245050818560"; 104 | List list = Arrays.asList("74518861914832896"); 105 | CheckFriendsResult result = relationService.checkFriends(identifier, list); 106 | Assert.assertEquals(2, result.getInfoItems().size()); 107 | 108 | } 109 | 110 | @Test 111 | public void testListFriends() throws TIMException{ 112 | 113 | String account = "10001"; 114 | Integer startIndex = 0; 115 | 116 | ListFriendsResult result = relationService.listFriends(account,startIndex); 117 | Assert.assertEquals(result.getFriendNum(),Integer.valueOf(0)); 118 | } 119 | 120 | @Test 121 | public void testListFriendsDirectivity() throws TIMException{ 122 | String account = "119020659249512448"; 123 | List friends = Lists.newArrayList("119029328846520320","119097887090016256","118283730413420544"); 124 | List profiles = Lists.newArrayList(TIMProfile.TAG_PROFILE_IM_NICK,TIMProfile.TAG_PROFILE_IM_GENDER); 125 | List customProfiles = Lists.newArrayList("college"); 126 | List snsProfiles = Lists.newArrayList(TIMFriendProfile.TAG_SNS_IM_REMARK); 127 | ListFriendsDirectivityResult result = relationService.listFriendsDirectivity(account,friends,profiles,customProfiles,snsProfiles,null); 128 | 129 | } 130 | 131 | 132 | @Test 133 | public void testAddBlockAccounts() throws TIMException { 134 | String identifier = "test_1"; 135 | List account = Arrays.asList("test_2"); 136 | AddBlockAccountsResult result = relationService.addBlockAccounts(identifier, account); 137 | Assert.assertEquals(result.getResultItems().size(), 1); 138 | 139 | } 140 | 141 | 142 | @Test 143 | public void testRemoveblockAccounts() throws TIMException { 144 | 145 | String identifier = "test_1"; 146 | List account = Arrays.asList("test_2"); 147 | RemoveBlockAccountsResult result = relationService.removeblockAccounts(identifier, account); 148 | Assert.assertEquals(result.getResultItems().size(), 1); 149 | 150 | } 151 | 152 | @Test 153 | public void testListBlockAccounts() throws TIMException { 154 | 155 | String identifier = "test_1"; 156 | Integer offset = 0; 157 | Integer rows = 1; 158 | Integer lastSequence = 0; 159 | ListBlockAccountsResult res = relationService.listBlockAccounts(identifier, offset, rows, lastSequence); 160 | Assert.assertTrue(res.getCurruentSequence() > 0); 161 | } 162 | 163 | @Test 164 | public void testCheckBlockAccounts() throws TIMException { 165 | 166 | String identifier = "test_1"; 167 | List accounts = Arrays.asList("test_2", "test_3"); 168 | CheckBlockAccountsResult result = relationService.checkBlockAccounts(identifier, accounts, TIMRelationService.BlackCheckType.BlackCheckResult_Type_Both); 169 | Assert.assertTrue(result.getBlackListCheckItems().size() > 0); 170 | } 171 | 172 | @Test 173 | public void testAddGroups() throws TIMException { 174 | String identifier = "test_1"; 175 | List groupNames = Arrays.asList("group123"); 176 | AddGroupsResult res = relationService.addGroups(identifier, groupNames, null); 177 | Assert.assertTrue(res.getResultItems().size() >= 0); 178 | } 179 | 180 | @Test 181 | public void testDeleteGroups() throws TIMException { 182 | String identifier = "test_1"; 183 | List groupNames = Arrays.asList("group123"); 184 | DeleteGroupsResult res = relationService.deleteGroups(identifier, groupNames); 185 | Assert.assertTrue(res.getCurrentSequence() >= 0); 186 | 187 | } 188 | 189 | @Test 190 | public void remarkFriend() throws TIMException{ 191 | relationService.remarkFriend("69887072709640192","72356624357916672","你好测试"); 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/impl/TIMChatServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMChatService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.bean.chat.ChatMsgEnum; 6 | import com.sevlow.sdk.tim.bean.chat.MsgBody; 7 | import com.sevlow.sdk.tim.bean.chat.MsgContent; 8 | import com.sevlow.sdk.tim.bean.chat.MsgCustomContent; 9 | import com.sevlow.sdk.tim.common.error.TIMError; 10 | import com.sevlow.sdk.tim.common.error.TIMException; 11 | import com.sevlow.sdk.tim.constant.TIMErrorConstant; 12 | import lombok.NonNull; 13 | import lombok.extern.slf4j.Slf4j; 14 | import org.apache.commons.lang3.RandomUtils; 15 | 16 | import java.util.ArrayList; 17 | import java.util.HashMap; 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | /** 22 | * @author pengshiqing 23 | * @Date: 2019/7/16 24 | * @Description: 25 | */ 26 | 27 | @Slf4j 28 | public class TIMChatServiceImpl implements TIMChatService { 29 | 30 | private TIMService timService; 31 | 32 | public TIMChatServiceImpl(TIMService timService) { 33 | this.timService = timService; 34 | } 35 | 36 | /** 37 | * 批量消息 38 | * @param fromAccount 指定发送账号 39 | * @param toAccounts 群发接收账号集合 40 | * @param msgList 消息集合 41 | */ 42 | @Override 43 | public void batchSendTextMsg(String fromAccount, List toAccounts, List msgList) throws TIMException { 44 | batchSendTextMsg(fromAccount,toAccounts,msgList,null); 45 | } 46 | 47 | @Override 48 | public void batchSendTextMsg(String fromAccount, List toAccounts, List msgList, ChatMsgEnum msgEnum) throws TIMException { 49 | if (toAccounts == null || toAccounts.size() > 500) { 50 | throw new TIMException(new TIMError(90011, TIMErrorConstant.getErrorInfo(90011))); 51 | } 52 | if (msgEnum == null){ 53 | msgEnum = ChatMsgEnum.SYNC ; 54 | } 55 | String api = "v4/openim/batchsendmsg"; 56 | 57 | Map body = new HashMap<>(4); 58 | body.put("SyncOtherMachine", msgEnum.getType()); 59 | body.put("From_Account",fromAccount); 60 | body.put("To_Account",toAccounts); 61 | body.put("MsgRandom", RandomUtils.nextInt(10000000,99999999)); 62 | 63 | List msgBodies = new ArrayList<>(); 64 | 65 | for (String msg : msgList) { 66 | MsgBody msgBody = new MsgBody(); 67 | msgBody.setMsgType("TIMTextElem"); 68 | msgBody.setMsgContent(new MsgContent(msg)); 69 | msgBodies.add(msgBody); 70 | } 71 | body.put("MsgBody",msgBodies); 72 | 73 | this.timService.post(api, body); 74 | } 75 | 76 | /** 77 | * 批量自定义消息 78 | * 79 | * @param fromAccount 指定发送账号 80 | * @param toAccounts 群发接收账号集合 81 | * @param msgCustomContent 消息集合 82 | */ 83 | @Override 84 | public void batchSendCustomMsg(String fromAccount, List toAccounts, @NonNull MsgCustomContent msgCustomContent) throws TIMException { 85 | batchSendCustomMsg(fromAccount,toAccounts,msgCustomContent,null); 86 | } 87 | 88 | @Override 89 | public void batchSendCustomMsg(String fromAccount, List toAccounts, @NonNull MsgCustomContent msgCustomContent, ChatMsgEnum msgEnum) throws TIMException { 90 | if (toAccounts == null || toAccounts.size() > 500) { 91 | throw new TIMException(new TIMError(90011, TIMErrorConstant.getErrorInfo(90011))); 92 | } 93 | 94 | if (msgEnum == null){ 95 | msgEnum = ChatMsgEnum.SYNC ; 96 | } 97 | 98 | String api = "v4/openim/batchsendmsg"; 99 | 100 | Map body = new HashMap<>(4); 101 | body.put("SyncOtherMachine", msgEnum.getType()); 102 | body.put("From_Account",fromAccount); 103 | body.put("To_Account",toAccounts); 104 | body.put("MsgRandom", RandomUtils.nextInt(10000000,99999999)); 105 | 106 | List msgBodies = new ArrayList<>(); 107 | 108 | MsgBody msgBody = new MsgBody(); 109 | msgBody.setMsgType("TIMCustomElem"); 110 | msgBody.setMsgContent(msgCustomContent); 111 | 112 | msgBodies.add(msgBody); 113 | 114 | body.put("MsgBody",msgBodies); 115 | 116 | this.timService.post(api, body); 117 | } 118 | 119 | /** 120 | * 发送单聊消息 121 | * 122 | * @param fromAccount 指定发送账号 123 | * @param toAccount 群发接收账号集合 124 | * @param msgList 消息集合 125 | */ 126 | @Override 127 | public void sendTextMsg(String fromAccount, String toAccount, List msgList) throws TIMException { 128 | sendTextMsg(fromAccount,toAccount,msgList,null); 129 | } 130 | 131 | @Override 132 | public void sendTextMsg(String fromAccount, String toAccount, List msgList, ChatMsgEnum msgEnum) throws TIMException { 133 | String api = "v4/openim/sendmsg"; 134 | 135 | if (msgEnum == null){ 136 | msgEnum = ChatMsgEnum.SYNC ; 137 | } 138 | 139 | Map body = new HashMap<>(4); 140 | body.put("SyncOtherMachine", msgEnum.getType()); 141 | body.put("From_Account",fromAccount); 142 | body.put("To_Account",toAccount); 143 | body.put("MsgRandom", RandomUtils.nextInt(10000000,99999999)); 144 | 145 | List msgBodies = new ArrayList<>(); 146 | 147 | for (String msg : msgList) { 148 | MsgBody msgBody = new MsgBody(); 149 | msgBody.setMsgType("TIMTextElem"); 150 | msgBody.setMsgContent(new MsgContent(msg)); 151 | msgBodies.add(msgBody); 152 | } 153 | body.put("MsgBody",msgBodies); 154 | 155 | this.timService.post(api, body); 156 | } 157 | 158 | /** 159 | * 发送单聊自定义消息 160 | * 161 | * @param fromAccount 指定发送账号 162 | * @param toAccount 群发接收账号集合 163 | * @param msgCustomContent 消息集合 164 | */ 165 | @Override 166 | public void sendCustomMsg(@NonNull String fromAccount, @NonNull String toAccount, @NonNull MsgCustomContent msgCustomContent) throws TIMException { 167 | sendCustomMsg(fromAccount,toAccount,msgCustomContent,null); 168 | } 169 | 170 | @Override 171 | public void sendCustomMsg(@NonNull String fromAccount, @NonNull String toAccount, @NonNull MsgCustomContent msgCustomContent, ChatMsgEnum msgEnum) throws TIMException { 172 | String api = "v4/openim/sendmsg"; 173 | 174 | if (msgEnum == null){ 175 | msgEnum = ChatMsgEnum.SYNC ; 176 | } 177 | 178 | Map body = new HashMap<>(4); 179 | body.put("SyncOtherMachine", msgEnum.getType()); 180 | body.put("From_Account",fromAccount); 181 | body.put("To_Account",toAccount); 182 | body.put("MsgRandom", RandomUtils.nextInt(10000000,99999999)); 183 | 184 | List msgBodies = new ArrayList<>(); 185 | 186 | MsgBody msgBody = new MsgBody(); 187 | msgBody.setMsgType("TIMCustomElem"); 188 | msgBody.setMsgContent(msgCustomContent); 189 | msgBodies.add(msgBody); 190 | body.put("MsgBody",msgBodies); 191 | 192 | this.timService.post(api, body); 193 | } 194 | 195 | 196 | } 197 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/impl/TIMGroupServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMGroupService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.bean.chat.MsgBody; 6 | import com.sevlow.sdk.tim.bean.chat.MsgCustomContent; 7 | import com.sevlow.sdk.tim.bean.group.*; 8 | import com.sevlow.sdk.tim.common.error.TIMException; 9 | import com.sevlow.sdk.tim.utils.JsonUtils; 10 | import org.apache.commons.collections.CollectionUtils; 11 | import org.apache.commons.lang3.RandomUtils; 12 | import org.apache.commons.lang3.StringUtils; 13 | 14 | import java.util.*; 15 | import java.util.stream.Collectors; 16 | 17 | /** 18 | * @author pengshiqing 19 | * @Date: 2019/11/19 20 | * @Description: 21 | */ 22 | public class TIMGroupServiceImpl implements TIMGroupService { 23 | 24 | private TIMService timService; 25 | 26 | public TIMGroupServiceImpl(TIMService timService) { 27 | this.timService = timService; 28 | } 29 | 30 | 31 | @Override 32 | public AllGroupResult getAllGroup(GroupInfo.Type type) throws TIMException { 33 | 34 | Map map = new HashMap(2); 35 | 36 | if (type != null) { 37 | map.put("GroupType", type); 38 | } 39 | return this.getGroup(map); 40 | } 41 | 42 | 43 | @Override 44 | public AllGroupResult pageGroup(int next, int limit, GroupInfo.Type type) throws TIMException { 45 | 46 | Map map = new HashMap(2); 47 | map.put("Next", next); 48 | map.put("Limit", limit); 49 | if (type != null) { 50 | map.put("GroupType", type); 51 | } 52 | return this.getGroup(map); 53 | } 54 | 55 | AllGroupResult getGroup(Map map) throws TIMException { 56 | String api = "v4/group_open_http_svc/get_appid_group_list"; 57 | return JsonUtils.fromJson(this.timService.post(api, map), AllGroupResult.class); 58 | } 59 | 60 | @Override 61 | public CreateGroupResult createGroup(GroupInfo groupInfo) throws TIMException { 62 | 63 | String api = "v4/group_open_http_svc/create_group"; 64 | 65 | return JsonUtils.fromJson(this.timService.post(api, groupInfo), CreateGroupResult.class); 66 | } 67 | 68 | @Override 69 | public void updateGroup(GroupInfo groupInfo) throws TIMException { 70 | 71 | String api = "v4/group_open_http_svc/modify_group_base_info"; 72 | 73 | this.timService.post(api, groupInfo); 74 | } 75 | 76 | @Override 77 | public AddGroupResult addGroupMember(String groupId, List members, SilenceEnum silenceEnum) throws TIMException { 78 | 79 | String api = "v4/group_open_http_svc/add_group_member"; 80 | 81 | Map map = new HashMap<>(3); 82 | map.put("GroupId", groupId); 83 | if (silenceEnum != null) { 84 | map.put("Silence", silenceEnum.getType()); 85 | } 86 | 87 | List memberList = new ArrayList<>(); 88 | 89 | if (CollectionUtils.isNotEmpty(members)) { 90 | memberList = members.stream().map(MemberAccount::new).collect(Collectors.toList()); 91 | } 92 | 93 | map.put("MemberList", memberList); 94 | 95 | return JsonUtils.fromJson(this.timService.post(api, map), AddGroupResult.class); 96 | } 97 | 98 | @Override 99 | public AddGroupResult addGroupMember(String groupId, List members) throws TIMException { 100 | return this.addGroupMember(groupId, members, null); 101 | } 102 | 103 | 104 | @Override 105 | public void deleteGroupMember(String groupId, List members, SilenceEnum silenceEnum, String reason) throws TIMException { 106 | String api = "v4/group_open_http_svc/delete_group_member"; 107 | 108 | Map map = new HashMap<>(3); 109 | map.put("GroupId", groupId); 110 | 111 | if (silenceEnum != null) { 112 | map.put("Silence", silenceEnum.getType()); 113 | } 114 | if (StringUtils.isNotBlank(reason)) { 115 | map.put("Reason", reason); 116 | } 117 | map.put("MemberToDel_Account", members); 118 | this.timService.post(api, map); 119 | } 120 | 121 | 122 | @Override 123 | public void modifyGroupMemberInfo(GroupMemberInfo groupMemberInfo) throws TIMException { 124 | String api = "v4/group_open_http_svc/modify_group_member_info"; 125 | this.timService.post(api, groupMemberInfo); 126 | } 127 | 128 | @Override 129 | public void destroyGroup(String groupId) throws TIMException { 130 | String api = "v4/group_open_http_svc/destroy_group"; 131 | Map map = new HashMap(2); 132 | map.put("GroupId", groupId); 133 | this.timService.post(api, map); 134 | } 135 | 136 | @Override 137 | public void forbidSendMsg(String groupId, List members, int time) throws TIMException { 138 | String api = "v4/group_open_http_svc/forbid_send_msg"; 139 | Map map = new HashMap(2); 140 | if (time < 0) { 141 | time = 0; 142 | } 143 | map.put("GroupId", groupId); 144 | map.put("Members_Account", members); 145 | map.put("ShutUpTime", time); 146 | this.timService.post(api, map); 147 | } 148 | 149 | 150 | @Override 151 | public ShuttedMemberResult getShuttedMember(String groupId) throws TIMException { 152 | String api = "v4/group_open_http_svc/get_group_shutted_uin"; 153 | 154 | Map map = new HashMap<>(3); 155 | map.put("GroupId", groupId); 156 | return JsonUtils.fromJson(this.timService.post(api, map), ShuttedMemberResult.class); 157 | } 158 | 159 | 160 | @Override 161 | public void sendGroupMsg(String groupId, String account, Boolean isSentOnline, String message) throws TIMException { 162 | String api = "v4/group_open_http_svc/send_group_msg"; 163 | 164 | Map map = new HashMap<>(5); 165 | map.put("GroupId", groupId); 166 | if (StringUtils.isNotBlank(account)) { 167 | map.put("From_Account", account); 168 | } 169 | if (isSentOnline != null && isSentOnline) { 170 | map.put("OnlineOnlyFlag", 1); 171 | } 172 | map.put("Random", RandomUtils.nextInt(1000000, 10000000)); 173 | 174 | map.put("MsgBody", Collections.singletonList(new TIMTextElem(message))); 175 | 176 | this.timService.post(api, map); 177 | } 178 | 179 | /** 180 | * 发送自定义消息 181 | * 182 | * @param groupId 183 | * @param account 184 | * @param isSentOnline true 则消息表示只在线下发,不存离线和漫游(AVChatRoom 和 BChatRoom 不允许使用)。 185 | * @param message 自定义消息内容 186 | */ 187 | @Override 188 | public void sentGroupCustomMsg(String groupId, String account, Boolean isSentOnline, MsgCustomContent message) throws TIMException { 189 | String api = "v4/group_open_http_svc/send_group_msg"; 190 | 191 | 192 | Map body = new HashMap<>(5); 193 | body.put("GroupId", groupId); 194 | if (StringUtils.isNotBlank(account)) { 195 | body.put("From_Account", account); 196 | } 197 | if (isSentOnline != null && isSentOnline) { 198 | body.put("OnlineOnlyFlag", 1); 199 | } 200 | body.put("Random", RandomUtils.nextInt(1000000, 10000000)); 201 | 202 | List msgBodies = new ArrayList<>(); 203 | 204 | MsgBody msgBody = new MsgBody(); 205 | msgBody.setMsgType("TIMCustomElem"); 206 | msgBody.setMsgContent(message); 207 | msgBodies.add(msgBody); 208 | body.put("MsgBody", msgBodies); 209 | 210 | this.timService.post(api, body); 211 | } 212 | 213 | 214 | @Override 215 | public void sendGroupSystemNotification(String groupId, List toMembers, String message) throws TIMException { 216 | String api = "v4/group_open_http_svc/send_group_system_notification"; 217 | 218 | Map map = new HashMap<>(5); 219 | map.put("GroupId", groupId); 220 | map.put("Content", message); 221 | if (CollectionUtils.isNotEmpty(toMembers)) { 222 | map.put("ToMembers_Account", toMembers); 223 | } 224 | this.timService.post(api, map); 225 | } 226 | 227 | @Override 228 | public void sendGroupSystemNotification(String groupId, String message) throws TIMException { 229 | this.sendGroupSystemNotification(groupId, null, message); 230 | } 231 | 232 | @Override 233 | public AddGroupResult importGroupMember(String groupId, List members) throws TIMException { 234 | String api = "v4/group_open_http_svc/import_group_member"; 235 | Map map = new HashMap<>(3); 236 | map.put("GroupId", groupId); 237 | map.put("MemberList", members); 238 | return JsonUtils.fromJson(this.timService.post(api, map), AddGroupResult.class); 239 | } 240 | 241 | 242 | } 243 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/impl/TIMServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.*; 4 | import com.sevlow.sdk.tim.common.TLSSigature; 5 | import com.sevlow.sdk.tim.common.error.TIMError; 6 | import com.sevlow.sdk.tim.common.error.TIMException; 7 | import com.sevlow.sdk.tim.config.TIMConfig; 8 | import com.sevlow.sdk.tim.utils.JsonUtils; 9 | import lombok.NonNull; 10 | import lombok.extern.slf4j.Slf4j; 11 | import okhttp3.*; 12 | import org.apache.commons.io.FileUtils; 13 | import org.apache.commons.io.IOUtils; 14 | import org.apache.commons.lang3.RegExUtils; 15 | 16 | import java.io.File; 17 | import java.io.InputStream; 18 | import java.net.SocketTimeoutException; 19 | import java.util.Iterator; 20 | import java.util.Map; 21 | 22 | 23 | /** 24 | * @author element 25 | */ 26 | @Slf4j 27 | public class TIMServiceImpl implements TIMService { 28 | 29 | private TIMConfig config; 30 | 31 | private String priKey = null; 32 | 33 | private final OkHttpClient HTTP_CLIENT = new OkHttpClient(); 34 | private final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 35 | 36 | private TIMAccountService accountService = new TIMAccountServiceImpl(this); 37 | private TIMOnlineStatusService onlineStatusService = new TIMOnlineStatusServiceImpl(this); 38 | private TIMRelationService relationService = new TIMRelationServiceImpl(this); 39 | private TIMChatService chatService = new TIMChatServiceImpl(this); 40 | private TIMProfileService profileService = new TIMProfileServiceImpl(this); 41 | private TIMGroupServiceImpl groupService = new TIMGroupServiceImpl(this); 42 | private TIMOperationalService operationalService = null; 43 | private TIMDirtyWordService dirtyWordService = null; 44 | private TIMNoSpeakService noSpeakService = null; 45 | 46 | public TIMServiceImpl(TIMConfig config) { 47 | this.config = config; 48 | } 49 | 50 | @Override 51 | public TIMConfig getConfig() { 52 | return this.config; 53 | } 54 | 55 | @Override 56 | public String getUserSig(@NonNull String identifier) throws TIMException { 57 | return getUserSig(identifier, 30); 58 | } 59 | 60 | @Override 61 | public String getUserSig(@NonNull String identifier, Integer expireOfDay) throws TIMException { 62 | TLSSigature.GenTLSSignatureResult signatureResult; 63 | 64 | if (expireOfDay == null) { 65 | expireOfDay = 30; 66 | } 67 | if (expireOfDay < 1) { 68 | expireOfDay = 1; 69 | } 70 | 71 | try { 72 | 73 | if (priKey == null) { 74 | 75 | final String CLASSPATH_PREFIX = "classpath:"; 76 | 77 | if (config.getPrivateKeyPath().startsWith(CLASSPATH_PREFIX)) { 78 | InputStream fis = TIMConfig.class.getClassLoader().getResourceAsStream(RegExUtils.removeFirst(config.getPrivateKeyPath(), CLASSPATH_PREFIX)); 79 | priKey = IOUtils.toString(fis, "UTF-8"); 80 | } else { 81 | priKey = FileUtils.readFileToString(new File(config.getPrivateKeyPath()), "UTF-8"); 82 | } 83 | 84 | } 85 | 86 | int secondOfMonth = 60 * 60 * 24 * expireOfDay; 87 | signatureResult = TLSSigature.GenTLSSignatureEx(config.getAppId(), identifier, priKey, secondOfMonth); 88 | if (signatureResult == null || signatureResult.urlSig == null) { 89 | throw new TIMException(new TIMError(-1, "UserSig生成失败")); 90 | } 91 | 92 | } catch (TIMException e) { 93 | throw e; 94 | } catch (Exception e) { 95 | log.error(e.getMessage(), e); 96 | throw new TIMException(new TIMError(-1, e.getClass().getName() + " >> " + e.getMessage())); 97 | } 98 | 99 | return signatureResult.urlSig; 100 | } 101 | 102 | @Override 103 | public String get(String api, Map queryParams) throws TIMException { 104 | 105 | String url = buildFullUrl(api, queryParams); 106 | 107 | log.debug("【TIMJava】 request method : {}", "GET"); 108 | log.debug("【TIMJava】 request url : {}", url); 109 | 110 | Request request = new Request.Builder() 111 | .url(url) 112 | .get() 113 | .build(); 114 | 115 | return execute(request); 116 | } 117 | 118 | @Override 119 | public String post(String api, Object body) throws TIMException { 120 | 121 | String url = buildFullUrl(api, null); 122 | String json = JsonUtils.toJson(body); 123 | 124 | log.debug("【TIMJava】 request method : {}", "POST"); 125 | log.debug("【TIMJava】 request url : {}", url); 126 | log.debug("【TIMJava】 request body : {}", json); 127 | 128 | Request request = new Request.Builder() 129 | .url(url) 130 | .post(RequestBody.create(JSON, json)) 131 | .build(); 132 | 133 | return execute(request); 134 | } 135 | 136 | private static final String CONTENT_TYPE = "json"; 137 | private static final String URL = "https://console.tim.qq.com"; 138 | 139 | private String buildFullUrl(String api, Map queryParams) throws TIMException { 140 | Long appid = this.getConfig().getAppId(); 141 | String adminIdentifier = this.getConfig().getAdminIdentifier(); 142 | String userSig = this.getUserSig(adminIdentifier); 143 | String randomText = (Math.random() * 10000000 + "").substring(0, 8); 144 | 145 | String urlTemplate = URL + "/" + api + "?sdkappid=%s&identifier=%s&usersig=%s&random=%s&contenttype=%s"; 146 | 147 | String url = String.format(urlTemplate, appid, adminIdentifier, userSig, randomText, CONTENT_TYPE); 148 | 149 | if (queryParams != null) { 150 | Iterator> iterator = queryParams.entrySet().iterator(); 151 | Map.Entry entry; 152 | while (iterator.hasNext()) { 153 | entry = iterator.next(); 154 | url += "&" + entry.getKey() + "=" + entry.getValue(); 155 | } 156 | } 157 | 158 | return url; 159 | } 160 | 161 | private String executeInternal(Request request) throws Exception { 162 | 163 | Response response = HTTP_CLIENT.newCall(request).execute(); 164 | String jsonResult = response.body().string(); 165 | 166 | TIMError timError = JsonUtils.fromJson(jsonResult, TIMError.class); 167 | 168 | if (0 != timError.getErrorCode()) { 169 | throw new TIMException(timError); 170 | } 171 | 172 | log.debug("【TIMJava】response body -------- "); 173 | log.debug(jsonResult); 174 | log.debug("【TIMJava】response body -------- "); 175 | 176 | return jsonResult; 177 | 178 | } 179 | 180 | private String execute(Request request) throws TIMException { 181 | return execute(request, 1); 182 | } 183 | 184 | private String execute(Request request, int reqCount) throws TIMException { 185 | try { 186 | 187 | log.debug("【TIMJava】 发起请求 当前第 {} 次 / {} 次 {}", reqCount, config.getReqMaxRetry(), reqCount > 1 ? "[重试请求]" : ""); 188 | 189 | return executeInternal(request); 190 | 191 | } catch (SocketTimeoutException e) { 192 | 193 | if (reqCount >= config.getReqMaxRetry()) { 194 | throw new TIMException(new TIMError(-1, "请求失效,请检查你的网络状态")); 195 | } 196 | 197 | // 执行重试 198 | return execute(request, reqCount++); 199 | 200 | } catch (TIMException e) { 201 | throw e; 202 | } catch (Exception e) { 203 | throw new TIMException(new TIMError(-1, e.getMessage())); 204 | } 205 | } 206 | 207 | @Override 208 | public TIMAccountService getAccountService() { 209 | return this.accountService; 210 | } 211 | 212 | 213 | @Override 214 | public TIMDirtyWordService getDirtyWordService() { 215 | return this.dirtyWordService; 216 | } 217 | 218 | @Override 219 | public TIMGroupService getGroupService() { 220 | return this.groupService; 221 | } 222 | 223 | @Override 224 | public TIMNoSpeakService getNoSpeakService() { 225 | return this.noSpeakService; 226 | } 227 | 228 | @Override 229 | public TIMOnlineStatusService getOnlineStatusService() { 230 | return this.onlineStatusService; 231 | } 232 | 233 | @Override 234 | public TIMOperationalService getOperationalService() { 235 | return this.operationalService; 236 | } 237 | 238 | @Override 239 | public TIMProfileService getProfileService() { 240 | return this.profileService; 241 | } 242 | 243 | @Override 244 | public TIMRelationService getRelationService() { 245 | return this.relationService; 246 | } 247 | 248 | @Override 249 | public TIMChatService getChatService() { 250 | return this.chatService; 251 | } 252 | 253 | } 254 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.sevlow 8 | tim-java-sdk 9 | 0.5.5 10 | TIMJava - Tencent IM Java SDK 11 | 腾讯IM 云通讯 Java SDK 12 | https://github.com/forfuns/TIMJava 13 | 14 | 15 | 16 | The Apache License, Version 2.0 17 | http://www.apache.org/licenses/LICENSE-2.0.txt 18 | 19 | 20 | 21 | 22 | 23 | 爱因斯唐 24 | my-tangjianbin@163.com 25 | https://github.com/forfuns 26 | 27 | 28 | yixuanp21810 29 | 30 | https://github.com/yixuanp21810 31 | 32 | 33 | 34 | 35 | 1.8 36 | 1.8 37 | 38 | UTF-8 39 | 40 | 41 | 42 | 43 | 44 | org.projectlombok 45 | lombok 46 | 1.18.8 47 | 48 | 49 | 50 | org.bouncycastle 51 | bcpkix-jdk15on 52 | 1.59 53 | 54 | 55 | net.sf.json-lib 56 | json-lib 57 | 2.4 58 | jdk15 59 | 60 | 61 | commons-io 62 | commons-io 63 | 2.7 64 | 65 | 66 | org.apache.commons 67 | commons-lang3 68 | 3.9 69 | 70 | 71 | org.slf4j 72 | slf4j-api 73 | 1.7.25 74 | 75 | 76 | com.google.code.gson 77 | gson 78 | 2.8.2 79 | 80 | 81 | com.squareup.okhttp3 82 | okhttp 83 | 3.8.1 84 | compile 85 | 86 | 87 | org.yaml 88 | snakeyaml 89 | 1.26 90 | 91 | 92 | 93 | ch.qos.logback 94 | logback-classic 95 | 1.2.0 96 | test 97 | 98 | 99 | org.testng 100 | testng 101 | 6.10 102 | test 103 | 104 | 105 | org.assertj 106 | assertj-guava 107 | 3.0.0 108 | test 109 | 110 | 111 | com.google.inject 112 | guice 113 | 3.0 114 | test 115 | 116 | 117 | 118 | 119 | 120 | 121 | 70yi-nexus-releases 122 | Nexus Release Repository 123 | http://mvn.7billion.cn/repository/maven-releases/ 124 | 125 | 126 | 70yi-nexus-snapshots 127 | Nexus Snapshot Repository 128 | http://mvn.7billion.cn/repository/maven-snapshots/ 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | org.apache.maven.plugins 137 | maven-surefire-plugin 138 | 2.17 139 | 140 | true 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | pl.project13.maven 161 | git-commit-id-plugin 162 | 2.2.4 163 | 164 | 165 | 166 | revision 167 | 168 | 169 | 170 | 171 | 172 | yyyy-MM-dd HH:mm:ss 173 | 174 | true 175 | 176 | ${project.basedir}/.git 177 | 178 | false 179 | 180 | true 181 | 182 | ${project.build.outputDirectory}/git.properties 183 | 184 | false 185 | 186 | 187 | 188 | 189 | false 190 | 191 | false 192 | 195 | 7 196 | 197 | -dirty 198 | 201 | false 202 | 203 | 204 | 205 | 206 | org.apache.maven.plugins 207 | maven-release-plugin 208 | 2.5.1 209 | 210 | true 211 | false 212 | release 213 | deploy 214 | 215 | 216 | 217 | org.apache.maven.plugins 218 | maven-compiler-plugin 219 | 3.5.1 220 | 221 | 1.8 222 | 1.8 223 | 224 | 225 | -Xlint:deprecation 226 | 227 | 228 | 229 | 230 | 231 | org.apache.maven.plugins 232 | maven-source-plugin 233 | 234 | true 235 | 236 | 237 | 238 | compile 239 | 240 | jar 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/TIMRelationService.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api; 2 | 3 | import com.sevlow.sdk.tim.bean.*; 4 | import com.sevlow.sdk.tim.bean.account.TIMFriend; 5 | import com.sevlow.sdk.tim.bean.relation.ListFriendsDirectivityResult; 6 | import com.sevlow.sdk.tim.bean.relation.ListFriendsResult; 7 | import com.sevlow.sdk.tim.common.error.TIMException; 8 | import lombok.NonNull; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * @author Element 14 | * 15 | * 关系链管理 16 | * API Doc : https://cloud.tencent.com/document/product/269/1642 17 | */ 18 | public interface TIMRelationService { 19 | 20 | /** 21 | * 加好友方式 22 | */ 23 | public static enum AddType { 24 | 25 | // 单向添加好友 26 | Add_Type_Single("Add_Type_Single"), 27 | // 双向添加好友 28 | Add_Type_Both("Add_Type_Both"); 29 | 30 | private String typeInfo; 31 | 32 | private AddType(String typeInfo) { 33 | this.typeInfo = typeInfo; 34 | } 35 | 36 | } 37 | 38 | /** 39 | * 删除好友方式 40 | */ 41 | public static enum DeleteType { 42 | // 单向删除好友 43 | Delete_Type_Single("Delete_Type_Single"), 44 | // 双向删除好友 45 | Delete_Type_Both("Delete_Type_Both"); 46 | 47 | private String typeInfo; 48 | 49 | private DeleteType(String typeInfo) { 50 | this.typeInfo = typeInfo; 51 | } 52 | } 53 | 54 | /** 55 | * 双向好友检查方式 56 | */ 57 | public static enum CheckType { 58 | 59 | // 单向检查 60 | CheckResult_Type_Singal("CheckResult_Type_Singal"), 61 | 62 | // 双向检查 63 | CheckResult_Type_Both("CheckResult_Type_Both"); 64 | 65 | private String typeInfo; 66 | 67 | private CheckType(String typeInfo) { 68 | this.typeInfo = typeInfo; 69 | } 70 | } 71 | 72 | public static enum BlackCheckType { 73 | 74 | BlackCheckResult_Type_Both("BlackCheckResult_Type_Both"), 75 | BlackCheckResult_Type_Singal("BlackCheckResult_Type_Singal"); 76 | 77 | private String typeInfo; 78 | 79 | private BlackCheckType(String typeInfo) { 80 | this.typeInfo = typeInfo; 81 | } 82 | 83 | } 84 | 85 | /** 86 | * 添加好友 87 | *

88 | * 添加好友,支持批量添加好友。 89 | *

90 | * API Doc : https://cloud.tencent.com/document/product/269/1643 91 | * 92 | * @param identifier 需要添加好友的 Identifier 93 | * @param friends 被添加的 Identifier 集合 94 | * @param friendSource 好友来源,自动补全AddSource_Type_XXX,所以仅输入"XXX"即可 95 | * @param addType 好友添加方式,为 null 则双向添加好友( Add_Type_Both ) 96 | * @return 97 | * @throws TIMException 98 | */ 99 | AddFriendsResult addFriends(@NonNull String identifier, @NonNull List friends, @NonNull String friendSource, AddType addType) throws TIMException; 100 | 101 | /** 102 | * 添加好友(双向添加好友) 103 | * 104 | *

105 | * 添加好友,支持批量添加好友。 106 | *

107 | * API Doc : https://cloud.tencent.com/document/product/269/1643 108 | * 109 | * @param identifier 需要添加好友的 Identifier 110 | * @param friends 被添加的 Identifier 集合 111 | * @param friendSource 好友来源,自动补全AddSource_Type_XXX,所以仅输入"XXX"即可 112 | * @return 113 | * @throws TIMException 114 | */ 115 | AddFriendsResult addFriends(@NonNull String identifier, @NonNull List friends, @NonNull String friendSource) throws TIMException; 116 | 117 | 118 | /** 119 | * 导入好友 120 | *

121 | * 支持批量导入单向好友; 122 | * 往同一个用户导入好友时建议采用批量导入的方式,避免并发写导致的写冲突。 123 | *

124 | * API Doc : https://cloud.tencent.com/document/product/269/8301 125 | * 126 | * @param identifier 需要导入好友的 Identifier 127 | * @param friends 被添加的 Identifier 集合 128 | * @param friendSource 好友来源,自动补全AddSource_Type_XXX,所以仅输入"XXX"即可 129 | * @return 130 | */ 131 | ImportFriendsResult importFriends(@NonNull String identifier, @NonNull List friends, @NonNull String friendSource) throws TIMException; 132 | 133 | /** 134 | * 导入好友 135 | *

136 | * 支持批量导入单向好友; 137 | * 往同一个用户导入好友时建议采用批量导入的方式,避免并发写导致的写冲突。 138 | *

139 | * API Doc : https://cloud.tencent.com/document/product/269/8301 140 | * 141 | * @param identifier 需要导入好友的 Identifier 142 | * @param friends 被添加的 TIMFriend 集合 143 | * @return 144 | */ 145 | ImportFriendsResult importFriends(@NonNull String identifier, @NonNull List friends) throws TIMException; 146 | 147 | /** 148 | * 更新好友 149 | *

150 | * 支持批量更新同一用户的多个好友的关系链数据。 151 | * 更新一个用户多个好友时,建议采用批量方式,避免并发写导致的写冲突。 152 | *

153 | * API Doc : https://cloud.tencent.com/document/product/269/12525 154 | * 155 | * @param identifier 156 | * @param friends 157 | * @param snsItems 158 | * @return 159 | * @throws TIMException 160 | */ 161 | UpdateFriendsResult updateFriends(@NonNull String identifier, @NonNull List friends, List snsItems) throws TIMException; 162 | 163 | /** 164 | * 删除好友 165 | *

166 | * 删除好友,支持单向删除好友和双向删除好友。 167 | *

168 | * API Doc : https://cloud.tencent.com/document/product/269/1644 169 | * 170 | * @param identifier 账号 Identifier 171 | * @param friends 删除的好友列表 Identifier 集合 172 | * @param deleteType 删除方式,为 null 则双向删除( Delete_Type_Both ) 173 | * @return 174 | */ 175 | DeleteFriendsResult deleteFriend(@NonNull String identifier, @NonNull List friends, DeleteType deleteType) throws TIMException; 176 | 177 | /** 178 | * 删除好友(双向) 179 | *

180 | * 删除好友,支持单向删除好友和双向删除好友。 181 | *

182 | * API Doc : https://cloud.tencent.com/document/product/269/1644 183 | * 184 | * @param identifier 账号 Identifier 185 | * @param friends 删除的好友列表 Identifier 集合 186 | * @return 187 | */ 188 | DeleteFriendsResult deleteFriend(@NonNull String identifier, @NonNull List friends) throws TIMException; 189 | 190 | /** 191 | * 清空(删除)所有好友 192 | *

193 | * API Doc : https://cloud.tencent.com/document/product/269/1645 194 | * 195 | * @param identifier 196 | */ 197 | void emptyFriends(@NonNull String identifier) throws TIMException; 198 | 199 | /** 200 | * 校验好友 201 | *

202 | * 支持批量校验好友关系。 203 | *

204 | * API Doc : https://cloud.tencent.com/document/product/269/1646 205 | * 206 | * @param identifier 207 | * @param friends 208 | * @param checkType 209 | * @return 210 | */ 211 | CheckFriendsResult checkFriends(@NonNull String identifier, @NonNull List friends, CheckType checkType) throws TIMException; 212 | 213 | /** 214 | * 校验好友(双向) 215 | * 216 | * @param identifier 217 | * @param friends 218 | * @return 219 | */ 220 | CheckFriendsResult checkFriends(@NonNull String identifier, @NonNull List friends) throws TIMException; 221 | 222 | /** 223 | * 拉取好友 224 | * 225 | *

226 | * 分页拉取全量好友数据。 227 | * 不支持资料数据的拉取。 228 | * 不需要指定请求拉取的字段,默认返回全量的标配好友数据和自定义好友数据。 229 | *

230 | * API Doc : https://cloud.tencent.com/document/product/269/1647 231 | * 232 | * @param account 指定要拉取好友数据的用户的 UserID 233 | * @param startIndex 分页的起始位置 234 | * @param standardSequence 上次拉好友数据时返回的 StandardSequence,如果 StandardSequence 字段的值与后台一致,后台不会返回标配好友数据 235 | * @param customSequence 上次拉好友数据时返回的 CustomSequence,如果 CustomSequence 字段的值与后台一致,后台不会返回自定义好友数据 236 | * @return ListFriendsResult 237 | * @throws TIMException 238 | */ 239 | ListFriendsResult listFriends(@NonNull String account, @NonNull Integer startIndex, Integer standardSequence, Integer customSequence) throws TIMException; 240 | 241 | /** 242 | * 拉取好友 243 | * 244 | * @param account 指定要拉取好友数据的用户的 UserID 245 | * @param startIndex 分页的起始位置 246 | * @return 247 | * @throws TIMException 248 | * @see com.sevlow.sdk.tim.api.TIMRelationService#listFriends 249 | */ 250 | ListFriendsResult listFriends(@NonNull String account, @NonNull Integer startIndex) throws TIMException; 251 | 252 | /** 253 | * 拉取指定好友 254 | *

255 | * 支持拉取指定好友的好友数据和资料数据。 256 | * 建议每次拉取的好友数不超过100,避免因数据量太大导致回包失败。 257 | *

258 | * API Doc : https://cloud.tencent.com/document/product/269/8609 259 | * 260 | * @param account 指定要拉取好友数据的用户的 UserID 261 | * @param friends 好友的 UserID 列表 262 | * @param profiles 需要拉取的标配资料字段 @link com.sevlow.sdk.tim.bean.profile.TIMProfile 263 | * @param customProfiles 需要拉取的自定义资料字段 ,无需输入Tag_Profile_Custom_ 自动补充 264 | * @param snsProfiles 需要拉取的标配好友字段字段 @link com.sevlow.sdk.tim.bean.relation.TIMFriendProfile 265 | * @param customSnsProfiles 需要拉取的自定义好友字段 ,无需输入 Tag_SNS_Custom_ 自动补充 266 | * @return ListFriendsDirectivityResult 267 | * @throws TIMException 268 | * 269 | */ 270 | ListFriendsDirectivityResult listFriendsDirectivity(@NonNull String account, @NonNull List friends, 271 | List profiles, List customProfiles, 272 | List snsProfiles, List customSnsProfiles) throws TIMException; 273 | 274 | /** 275 | * 屏蔽用户(添加黑名单) 276 | *

277 | * 添加黑名单,支持批量添加黑名单。 278 | *

279 | * API Doc : https://cloud.tencent.com/document/product/269/3718 280 | * 281 | * @param identifier 282 | * @param accounts 283 | * @return 284 | */ 285 | AddBlockAccountsResult addBlockAccounts(@NonNull String identifier, @NonNull List accounts) throws TIMException; 286 | 287 | /** 288 | * 解除屏蔽 (删除黑名单) 289 | *

290 | * 删除指定黑名单。 291 | *

292 | * API Doc : https://cloud.tencent.com/document/product/269/3719 293 | * 294 | * @param identifier 295 | * @param blockAccounts 296 | * @return 297 | */ 298 | RemoveBlockAccountsResult removeblockAccounts(@NonNull String identifier, @NonNull List blockAccounts) throws TIMException; 299 | 300 | /** 301 | * 拉取黑名单 302 | *

303 | * 分页拉取所有黑名单。 304 | *

305 | * API Doc : https://cloud.tencent.com/document/product/269/3722 306 | * 307 | * @param identifier 需要拉取该 Identifier 的黑名单 308 | * @param offset 拉取的起始位置 309 | * @param rows 拉去总数 310 | * @param lastSequence 上一次拉黑名单时后台返回给客户端的 Seq,初次拉取时为0 311 | * @return 312 | */ 313 | ListBlockAccountsResult listBlockAccounts(@NonNull String identifier, Integer offset, Integer rows, Integer lastSequence) throws TIMException; 314 | 315 | /** 316 | * 校验黑名单 317 | *

318 | * 支持批量校验黑名单。 319 | *

320 | * API Doc : https://cloud.tencent.com/document/product/269/3725 321 | * 322 | * @param identifier 323 | * @param accounts 324 | * @param checkType 325 | * @return 326 | */ 327 | CheckBlockAccountsResult checkBlockAccounts(@NonNull String identifier, @NonNull List accounts, BlackCheckType checkType) throws TIMException; 328 | 329 | /** 330 | * 添加分组 331 | *

332 | * 添加分组,支持批量添加分组,并将指定好友加入到新增分组中。 333 | *

334 | * API Doc : https://cloud.tencent.com/document/product/269/10107 335 | * 336 | * @param identifier 337 | * @param groupNames 338 | * @param friends 339 | * @return 340 | */ 341 | AddGroupsResult addGroups(@NonNull String identifier, @NonNull List groupNames, List friends) throws TIMException; 342 | 343 | /** 344 | * 删除分组 345 | *

346 | * 删除指定分组。 347 | *

348 | * API Doc : https://cloud.tencent.com/document/product/269/10108 349 | * 350 | * @param identifier 351 | * @param groupNames 352 | * @return 353 | */ 354 | DeleteGroupsResult deleteGroups(@NonNull String identifier, @NonNull List groupNames) throws TIMException; 355 | 356 | 357 | /** 358 | * 修改好友备注 359 | */ 360 | void remarkFriend(@NonNull String identifier, @NonNull String friendId, @NonNull String remark) throws TIMException; 361 | 362 | 363 | } 364 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/common/TLSSigature.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.common; 2 | 3 | import net.sf.json.JSONObject; 4 | import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; 5 | import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; 6 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 7 | import org.bouncycastle.openssl.PEMParser; 8 | import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; 9 | import org.bouncycastle.util.encoders.Base64; 10 | 11 | import java.io.CharArrayReader; 12 | import java.io.IOException; 13 | import java.io.Reader; 14 | import java.nio.charset.Charset; 15 | import java.security.PrivateKey; 16 | import java.security.PublicKey; 17 | import java.security.Security; 18 | import java.security.Signature; 19 | import java.util.Arrays; 20 | import java.util.zip.DataFormatException; 21 | import java.util.zip.Deflater; 22 | import java.util.zip.Inflater; 23 | 24 | /** 25 | * @author Element 26 | * @Package com.sevlow.sdk.tim.common 27 | * @date 2019-05-27 11:32 28 | * @Description: TODO 29 | */ 30 | public class TLSSigature { 31 | 32 | public static class GenTLSSignatureResult { 33 | public String errMessage; 34 | public String urlSig; 35 | public int expireTime; 36 | public int initTime; 37 | 38 | public GenTLSSignatureResult() { 39 | errMessage = ""; 40 | urlSig = ""; 41 | } 42 | } 43 | 44 | public static class CheckTLSSignatureResult { 45 | public String errMessage; 46 | public boolean verifyResult; 47 | public int expireTime; 48 | public int initTime; 49 | 50 | public CheckTLSSignatureResult() { 51 | errMessage = ""; 52 | verifyResult = false; 53 | } 54 | } 55 | 56 | /** 57 | * 生成 tls 票据 58 | * 59 | * @param expire 有效期,单位是秒,推荐一个月 60 | * @param appid3rd 填写与 sdkAppid 一致字符串形式的值 61 | * @param sdkappid 应用的 appid 62 | * @param identifier 用户 id 63 | * @param accountType 创建应用后在配置页面上展示的 acctype 64 | * @param priKeyContent 生成 tls 票据使用的私钥内容 65 | * @return 如果出错,GenTLSSignatureResult 中的 urlSig为空,errMsg 为出错信息,成功返回有效的票据 66 | */ 67 | @Deprecated 68 | public static GenTLSSignatureResult GenTLSSignature(long expire, 69 | String appid3rd, long sdkappid, String identifier, 70 | long accountType, String priKeyContent) { 71 | 72 | GenTLSSignatureResult result = new GenTLSSignatureResult(); 73 | 74 | Security.addProvider(new BouncyCastleProvider()); 75 | Reader reader = new CharArrayReader(priKeyContent.toCharArray()); 76 | JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); 77 | PEMParser parser = new PEMParser(reader); 78 | PrivateKey privKeyStruct; 79 | try { 80 | Object obj = parser.readObject(); 81 | parser.close(); 82 | privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj); 83 | } catch (IOException e) { 84 | result.errMessage = "read pem error:" + e.getMessage(); 85 | return result; 86 | } 87 | 88 | //Create Json string and serialization String 89 | String jsonString = "{" 90 | + "\"TLS.account_type\":\"" + accountType + "\"," 91 | + "\"TLS.identifier\":\"" + identifier + "\"," 92 | + "\"TLS.appid_at_3rd\":\"" + appid3rd + "\"," 93 | + "\"TLS.sdk_appid\":\"" + sdkappid + "\"," 94 | + "\"TLS.expire_after\":\"" + expire + "\"" 95 | + "}"; 96 | //System.out.println("#jsonString : \n" + jsonString); 97 | 98 | String time = String.valueOf(System.currentTimeMillis() / 1000); 99 | String SerialString = 100 | "TLS.appid_at_3rd:" + appid3rd + "\n" + 101 | "TLS.account_type:" + accountType + "\n" + 102 | "TLS.identifier:" + identifier + "\n" + 103 | "TLS.sdk_appid:" + sdkappid + "\n" + 104 | "TLS.time:" + time + "\n" + 105 | "TLS.expire_after:" + expire + "\n"; 106 | 107 | try { 108 | //Create Signature by SerialString 109 | Signature signature = Signature.getInstance("SHA256withECDSA", "BC"); 110 | signature.initSign(privKeyStruct); 111 | signature.update(SerialString.getBytes(Charset.forName("UTF-8"))); 112 | byte[] signatureBytes = signature.sign(); 113 | 114 | String sigTLS = Base64.toBase64String(signatureBytes); 115 | 116 | //Add TlsSig to jsonString 117 | JSONObject jsonObject = JSONObject.fromObject(jsonString); 118 | jsonObject.put("TLS.sig", sigTLS); 119 | jsonObject.put("TLS.time", time); 120 | jsonString = jsonObject.toString(); 121 | 122 | //compression 123 | Deflater compresser = new Deflater(); 124 | compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8"))); 125 | 126 | compresser.finish(); 127 | byte[] compressBytes = new byte[512]; 128 | int compressBytesLength = compresser.deflate(compressBytes); 129 | compresser.end(); 130 | 131 | result.urlSig = new String(Base64Url.base64EncodeUrl(Arrays.copyOfRange(compressBytes, 0, compressBytesLength))); 132 | } catch (Exception e) { 133 | e.printStackTrace(); 134 | result.errMessage = e.getMessage(); 135 | } 136 | 137 | return result; 138 | } 139 | 140 | /** 141 | * 校验 tls 票据 142 | * 143 | * @param sig 返回 tls 票据 144 | * @param appid3rd 填写与 sdkAppid 一致的字符串形式的值 145 | * @param sdkappid 应的 appid 146 | * @param identifier 用户 id 147 | * @param accountType 创建应用后在配置页面上展示的 acctype 148 | * @param pubKeyContent 用于校验 tls 票据的公钥内容,但是需要先将公钥文件转换为 java 原生 api 使用的格式,下面是推荐的命令 149 | * openssl pkcs8 -topk8 -in ec_key.pem -outform PEM -out p8_priv.pem -nocrypt 150 | * @return 如果出错 CheckTLSSignatureResult 中的 verifyResult 为 false,错误信息在 errMsg,校验成功为 true 151 | */ 152 | @Deprecated 153 | public static CheckTLSSignatureResult CheckTLSSignature(String sig, String appid3rd, long sdkappid, 154 | String identifier, long accountType, 155 | String pubKeyContent) { 156 | CheckTLSSignatureResult result = new CheckTLSSignatureResult(); 157 | Security.addProvider(new BouncyCastleProvider()); 158 | 159 | byte[] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8"))); 160 | 161 | //Decompression 162 | Inflater decompression = new Inflater(); 163 | decompression.setInput(compressBytes, 0, compressBytes.length); 164 | byte[] decompressBytes = new byte[1024]; 165 | int decompressLength; 166 | try { 167 | decompressLength = decompression.inflate(decompressBytes); 168 | } catch (DataFormatException e) { 169 | result.errMessage = "uncompress data error:" + e.getMessage(); 170 | return result; 171 | } 172 | decompression.end(); 173 | 174 | String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength)); 175 | 176 | //Get TLS.Sig from json 177 | JSONObject jsonObject = JSONObject.fromObject(jsonString); 178 | String sigTLS = jsonObject.getString("TLS.sig"); 179 | 180 | //debase64 TLS.Sig to get serailString 181 | byte[] signatureBytes = Base64.decode(sigTLS.getBytes(Charset.forName("UTF-8"))); 182 | 183 | try { 184 | 185 | String sigTime = jsonObject.getString("TLS.time"); 186 | String sigExpire = jsonObject.getString("TLS.expire_after"); 187 | 188 | //checkTime 189 | if (System.currentTimeMillis() / 1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) { 190 | result.errMessage = new String("TLS sig is out of date "); 191 | System.out.println("Timeout"); 192 | return result; 193 | } 194 | 195 | //Get Serial String from json 196 | String SerialString = 197 | "TLS.appid_at_3rd:" + appid3rd + "\n" + 198 | "TLS.account_type:" + accountType + "\n" + 199 | "TLS.identifier:" + identifier + "\n" + 200 | "TLS.sdk_appid:" + sdkappid + "\n" + 201 | "TLS.time:" + sigTime + "\n" + 202 | "TLS.expire_after:" + sigExpire + "\n"; 203 | 204 | Reader reader = new CharArrayReader(pubKeyContent.toCharArray()); 205 | PEMParser parser = new PEMParser(reader); 206 | JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); 207 | Object obj = parser.readObject(); 208 | parser.close(); 209 | PublicKey pubKeyStruct = converter.getPublicKey((SubjectPublicKeyInfo) obj); 210 | 211 | Signature signature = Signature.getInstance("SHA256withECDSA", "BC"); 212 | signature.initVerify(pubKeyStruct); 213 | signature.update(SerialString.getBytes(Charset.forName("UTF-8"))); 214 | result.verifyResult = signature.verify(signatureBytes); 215 | } catch (Exception e) { 216 | e.printStackTrace(); 217 | result.errMessage = "Failed in checking sig"; 218 | } 219 | 220 | return result; 221 | } 222 | 223 | /** 224 | * 生成 tls 票据,精简参数列表,有效期默认为 180 天 225 | * 226 | * @param skdAppid 应用的 sdkappid 227 | * @param identifier 用户 id 228 | * @param priKeyContent 私钥文件内容 229 | * @return GenTLSSignatureResult 230 | */ 231 | public static GenTLSSignatureResult GenTLSSignatureEx( 232 | long skdAppid, 233 | String identifier, 234 | String priKeyContent) { 235 | return GenTLSSignatureEx(skdAppid, identifier, priKeyContent, 3600 * 24 * 180); 236 | } 237 | 238 | /** 239 | * 生成 tls 票据,精简参数列表 240 | * 241 | * @param skdAppid 应用的 sdkappid 242 | * @param identifier 用户 id 243 | * @param priKeyContent 私钥文件内容 244 | * @param expire 有效期,以秒为单位,推荐时长一个月 245 | * @return GenTLSSignatureResult 246 | */ 247 | public static GenTLSSignatureResult GenTLSSignatureEx( 248 | long skdAppid, 249 | String identifier, 250 | String priKeyContent, 251 | long expire) { 252 | return GenTLSSignature(expire, "0", skdAppid, identifier, 0, priKeyContent); 253 | } 254 | 255 | public static CheckTLSSignatureResult CheckTLSSignatureEx( 256 | String sig, 257 | long sdkappid, 258 | String identifier, 259 | String publicKey) throws DataFormatException { 260 | 261 | CheckTLSSignatureResult result = new CheckTLSSignatureResult(); 262 | Security.addProvider(new BouncyCastleProvider()); 263 | 264 | byte[] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8"))); 265 | 266 | //Decompression 267 | Inflater decompression = new Inflater(); 268 | decompression.setInput(compressBytes, 0, compressBytes.length); 269 | byte[] decompressBytes = new byte[1024]; 270 | int decompressLength = decompression.inflate(decompressBytes); 271 | decompression.end(); 272 | 273 | String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength)); 274 | 275 | //Get TLS.Sig from json 276 | JSONObject jsonObject = JSONObject.fromObject(jsonString); 277 | String sigTLS = jsonObject.getString("TLS.sig"); 278 | 279 | //debase64 TLS.Sig to get serailString 280 | byte[] signatureBytes = Base64.decode(sigTLS.getBytes(Charset.forName("UTF-8"))); 281 | 282 | try { 283 | String strSdkAppid = jsonObject.getString("TLS.sdk_appid"); 284 | String sigTime = jsonObject.getString("TLS.time"); 285 | String sigExpire = jsonObject.getString("TLS.expire_after"); 286 | 287 | if (Integer.parseInt(strSdkAppid) != sdkappid) { 288 | result.errMessage = new String("sdkappid " 289 | + strSdkAppid 290 | + " in tls sig not equal sdkappid " 291 | + sdkappid 292 | + " in request"); 293 | return result; 294 | } 295 | 296 | if (System.currentTimeMillis() / 1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) { 297 | result.errMessage = new String("TLS sig is out of date"); 298 | return result; 299 | } 300 | 301 | //Get Serial String from json 302 | String SerialString = 303 | "TLS.appid_at_3rd:" + 0 + "\n" + 304 | "TLS.account_type:" + 0 + "\n" + 305 | "TLS.identifier:" + identifier + "\n" + 306 | "TLS.sdk_appid:" + sdkappid + "\n" + 307 | "TLS.time:" + sigTime + "\n" + 308 | "TLS.expire_after:" + sigExpire + "\n"; 309 | 310 | Reader reader = new CharArrayReader(publicKey.toCharArray()); 311 | PEMParser parser = new PEMParser(reader); 312 | JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); 313 | Object obj = parser.readObject(); 314 | parser.close(); 315 | PublicKey pubKeyStruct = converter.getPublicKey((SubjectPublicKeyInfo) obj); 316 | 317 | Signature signature = Signature.getInstance("SHA256withECDSA", "BC"); 318 | signature.initVerify(pubKeyStruct); 319 | signature.update(SerialString.getBytes(Charset.forName("UTF-8"))); 320 | boolean bool = signature.verify(signatureBytes); 321 | result.expireTime = Integer.parseInt(sigExpire); 322 | result.initTime = Integer.parseInt(sigTime); 323 | result.verifyResult = bool; 324 | } catch (Exception e) { 325 | e.printStackTrace(); 326 | result.errMessage = "Failed in checking sig"; 327 | } 328 | 329 | return result; 330 | } 331 | 332 | public static GenTLSSignatureResult genSig( 333 | long sdkappid, 334 | String identifier, 335 | String priKey) { 336 | // 默认 180 天 337 | return GenTLSSignature(24 * 3600 * 180, "0", sdkappid, identifier, 0, priKey); 338 | } 339 | 340 | public static GenTLSSignatureResult genSig( 341 | long sdkappid, 342 | String identifier, 343 | int expire, 344 | String priKey) { 345 | return GenTLSSignature(expire, "0", sdkappid, identifier, 0, priKey); 346 | } 347 | 348 | } 349 | -------------------------------------------------------------------------------- /src/main/java/com/sevlow/sdk/tim/api/impl/TIMRelationServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sevlow.sdk.tim.api.impl; 2 | 3 | import com.sevlow.sdk.tim.api.TIMRelationService; 4 | import com.sevlow.sdk.tim.api.TIMService; 5 | import com.sevlow.sdk.tim.bean.*; 6 | import com.sevlow.sdk.tim.bean.account.TIMFriend; 7 | import com.sevlow.sdk.tim.bean.relation.ListFriendsDirectivityResult; 8 | import com.sevlow.sdk.tim.bean.relation.ListFriendsResult; 9 | import com.sevlow.sdk.tim.common.error.TIMError; 10 | import com.sevlow.sdk.tim.common.error.TIMException; 11 | import com.sevlow.sdk.tim.utils.JsonUtils; 12 | import lombok.NonNull; 13 | import lombok.extern.slf4j.Slf4j; 14 | import org.apache.commons.lang3.RegExUtils; 15 | import org.apache.commons.lang3.StringUtils; 16 | 17 | import java.util.ArrayList; 18 | import java.util.HashMap; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | /** 23 | * 关系管理实现类 24 | * 25 | * @author element 26 | */ 27 | @Slf4j 28 | public class TIMRelationServiceImpl implements TIMRelationService { 29 | 30 | private final String ADD_SOURCE_TYPE_PREFIX = "AddSource_Type_"; 31 | 32 | private static final String CUSTOM_PROFILE_PREFIX = "Tag_Profile_Custom_"; 33 | 34 | private static final String CUSTOM_SNS_PROFILE_PREFIX = "Tag_SNS_Custom_"; 35 | 36 | private TIMService timService; 37 | 38 | public TIMRelationServiceImpl(TIMService timService) { 39 | this.timService = timService; 40 | } 41 | 42 | private String cleanAddSourceTypePrefix(String addSourceType) throws TIMException { 43 | if (StringUtils.startsWith(addSourceType, ADD_SOURCE_TYPE_PREFIX)) { 44 | addSourceType = RegExUtils.removeFirst(addSourceType, ADD_SOURCE_TYPE_PREFIX); 45 | } 46 | 47 | if (addSourceType.length() > 8) { 48 | throw new TIMException(new TIMError(30001, "addSourceType除去前缀Add_Source_外,不能多于8个字符")); 49 | } 50 | return addSourceType; 51 | } 52 | 53 | @Override 54 | public AddFriendsResult addFriends(@NonNull String identifier, @NonNull List friends, @NonNull String friendSource, AddType addType) throws TIMException { 55 | 56 | if (friends.isEmpty()) { 57 | throw new TIMException(new TIMError(30001, "好友列表不能为空")); 58 | } 59 | 60 | String api = "v4/sns/friend_add"; 61 | 62 | friendSource = cleanAddSourceTypePrefix(friendSource); 63 | 64 | Map body = new HashMap<>(); 65 | body.put("From_Account", identifier); 66 | 67 | List> friendItems = new ArrayList<>(); 68 | Map friendItem = null; 69 | for (String friend : friends) { 70 | friendItem = new HashMap<>(); 71 | friendItem.put("To_Account", friend); 72 | friendItem.put("AddSource", ADD_SOURCE_TYPE_PREFIX + friendSource); 73 | 74 | friendItems.add(friendItem); 75 | } 76 | 77 | body.put("AddFriendItem", friendItems); 78 | 79 | 80 | if (addType == null) { 81 | body.put("AddType", AddType.Add_Type_Both); 82 | } 83 | 84 | return JsonUtils.fromJson(this.timService.post(api, body), AddFriendsResult.class); 85 | } 86 | 87 | @Override 88 | public AddFriendsResult addFriends(@NonNull String identifier, @NonNull List friends, @NonNull String friendSource) throws TIMException { 89 | return this.addFriends(identifier, friends, friendSource, null); 90 | } 91 | 92 | @Override 93 | public ImportFriendsResult importFriends(@NonNull String identifier, @NonNull List friends, @NonNull String friendSource) throws TIMException { 94 | 95 | friendSource = cleanAddSourceTypePrefix(friendSource); 96 | 97 | List list = new ArrayList<>(); 98 | TIMFriend timFriend = null; 99 | 100 | for (String friend : friends) { 101 | timFriend = new TIMFriend(friend, ADD_SOURCE_TYPE_PREFIX + friendSource); 102 | list.add(timFriend); 103 | } 104 | 105 | return this.importFriends(identifier, list); 106 | } 107 | 108 | @Override 109 | public ImportFriendsResult importFriends(@NonNull String identifier, @NonNull List friends) throws TIMException { 110 | 111 | if (friends.isEmpty()) { 112 | throw new TIMException(new TIMError(30001, "好友列表不能为空")); 113 | } 114 | 115 | String api = "v4/sns/friend_import"; 116 | 117 | Map body = new HashMap<>(); 118 | 119 | body.put("From_Account", identifier); 120 | body.put("AddFriendItem", friends); 121 | 122 | return JsonUtils.fromJson(this.timService.post(api, body), ImportFriendsResult.class); 123 | } 124 | 125 | @Override 126 | public UpdateFriendsResult updateFriends(@NonNull String identifier, @NonNull List friends, List snsItems) throws TIMException { 127 | 128 | if (friends.isEmpty()) { 129 | throw new TIMException(new TIMError(30001, "好友列表不能为空")); 130 | } 131 | 132 | String api = "v4/sns/friend_update"; 133 | 134 | Map body = new HashMap<>(); 135 | body.put("From_Account", identifier); 136 | 137 | List> updateItems = new ArrayList<>(); 138 | Map updateItem = null; 139 | 140 | for (String friend : friends) { 141 | 142 | updateItem = new HashMap<>(); 143 | updateItem.put("To_Account", friend); 144 | updateItem.put("SnsItem", snsItems); 145 | 146 | updateItems.add(updateItem); 147 | } 148 | 149 | body.put("UpdateItem", updateItems); 150 | 151 | return JsonUtils.fromJson(this.timService.post(api, body), UpdateFriendsResult.class); 152 | } 153 | 154 | @Override 155 | public DeleteFriendsResult deleteFriend(@NonNull String identifier, @NonNull List friends, DeleteType deleteType) throws TIMException { 156 | 157 | if (friends.isEmpty()) { 158 | throw new TIMException(new TIMError(30001, "好友列表不能为空")); 159 | } 160 | 161 | String api = "v4/sns/friend_delete"; 162 | 163 | Map body = new HashMap<>(); 164 | body.put("From_Account", identifier); 165 | body.put("To_Account", friends); 166 | 167 | if (deleteType == null) { 168 | deleteType = DeleteType.Delete_Type_Both; 169 | } 170 | 171 | body.put("DeleteType", deleteType); 172 | 173 | return JsonUtils.fromJson(this.timService.post(api, body), DeleteFriendsResult.class); 174 | } 175 | 176 | 177 | @Override 178 | public DeleteFriendsResult deleteFriend(@NonNull String identifier, @NonNull List friends) throws TIMException { 179 | return deleteFriend(identifier, friends, null); 180 | } 181 | 182 | @Override 183 | public void emptyFriends(@NonNull String identifier) throws TIMException { 184 | 185 | String api = "v4/sns/friend_delete_all"; 186 | 187 | Map body = new HashMap<>(); 188 | body.put("From_Account", identifier); 189 | 190 | this.timService.post(api, body); 191 | 192 | } 193 | 194 | @Override 195 | public CheckFriendsResult checkFriends(@NonNull String identifier, @NonNull List friends, CheckType checkType) throws TIMException { 196 | 197 | if (friends.isEmpty()) { 198 | throw new TIMException(new TIMError(30001, "好友列表不能为空")); 199 | } 200 | 201 | String api = "v4/sns/friend_check"; 202 | 203 | Map body = new HashMap<>(); 204 | body.put("From_Account", identifier); 205 | body.put("To_Account", friends); 206 | // 如果不是双向检查,则默认为单项检查 207 | if (!CheckType.CheckResult_Type_Both.equals(checkType)) { 208 | checkType = CheckType.CheckResult_Type_Singal; 209 | } 210 | body.put("CheckType", checkType); 211 | return JsonUtils.fromJson(this.timService.post(api, body), CheckFriendsResult.class); 212 | } 213 | 214 | @Override 215 | public CheckFriendsResult checkFriends(@NonNull String identifier, @NonNull List friends) throws TIMException { 216 | return checkFriends(identifier, friends, null); 217 | } 218 | 219 | @Override 220 | public ListFriendsResult listFriends(@NonNull String account, @NonNull Integer startIndex, Integer standardSequence, Integer customSequence) throws TIMException { 221 | String api = "v4/sns/friend_get"; 222 | 223 | Map body = new HashMap<>(); 224 | body.put("From_Account", account); 225 | body.put("StartIndex", startIndex); 226 | 227 | if (standardSequence != null) { 228 | body.put("StandardSequence", standardSequence); 229 | } 230 | 231 | if (customSequence != null) { 232 | body.put("CustomSequence", customSequence); 233 | } 234 | 235 | return JsonUtils.fromJson(this.timService.post(api, body), ListFriendsResult.class); 236 | } 237 | 238 | @Override 239 | public ListFriendsResult listFriends(@NonNull String account, @NonNull Integer startIndex) throws TIMException { 240 | return this.listFriends(account, startIndex, null, null); 241 | } 242 | 243 | @Override 244 | public ListFriendsDirectivityResult listFriendsDirectivity(@NonNull String account, @NonNull List friends, List profiles, List customProfiles, List snsProfiles, List customSnsProfiles) throws TIMException { 245 | 246 | if (friends.isEmpty()) { 247 | throw new TIMException(new TIMError(30001, "查找的好友列表不能为空")); 248 | } 249 | 250 | if(friends.size() > 100){ 251 | throw new TIMException(new TIMError(90011, "批量目标帐号超过100,请减少 To_Account 中目标帐号数量")); 252 | } 253 | 254 | List tagList = new ArrayList<>(); 255 | if (profiles != null && !profiles.isEmpty()) { 256 | tagList.addAll(profiles); 257 | } 258 | 259 | if (snsProfiles != null && !snsProfiles.isEmpty()) { 260 | tagList.addAll(snsProfiles); 261 | } 262 | 263 | if (customProfiles != null && !customProfiles.isEmpty()) { 264 | for (String custom : customProfiles) { 265 | if (custom.length() > 8) { 266 | throw new TIMException(new TIMError(-1, "自定义资料字段不能超过8个字符")); 267 | } 268 | tagList.add(CUSTOM_PROFILE_PREFIX.concat(custom)); 269 | } 270 | } 271 | 272 | if (customSnsProfiles != null && !customSnsProfiles.isEmpty()) { 273 | for (String custom : customSnsProfiles) { 274 | if (custom.length() > 8) { 275 | throw new TIMException(new TIMError(-1, "自定义好友资料字段不能超过8个字符")); 276 | } 277 | tagList.add(CUSTOM_SNS_PROFILE_PREFIX.concat(custom)); 278 | } 279 | } 280 | 281 | if (tagList.isEmpty()) { 282 | throw new TIMException(new TIMError(30001, "拉取的字段不能为空")); 283 | } 284 | 285 | String api = "v4/sns/friend_get_list"; 286 | Map body = new HashMap<>(); 287 | body.put("From_Account", account); 288 | body.put("To_Account", friends); 289 | body.put("TagList", tagList); 290 | return JsonUtils.fromJson(this.timService.post(api, body), ListFriendsDirectivityResult.class); 291 | } 292 | 293 | @Override 294 | public AddBlockAccountsResult addBlockAccounts(@NonNull String identifier, @NonNull List accounts) throws TIMException { 295 | 296 | if (accounts.isEmpty()) { 297 | throw new TIMException(new TIMError(30001, "屏蔽的用户列表不能为空")); 298 | } 299 | 300 | String api = "v4/sns/black_list_add"; 301 | 302 | Map body = new HashMap<>(); 303 | 304 | body.put("From_Account", identifier); 305 | body.put("To_Account", accounts); 306 | return JsonUtils.fromJson(this.timService.post(api, body), AddBlockAccountsResult.class); 307 | } 308 | 309 | @Override 310 | public RemoveBlockAccountsResult removeblockAccounts(@NonNull String identifier, @NonNull List blockAccounts) throws TIMException { 311 | if (blockAccounts.isEmpty()) { 312 | throw new TIMException(new TIMError(30001, "屏蔽的用户列表不能为空")); 313 | } 314 | 315 | String api = "v4/sns/black_list_delete"; 316 | 317 | Map body = new HashMap<>(); 318 | 319 | body.put("From_Account", identifier); 320 | body.put("To_Account", blockAccounts); 321 | return JsonUtils.fromJson(this.timService.post(api, body), RemoveBlockAccountsResult.class); 322 | } 323 | 324 | @Override 325 | public ListBlockAccountsResult listBlockAccounts(@NonNull String identifier, Integer offset, Integer rows, Integer lastSequence) throws TIMException { 326 | 327 | String api = "v4/sns/black_list_get"; 328 | 329 | Map body = new HashMap<>(); 330 | 331 | body.put("From_Account", identifier); 332 | body.put("StartIndex", offset); 333 | body.put("MaxLimited", rows); 334 | body.put("LastSequence", lastSequence); 335 | 336 | return JsonUtils.fromJson(this.timService.post(api, body), ListBlockAccountsResult.class); 337 | } 338 | 339 | @Override 340 | public CheckBlockAccountsResult checkBlockAccounts(@NonNull String identifier, @NonNull List accounts, BlackCheckType checkType) throws TIMException { 341 | 342 | if (accounts.isEmpty()) { 343 | throw new TIMException(new TIMError(30001, "检查的账号列表不能为空")); 344 | } 345 | 346 | String api = "v4/sns/black_list_check"; 347 | 348 | Map body = new HashMap<>(); 349 | body.put("From_Account", identifier); 350 | body.put("To_Account", accounts); 351 | // 只要不是双向验证,默认为单向验证 352 | if (checkType == null) { 353 | checkType = BlackCheckType.BlackCheckResult_Type_Both; 354 | } 355 | 356 | body.put("CheckType", checkType); 357 | 358 | return JsonUtils.fromJson(this.timService.post(api, body), CheckBlockAccountsResult.class); 359 | } 360 | 361 | @Override 362 | public AddGroupsResult addGroups(@NonNull String identifier, @NonNull List groupNames, List friends) throws TIMException { 363 | if (groupNames.isEmpty()) { 364 | throw new TIMException(new TIMError(30001, "分组列表 不能为空")); 365 | } 366 | String api = "v4/sns/group_add"; 367 | 368 | Map body = new HashMap<>(); 369 | body.put("From_Account", identifier); 370 | body.put("GroupName", groupNames); 371 | body.put("To_Account", friends); 372 | return JsonUtils.fromJson(this.timService.post(api, body), AddGroupsResult.class); 373 | } 374 | 375 | @Override 376 | public DeleteGroupsResult deleteGroups(@NonNull String identifier, @NonNull List groupNames) throws TIMException { 377 | if (groupNames.isEmpty()) { 378 | throw new TIMException(new TIMError(30001, "分组列表 不能为空")); 379 | } 380 | String api = "v4/sns/group_delete"; 381 | 382 | Map body = new HashMap<>(); 383 | body.put("From_Account", identifier); 384 | body.put("GroupName", groupNames); 385 | return JsonUtils.fromJson(this.timService.post(api, body), DeleteGroupsResult.class); 386 | } 387 | 388 | /** 389 | * 修改好友备注 390 | */ 391 | @Override 392 | public void remarkFriend(@NonNull String identifier, @NonNull String friendId, @NonNull String remark) throws TIMException { 393 | 394 | if (remark.length() > 96) { 395 | throw new TIMException(new TIMError(-1, "备注长度最长不得超过 96 个字节")); 396 | } 397 | 398 | String api = "v4/sns/friend_update"; 399 | 400 | Map body = new HashMap<>(4); 401 | 402 | List updateItem = new ArrayList(); 403 | 404 | Map map = new HashMap(); 405 | 406 | List> list = new ArrayList<>(); 407 | 408 | SnsItem rename = new SnsItem<>(); 409 | rename.setTag("Tag_SNS_IM_Remark"); 410 | rename.setValue(remark); 411 | 412 | list.add(rename); 413 | 414 | map.put("To_Account", friendId); 415 | map.put("SnsItem", list); 416 | 417 | updateItem.add(map); 418 | 419 | body.put("From_Account", identifier); 420 | body.put("UpdateItem", updateItem); 421 | 422 | this.timService.post(api, body); 423 | } 424 | 425 | 426 | } 427 | --------------------------------------------------------------------------------