useIM.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { ref, onMounted, onUnmounted } from 'vue';
  2. import { getConversationSync } from '@/api/common'
  3. // 配置悟空IM
  4. import {
  5. MessageText,
  6. Channel,
  7. WKSDK,
  8. ChannelTypePerson,
  9. // ChannelTypeGroup
  10. } from "wukongimjssdk"
  11. import Snackbar from '@/plugins/snackbar'
  12. import { useUserStore } from '@/store/user'
  13. const userStore = useUserStore()
  14. const ConnectStatus = {
  15. Disconnect: 0, // 断开连接
  16. Connected: 1, // 连接成功
  17. Connecting: 2, // 连接中
  18. ConnectFail: 3, // 连接错误
  19. ConnectKick: 4, // 连接被踢,服务器要求客户端断开(一般是账号在其他地方登录,被踢)
  20. }
  21. initDataSource()
  22. // api 接入
  23. function initDataSource () {
  24. // 最近会话数据源
  25. WKSDK.shared().config.provider.syncConversationsCallback = async () => {
  26. const res = await getConversationSync({ msg_count: 100 })
  27. return res
  28. }
  29. }
  30. export function initKey () {
  31. return new Promise((resolve) => {
  32. userStore.getChatKey().then(() => {
  33. resolve()
  34. }).catch((error) => {
  35. // 报错重连
  36. console.log(error.msg)
  37. setTimeout(() => {
  38. initKey()
  39. }, 3000)
  40. })
  41. })
  42. }
  43. export function initConnect () {
  44. const connected = ref(false)
  45. const conversationList = ref([])
  46. // 单机模式可以直接设置地址
  47. WKSDK.shared().config.addr = 'ws://' + userStore.IMConfig.wsUrl // 默认端口为5200
  48. // 认证信息
  49. WKSDK.shared().config.uid = userStore.IMConfig.uid // 用户uid(需要在悟空通讯端注册过)
  50. WKSDK.shared().config.token = userStore.IMConfig.token // 用户token (需要在悟空通讯端注册过)
  51. // 连接
  52. onMounted(() => {
  53. // 连接状态监听
  54. WKSDK.shared().connectManager.addConnectStatusListener(
  55. async (status, reasonCode) => {
  56. if (status === ConnectStatus.Connected) {
  57. console.log('连接成功')
  58. connected.value = true
  59. // 获取列表
  60. const res = await syncConversation()
  61. conversationList.value = [...res]
  62. } else {
  63. console.log('reasonCode', reasonCode)
  64. connected.value = false
  65. conversationList.value = []
  66. }
  67. }
  68. )
  69. // 消息发送状态监听
  70. WKSDK.shared().chatManager.addMessageStatusListener(statusListen)
  71. // 常规消息监听
  72. WKSDK.shared().chatManager.addMessageListener(messageListen)
  73. connect()
  74. })
  75. onUnmounted(() => {
  76. // 连接状态监听移除
  77. WKSDK.shared().connectManager.disconnect()
  78. // 消息发送状态监听移除
  79. WKSDK.shared().chatManager.removeMessageStatusListener(statusListen)
  80. // 常规消息监听移除
  81. WKSDK.shared().chatManager.removeMessageListener(messageListen)
  82. })
  83. return { connected, conversationList }
  84. }
  85. // 消息发送状态监听
  86. function statusListen (packet) {
  87. if (packet.reasonCode === 1) {
  88. // 发送成功
  89. } else {
  90. // 发送失败
  91. }
  92. }
  93. // 常规消息监听
  94. function messageListen (message) {
  95. console.log('收到消息', message)
  96. // message.content // 消息内容
  97. // message.channel // 消息频道
  98. // message.fromUID // 消息发送者
  99. }
  100. export function connect () {
  101. WKSDK.shared().connectManager.connect()
  102. }
  103. export function send (user, text) {
  104. // 例如发送文本消息hello给用户u10001
  105. const _text = new MessageText(text) // 文本消息
  106. WKSDK.shared().chatManager.send(_text, new Channel(user, ChannelTypePerson))
  107. // 例如发送文本消息hello给群频道g10001
  108. // WKSDK.shared().chatManager.send(_text, new Channel(user,ChannelTypeGroup))
  109. }
  110. // 同步最近会话
  111. export async function syncConversation () {
  112. return new Promise((resolve) => {
  113. WKSDK.shared().conversationManager.sync().then(res => {
  114. resolve(res)
  115. }).catch(error => {
  116. Snackbar.error(error.msg)
  117. })
  118. })
  119. }