即时通讯UniApp iOS端插件说明
插件名称
YmIMPlugin
- Ym: 取译码小二郞 译码两字的拼音首字母
- IM : 为即时通讯
- Plugin : 即为插件
Vue中导入
var ymIMPlugin = uni.requireNativePlugin("YmIMPlugin")
1.登录接口
- 接口名:
login
- 参数:json格式
{
userid : 123,
name : 'zhangsan',
avatar : '',
token : '' ,
}
参数说明
- userid : 登录用户id, 字符串类型
- name : 登录用户昵称/名称, 字符串类型
- avatar : 登录用户头像, 字符串类型
- token : 即时通讯接口鉴权token, 字符串类型
- 回调 : 有
回调格式:
{
code : 0, // 0:成功 1:失败
data : {},
message : ''
}
- iOS代码
UNI_EXPORT_METHOD(@selector(login:callback:))
- (void)login:(NSDictionary *)userInfo callback:(UniModuleKeepAliveCallback)callback {
//即时通讯配置, 配置接口及socket地址
RZIMConfig *config = [RZIMConfig sharedConfig];
config.socketURL = @"wss://im.yuefuximai.com/websocket"; //
config.baseURL = @"https://im.yuefuximai.com/"; //
NSString *uid = [NSString stringWithFormat:@"%@",userInfo[@"userid"]];
NSString *name = [NSString stringWithFormat:@"%@",userInfo[@"name"]];
NSString *avatar = [NSString stringWithFormat:@"%@",userInfo[@"avatar"]];
NSString *token = [NSString stringWithFormat:@"%@",userInfo[@"token"]];
//缓存 登录用户信息
RZUserInfo *user = [[RZUserInfo alloc] initWithUid:uid name:name portrait:avatar];
[RZIMClient sharedClient].userInfo = user;
//根据token连接服务器
[[RZIMClient sharedClient] connectWithToken:token success:^{
NSLog(@"连接成功");
!callback ?: callback(@{@"code":@(0),@"data":@{},@"message":@"登录成功"},NO);
} error:^(RZIMConnectErrorCode errorCode) {
NSLog(@"error code :%ld",(long)errorCode);
!callback ?: callback(@{@"code":@(1),@"data":@{},@"message":@"登录失败"},NO);
}];
}
- vue 调用
ymIMPlugin.login({
'userid':'123',
'name': 'zhangsan',
'avatar': 'http://xxxx',
'token': 'eadlfalkdfalksjfalkdfadfasdf'
},
(ret) => {
console.log(ret);
}
)
2.跳转聊天界面接口
- 接口名:
pushChat
- 参数:json格式
{
type : 1, // 1:单聊 2:群聊
tid : '123', //聊天目标id
title : '', //
}
参数说明
- type : 聊天类型,整型类型。 1:单聊 2:群聊
- tid : 聊天目标id,字符串类型。 如果是单聊, tid 为对方用户id。如果为群聊 tid 为群组的groupid
- title : 聊天界面顶部title,字符串类型
回调 : 无
iOS代码
UNI_EXPORT_METHOD_SYNC(@selector(pushChat:))
- (void)pushChat:(NSDictionary *)chatInfo {
NSInteger type = [chatInfo[@"type"] integerValue];
NSString *tid = [NSString stringWithFormat:@"%@",chatInfo[@"tid"]];
NSString *title = [NSString stringWithFormat:@"%@",chatInfo[@"title"]];
RZConversationType chatType = ConversationType_PRIVATE;
if (type == 1) {
chatType = ConversationType_PRIVATE;
}else if (type == 2) {
chatType = ConversationType_GROUP;
}else {
return;
}
//会有线程问题, 切到主线程
dispatch_async(dispatch_get_main_queue(), ^{
RZConversationViewController *vc = [[RZConversationViewController alloc] initWithConversationType:chatType targetId:tid];
vc.hidesBottomBarWhenPushed = YES;
vc.title = title;
UINavigationController *navVC = [self dc_findCurrentShowingViewController].navigationController;
[navVC pushViewController:vc animated:YES];
});
}
- vue调用
ymIMPlugin.pushChat({
'type':1, //单聊
'tid': '123',
'title': 'zhangsan',
});
3.获取会话列表接口
- 接口名:
fetchConversationList
- 参数:无
- 回调 : 有
- iOS代码
UNI_EXPORT_METHOD(@selector(fetchChatList:))
- (void)fetchConversationList:(UniModuleKeepAliveCallback)callback {
[[RZIMClient sharedClient] getConversationList:@[@(ConversationType_PRIVATE)] count:999 success:^(NSArray * _Nonnull dataSource) {
NSMutableArray *array = @[].mutableCopy;
[dataSource enumerateObjectsUsingBlock:^(RZConversation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSDictionary *dic = [obj mj_JSONObject];
[array addObject:dic];
}];
!callback ?: callback(@{@"code":@(0), @"data": array, @"message":@""},NO);
} error:^(RZErrorCode nErrorCode, NSString * _Nullable errorMessage) {
!callback ?: callback(@{@"code":@(1), @"data": @[], @"message":errorMessage},NO);
}];
}
- vue调用:
ymIMPlugin.fetchConversationList((ret) => {
console.log(ret);
}
)
4. 发送图片消息接口
- 接口名:
sendImage
- 参数:json格式
{
url : 'http://xxxx',
width : 640
height : 960,
groupId: '12345'
}
参数说明
- url : 网络图片地址,字符串类型
- width : 图片宽度,浮点类型
- height : 图片高度,浮点类型
- groupId: 群组id, 字符串类型
- 回调 : 有
- iOS代码
- (void)sendImage:(NSDictionary *)imageInfo callback:(UniModuleKeepAliveCallback)callback{
NSString *imageUrl = [NSString stringWithFormat:@"%@",imageInfo[@"url"]];
CGFloat imageW = [imageInfo[@"width"] floatValue];
CGFloat imageH = [imageInfo[@"height"] floatValue];
NSString *groupId = [NSString stringWithFormat:@"%@",imageInfo[@"groupId"]];
RZMessageContent *content = [[RZMessageContent alloc] init];
content.msg = imageUrl;
content.msgType = MessageType_PICTURE;
content.extraData = @{@"width":@(imageW),@"height":@(imageH)};
[self sendMessage:content inGroup:groupId success:^{
NSLog(@"图片消息发送成功");
!callback ?: callback(@{@"code":@(0),@"data":@"",@"message":@"图片消息发送成功"},NO);
} error:^(RZErrorCode nErrorCode, NSString * _Nullable errorMessage) {
NSLog(@"图片消息失败:%@",errorMessage);
!callback ?: callback(@{@"code":@(1),@"data":@"",@"message":errorMessage},NO);
}];
}
- vue调用
ymIMPlugin.sendImage({
'url':'http://xxxxxx',
'width': 640,
'height': 960,
'groupId': '1232323'
},
(ret) => {
console.log(ret);
}
)
5. 发送文本消息接口
- 接口名:
sendText
- 参数:json格式
{
text : '文本消息',
groupId: '12345'
}
参数说明
- text : 文本消息内容,字符串类型
- groupId: 群组id, 字符串类型
- 回调 : 有
- iOS代码
UNI_EXPORT_METHOD(@selector(sendText:callback:))
- (void)sendText:(NSDictionary *)textInfo callback:(UniModuleKeepAliveCallback)callback {
NSString *text = [NSString stringWithFormat:@"%@",textInfo[@"text"]];
NSString *groupId = [NSString stringWithFormat:@"%@",textInfo[@"groupId"]];
if (text.length == 0 || groupId.length == 0) {
return;
}
RZMessageContent *content = [[RZMessageContent alloc] init];
content.msg = text;
content.msgType = MessageType_TEXT;
[self sendMessage:content inGroup:groupId success:^{
NSLog(@"文本消息发送成功");
!callback ?: callback(@{@"code":@(0),@"data":@"",@"message":@"文本消息发送成功"},NO);
} error:^(RZErrorCode nErrorCode, NSString * _Nullable errorMessage) {
NSLog(@"文本消息失败:%@",errorMessage);
!callback ?: callback(@{@"code":@(1),@"data":@"",@"message":errorMessage},NO);
}];
}
- vue调用
ymIMPlugin.sendText({
'text':'我是文本消息',
'groupId': '1232323'
},
(ret) => {
console.log(ret);
}
)