1.0.5 • Published 3 years ago

qnweb-im v1.0.5

Weekly downloads
-
License
-
Repository
-
Last release
3 years ago

IM Web SDK

前期准备

DEMO 地址:https://github.com/pili-engineering/QNWebIMSDK

SDK 文件下载地址:qnweb-im.js

初始化

首先设置 AppID

const config = {
  appid: "YOUR_APP_ID",
  ws: false,
  autoLogin: true
};

然后创建im对象,供全局调用。

const im = QNIM.init(config);

API

base 基础部分

登录

im.login({
  mobile: String, // 与name 2选1
  name: String,
  password: String,
})

监听

im.on('events', (ret) => {
  //do something with ret
})
// or
im.on({
  eventName: (ret) => {
    //do something with ret
  },
  ...
})

取消监听

im.off('events', (ret) => {
  //do something with ret
})
// or
im.off({
  eventName: (ret) => {
    //do something with ret
  },
  ...
})

二维码登录

im.qrlogin({
  password,
  user_id
});

token登录

im.tokenLogin(user_id, token)

rosterManager

获取好友id列表

im.rosterManage.asyncGetRosterIdList().then(res => {
  //
});

获取好友信息

im.rosterManage.asyncGetRosterInfo(state.sid).then(res => {
  //
})

用户注册

rosterManage.asyncRegester({
  username,
  password
}).then(() => {
  //
});

根据id列表获取用户详细信息

im.rosterManage.asnycGetRosterListDetailByIds(rosterIdList).then(res => {
  //
});

根据id获取聊天信息

const rosterMessages = im.rosterManage.getRosterMessageByRid(uid);

读取消息

im.rosterManage.readRosterMessage(uid);

删除好友

im.rosterManage
  .asyncDeleteRoster({ user_id })
  .then(() => {
    alert("好友已删除");
  });

获取缓存的所有新用户

const userMaps = im.rosterManage.getAllRosterDetail();

撤回消息,只能撤回5分钟内的

im.rosterManage.recallMessage(user_id, message_id);

删除消息

im.rosterManage.deleteMessage(user_id, message_id);

获取用户的未读数

const unreadCount = im.rosterManage.getUnreadCount(user_id);

设置消息成未读

im.rosterManage.unreadMessage(user_id, message_id);

获取好友信息

const roserInfo = im.rosterManage.getRosterInfo(user_id);

获取好友申请列表

im.rosterManage
  .asyncGetApplyList({ cursor: "" })
  .then((res = []) => {
    //
  });

获取黑名单

im.rosterManage
  .asyncGetBlockedlist()
  .then((res = []) => {
    //
  });

加入黑名单

im.rosterManage
  .asyncBlockeAdd(user_id)
  .then((res = []) => {
    //
  });

移除黑名单

im.rosterManage
  .asyncBlockeRemove(user_id)
  .then((res = []) => {
    //
  });

请求加为好友

im.rosterManage
  .asyncApply({ user_id, alias })
  .then((res = []) => {
    //
  });

通过好友申请

im.rosterManage
  .asyncAccept({ user_id })
  .then((res = []) => {
    //
  });

拒绝好友申请

im.rosterManage
  .asyncDecline({ user_id })
  .then((res = []) => {
    //
  });

按名称搜索用户

im.rosterManage
  .asyncSearchRosterByName({ username })
  .then((res = []) => {
    //
  });

按ID搜索用户

im.rosterManage
  .asyncSearchRosterById({ user_id })
  .then((res = []) => {
    //
  });

groupManager

获取群信息

im.groupManage.asyncGetGroupInfo(group_id, fromServer).then(res => {
  //
})

获取加入的群组

im.groupManage.asyncGetJoinedGroups().then(res => {
  //
});

打开群组

// 此方法会准备群组聊天界面的一些必备信息。
im.groupManage.openGroup(group_id);

获取缓存的所有群组详情

const allGroupMap = im.groupManage.getAllGroupDetail();

获取群组成员(异步)

im.groupManage.asyncGetGroupMembers(group_id, fromServer).then(res => {
  //
});

获取群组成员(同步)

const members = im.groupManage.getGroupMembers(group_id);

按id获取群组详情

im.groupManage.asyncGetGroupListDetail(groupIds).then(res => {
  //
});

获取群消息

const groupMessages = rootState.im.groupManage.getGruopMessage(group_id);

将群消息设置已读

im.groupManage.readGroupMessage(group_id)

撤回消息

im.groupManage.recallMessage(group_id, message_id)

获取群未读消息数

const unreadCount = im.groupManage.getUnreadCount(group_id);

获取群管理员列表

im.groupManage.asyncGetAdminList({ group_id }).then(res => {
  //
})

群添加管理员

im.groupManage.asyncAdminAdd({
  group_id,
  user_list
})
  .then(() => {
    //
  });

移除管理员

im.groupManage.asyncAdminRemove({ group_id, user_list }).then(() => {
  //
});

