rongcloud.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as RongIMLib from '@rongcloud/imlib-v4'
  2. import config from '@/config';
  3. let appkey=config.RongYun.appkey;
  4. const im = RongIMLib.init({ appkey});
  5. export function rongcloudInit(params){
  6. im.watch({
  7. // 监听会话列表变更事件
  8. conversation (event) {
  9. if(params.conversation){
  10. params.conversation(event)
  11. }
  12. },
  13. // 监听消息通知
  14. message (event) {
  15. const message = event.message;
  16. console.log('收到消息'+message)
  17. if(params.receiveMsg){
  18. params.receiveMsg(message)
  19. }
  20. },
  21. // 监听 IM 连接状态变化
  22. status (event) {
  23. console.log('监听 IM 连接状态变化connection status:', event.status);
  24. if(params.status){
  25. params.status(event)
  26. }
  27. },
  28. // 监听聊天室 KV 数据变更
  29. chatroom (event) {
  30. /**
  31. * 聊天室 KV 存储数据更新
  32. * @example
  33. * [
  34. * {
  35. * 'key': 'name',
  36. * 'value': '我是小融融',
  37. * 'timestamp': 1597591258338,
  38. * 'chatroomId': 'z002',
  39. * 'type': 1 // 1: 更新( 含:修改和新增 )、2: 删除
  40. * },
  41. * ]
  42. */
  43. const updatedEntries = event.updatedEntries
  44. if(params.updatedEntries){
  45. params.updatedEntries(updatedEntries)
  46. }
  47. },
  48. expansion (event) {
  49. /**
  50. * 更新的消息拓展数据
  51. * @example {
  52. * expansion: { key: 'value' }, // 设置或更新的扩展值
  53. * messageUId: 'URIT-URIT-ODMF-DURR' // 设置或更新扩展的消息 uid
  54. * }
  55. */
  56. //const updatedExpansion = event.updatedExpansion;
  57. /**
  58. * 删除的消息拓展数据
  59. * @example {
  60. * deletedKeys: ['key1', 'key2'], // 设置或更新的扩展值
  61. * messageUId: 'URIT-URIT-ODMF-DURR' // 设置或更新扩展的消息 uid
  62. * }
  63. */
  64. const deletedExpansion = event.deletedExpansion;
  65. if(params.deletedExpansion){
  66. params.deletedExpansion(deletedExpansion)
  67. }
  68. }
  69. });
  70. }
  71. export function connect({token}) {
  72. im.connect({ token}).then(user => {
  73. console.log('链接成功, 链接用户 id 为: ', user.id);
  74. }).catch(error => {
  75. console.log('链接失败: ', error.code, error.msg);
  76. });
  77. }
  78. // 向会话内发消息
  79. export function sendMsg({sendmessage,targetId}) {
  80. console.log('向会话内发消息')
  81. return new Promise(function(resolve,reject){
  82. const conversation = im.Conversation.get({
  83. // targetId
  84. targetId,
  85. // 会话类型:RongIMLib.CONVERSATION_TYPE.PRIVATE | RongIMLib.CONVERSATION_TYPE.GROUP
  86. type: RongIMLib.CONVERSATION_TYPE.PRIVATE
  87. });
  88. conversation.send({
  89. // 消息类型,其中 RongIMLib.MESSAGE_TYPE 为 IMLib 内部的内置消息类型常量定义
  90. messageType: RongIMLib.MESSAGE_TYPE.TEXT, // 'RC:TxtMsg'
  91. // 消息内容
  92. content: {
  93. content:sendmessage // 文本内容
  94. }
  95. }).then(function(message){
  96. console.log('发送文字成功', message);
  97. resolve(message)
  98. }).catch(error => {
  99. reject(error)
  100. console.log('发送文字消息失败', error.code, error.msg);
  101. });
  102. })
  103. }