emConversation.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { EaseSDK, EMClient } from '../index';
  2. import { CHAT_TYPE } from '../constant';
  3. const emConversation = () => {
  4. //从服务端获取置顶会话列表
  5. const fetchPinConversationFromServer = (pageSize = 50, cursor = '') => {
  6. return new Promise((resolve, reject) => {
  7. EMClient.getServerPinnedConversations({ pageSize, cursor })
  8. .then((res) => {
  9. console.log('>>>>置顶会话列表数据获取成功',res);
  10. resolve(res);
  11. })
  12. .catch((error) => {
  13. reject(error);
  14. });
  15. });
  16. };
  17. //置顶会话列表
  18. const pinConversationItem = (params) => {
  19. const { conversationId, conversationType, isPinned } = params;
  20. return new Promise((resolve, reject) => {
  21. EMClient.pinConversation({
  22. conversationId,
  23. conversationType,
  24. isPinned,
  25. })
  26. .then((res) => {
  27. console.log('>>>>置顶会话列表添加成功');
  28. resolve(res);
  29. })
  30. .catch((error) => {
  31. reject(error);
  32. });
  33. });
  34. };
  35. //从服务端获取会话列表
  36. const fetchConversationFromServer = (
  37. options = { pageSize: 20, cursor: '' }
  38. ) => {
  39. return new Promise((resolve, reject) => {
  40. //支持分页这里写死只取二十条
  41. EMClient.getServerConversations({ ...options })
  42. .then((res) => {
  43. console.log('>>>>会话列表数据获取成功',res);
  44. const result = res.data.conversations;
  45. resolve(result);
  46. })
  47. .catch((error) => {
  48. reject(error);
  49. });
  50. });
  51. };
  52. //从服务端删除会话
  53. const removeConversationFromServer = (
  54. channel,
  55. chatType = CHAT_TYPE.SINGLE_CHAT,
  56. deleteRoam = false
  57. ) => {
  58. if (!channel) return;
  59. return new Promise((resolve, reject) => {
  60. EMClient.deleteConversation({ channel, chatType, deleteRoam })
  61. .then((res) => {
  62. console.log('>>>>会话删除成功');
  63. resolve(res);
  64. })
  65. .catch((error) => {
  66. reject(error);
  67. });
  68. });
  69. };
  70. //发送会话已读回执
  71. const sendChannelAck = (targetId, chatType = CHAT_TYPE.SINGLE_CHAT) => {
  72. if (!targetId) return;
  73. let option = {
  74. chatType: chatType, // 会话类型,设置为单聊。
  75. type: 'channel', // 消息类型。
  76. to: targetId, // 接收消息对象的用户 ID。
  77. };
  78. const msg = EaseSDK.message.create(option);
  79. EMClient.send(msg);
  80. };
  81. return {
  82. fetchPinConversationFromServer,
  83. pinConversationItem,
  84. fetchConversationFromServer,
  85. removeConversationFromServer,
  86. sendChannelAck,
  87. };
  88. };
  89. export default emConversation;