获取群公告详情

im.groupManage.asyncGetAnouncementById({ announcement_id, group_id }).then(res => {
  //
});

删除群公告

im.groupManage
  .asyncAnouncementDelete({ group_id, announcement_id })
  .then(() => {
    //
  });

添加群公告

im.groupManage.asyncAnnouncementEdit({ title, content, group_id })
  .then(() => {
    //
  });

群公告列表

im.groupManage.asyncGetAnnouncementList({ group_id }).then((res = []) => {
  //
});

创建群组

im.groupManage.asyncCreate({
  name,
  type,
  avatar,
  description,
  user_list,
})
  .then(() => {
    //
  });

解散群组

im.groupManage.asyncDestroy({ group_id })
  .then(() => {
    alert("您已解散了此群。。");
  });

获取群组详情

im.groupManage.asyncGetInfo({ group_id }).then(res => {
  //
});

更新群头像

im.groupManage.asyncUpdateAvatar({
  group_id,
  value,
})
  .then(() => {
    alert("更新头像完成");
  });

更新群描述

im.groupManage.asyncUpdateDescription({
  group_id,
  value
}).then(() => {
  //
});

更新群扩展

this.im.groupManage.asyncUpdateExt({
  group_id,
  value
}).then(() => {
  //
});

更新群名称

im.groupManage.asyncUpdateName({
  group_id,
  value
})
  .then(() => {
    //
  });

获取群成员

im.groupManage.asyncGetMemberList(group_id, fromServer).then(res => {
  //
});

设置群消息免打扰情况

im.groupManage.asyncGroupMsgMutemode({
  group_id,
  msg_mute_mode
})
  .then(() => {
    this.groupInfo.msg_mute_mode = this.groupInfo.msg_mute_mode ? 0 : 2;
  });

获取群黑名单

im.groupManage.asyncGroupBannedList({ group_id }).then(res => {
  //
});

禁言群成员

im.groupManage.asyncGroupBab({ group_id, duration, user_list }).then(() => {
  //
});

解除成员

im.groupManage.asyncGroupUnban({ group_id, user_list }).then(() => {
  //
});

全员禁言

im.groupManage.asyncBanAll({group_id,duration: -1}).then(res => {
  console.log(res)
});

全员取消禁言

im.groupManage.asyncUnBanAll({group_id}).then(res => {
  console.log(res)
});

判断禁言状态

// 判断禁言过期时间 groupInfo.ban_expire_time
// 如果为-1表示永久禁言, 如果将来的时间,则表示禁言中
// 全员禁言和单用户的禁言是互不影响的
// 只要有一个是禁言,用户的状态就是禁言

设置群成员是否可以邀请

im.groupManage.asyncUpdateAllowMemberInvitation({
  group_id,
  value
})
  .then(() => {
    //
  });

设置群成员是否可以修改群信息

im.groupManage.asyncUpdateAllowMemberModify({
  group_id,
  value
})
  .then(() => {
    //
  });

设置群是否开启已读模式

im.groupManage.asyncUpdateEnableReadack({
  group_id,
  value
})
  .then(() => {
    //
  });

设置群历史是否可见

im.groupManage.asyncUpdateHistoryVisible({
  group_id,
  value
})
  .then(() => {
    //
  });

设置入群是否需要申请

im.groupManage.asyncUpdateRequireadminapproval({
  group_id,
  apply_approval
})
  .then(() => {
    //
  });

更换群主

im.groupManage.asyncOwnerTransfer({
  group_id,
  new_owner
})
  .then(() => {
    //
  });

申请加入群

im.groupManage.asyncApply({ group_id, reason })
  .then(() => {
    //
  });

同意/拒绝申请用户加入群

im.groupManage.asyncApplyHandle({
  approval: true / false,
  user_id,
  group_id
}).then(() => {
  //
});

获取群黑名单

im.groupManage.asyncGroupBockedlist({ group_id }).then(res => {
  //
});

将成员加入黑名单

im.groupManage.asyncGroupBlock({ group_id, user_list }).then(() => {
  //
});

解除黑名单

im.groupManage.asyncGroupUnblock({ group_id, user_list })
  .then(() => {
    //
  });

踢出群组

im.groupManage.asyncKick({ group_id, user_list }).then(() => {
  //
});

获取群邀请列表

this.im.groupManage.asyncGetInvitationList().then(res => {
  //
});

邀请成员加入群

im.groupManage.asyncInvite({ group_id, user_list }).then(() => {
});

同意/拒绝群邀请

im.groupManage.asyncInviteHandle({
  approval: true,
  user_id,
  group_id
}).then(() => {
  //
});

退出群

im.groupManage.asyncLeave({ group_id })
  .then(() => {
    //
  });

修改群名片

im.groupManage.asyncUpdateDisplayName({
  group_id,
  value
})
  .then(() => {
    //
  });

获取群申请列表

im.groupManage.asncGetApplicationList({ group_list }).then(rs => {
  //
});

获取群文件

im.groupManage.asyncGetFileList({ group_id }).then((res = []) => {
  //
});

删除群文件

im.groupManage.asyncFileDelete({ file_list, group_id }).then(() => {
  //
});

sysManager

发送好友消息

im.sysManage.sendRosterMessage({
  type,
  uid,
  content,
  attachment
});

发送群消息

im.sysManage.sendGroupMessage({
  type,
  gid,
  content,
  attachment
});

群发送@消息

im.sysManage.sendMentionMessage({
  gid,
  txt,
  mentionAll,
  mentionList,
  mentionedMessage,
  pushMessage,
  senderNickname
});

发送输入状态消息

im.sysManage.sendInputStatusMessage(roster_id, "nothing" / "typing");

转发消息

im.sysManage.forwardMessage({
  uid,
  gid, //2选1
  mid,
});

请求历史消息

im.sysManage.requireHistoryMessage(roster_id / group_id, mid);

获取所有消息未读状态

const allAcks = im.sysManage.getAllMessageStatus() || {};

获取群文件上传url

im.sysManage.asyncGetGroupAvatarUploadUrl({
  group_id,
  "access-token"
})
  .then(res => {
    //
  });

获取聊天文件上传地址

im.sysManage.asyncGetFileUploadChatFileUrl({
  file_type,
  to_id,
  to_type
})
  .then(res => {
    //
  });

上传文件

im.sysManage.asyncFileUpload({
  file,
  fileType,
  to_id,
  toType: "chat",
  chatType: "roster"
})
  .then(res => {
    //
  })

拼装图片路径

const image = im.sysManage.getImage({ avatar, type = 'roster', thumbnail = true });

userManager

获取登录用户的token

const token =  im.userManage.getToken();

获取登录用户的uid

const cuid = im.userManage.getUid();

获取appid

const appid = im.userManage.getAppid();

获取最近回话列表

const list = im.userManage.getConversationList();

发送验证码

im.userManage
  .asyncUserSendSms({
    mobile,
  })
  .then(() => {
    //
  });

发送验证码(通过图片验证码)

im.userManage
  .asyncCaptchaSms({
    captcha,
    image_id,
    mobile,
  })
  .then(() => {
    //
  });

检查用户名是否可用

im.userManage.asyncUserNameCheck(username).then(() => {
  //
});

绑定手机号-使用签名绑定

im.userManage.asyncUserMobileBindSign({
  mobile,
  sign,
}).then(() => {
  //
});

手机号验证码登录

im.userManage.asyncUserMobileLogin({
  captcha,
  mobile
})
  .then(res => {
    //
  });

更新手机号

im.userManage
  .asyncUpdateMobile({ mobile })
  .then(() => {
    //
  });

更新头像

im.userManage
  .asyncUpdateAvatar({
    avatar
  })
  .then(() => {
    //
  });

更新昵称

im.userManage.asyncUpdateNickName({ nick_name }).then(() => {
  //
});

获取用户profile

im.userManage.asyncGetProfile(true).then(res => {
  //
})

更新用户profile

im.userManage.asyncUpdateProfile({
  username,
  avatar
}).then(res => {
  //
})

获取用户设置信息

im.userManage.asyncGetSettings().then(res => {
  //
})

修改用户设置

im.userManage
  .asyncUpdateSettings({
    "auth_answer": "string",
    "auth_mode": 0,
    "auth_question": "string",
    "auto_download": true,
    "group_confirm": true,
    "id": 0,
    "no_push": true,
    "no_push_detail": true,
    "no_push_end_hour": 0,
    "no_push_start_hour": 0,
    "no_sounds": true,
    "push_nick_name": "string",
    "user_id",
    "vibratory": true
  }).then(() => {
    //
  });

chatroomManage

创建聊天室

im.chatroomManage.create(name).then(() => {
  //
});

加入聊天室

im.chatroomManage.join(group_id).then(() => {
  // 
})

退出聊天室

im.chatroomManage.leave(group_id).then(() => {
  // 
})

解散聊天室

im.chatroomManage.destroy(group_id).then(() => {
  // 
})

字段说明

字段类型说明
fromServerbooleantrue表示从服务器取数据, false表示从本地缓存取数据
apply_approval0 | 1 | 2入群申请审批设置。0:同意所有申请 1:需要管理员确认 2:拒绝所有申请
禁言群成员 durationnumber禁言时长,单位为分钟。-1 为永久禁言
file_typenumber文件类型 100: 普通聊天文件, 101: 语音聊天文件(amr格式),102: 图片聊天文件, 103: 视频聊天文件, 104: 语音聊天文件(mp3格式), 200: 普通共享文件, 201: 语音共享文件, 202: 图片共享文件, 203: 视频共享文件
to_typenumber1: 用户,2: 群组
to_idnumber用户ID或群ID