im.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { SocketSession } from '@/store/services/socket-session'
  2. import appSettings from '@/core/config/app-settings'
  3. import { bindSocketClient, isLiveRoomPacket } from '@/features/im/socket-bind'
  4. let session = null
  5. function ensureSession(store) {
  6. if (session) return session
  7. session = new SocketSession({
  8. onOpen: () => {
  9. if (store.state && store.state.im) store.state.im.online = true
  10. setTimeout(() => {
  11. if (store.state && store.state.user && store.state.user.isLogin) {
  12. store.dispatch('im/fetchInbox').catch(() => {})
  13. }
  14. }, 300)
  15. setTimeout(() => {
  16. uni.$emit('im:connected')
  17. uni.$emit('websocket:connected')
  18. }, 500)
  19. },
  20. onMessage: (msg) => store.dispatch('im/dispatchPacket', msg),
  21. onClose: () => { if (store.state && store.state.im) store.state.im.online = false },
  22. onError: () => { if (store.state && store.state.im) store.state.im.online = false }
  23. })
  24. return session
  25. }
  26. export default {
  27. namespaced: true,
  28. state: {
  29. online: false,
  30. badge: 0,
  31. inbox: [],
  32. halted: false
  33. },
  34. mutations: {
  35. setBadge(state, n) {
  36. state.badge = n
  37. },
  38. setInbox(state, list) {
  39. state.inbox = list || []
  40. }
  41. },
  42. actions: {
  43. resume({ state }) {
  44. if (state.halted) {
  45. state.halted = false
  46. ensureSession(this).allowReconnect = true
  47. }
  48. ensureSession(this).open()
  49. },
  50. pause({ state }, permanent = false) {
  51. const sock = ensureSession(this)
  52. if (permanent) {
  53. state.halted = true
  54. sock.allowReconnect = false
  55. }
  56. sock.pause(!permanent)
  57. },
  58. async bindClient(_, clientId) {
  59. await bindSocketClient(clientId)
  60. },
  61. dispatchPacket({ dispatch }, message) {
  62. let packet
  63. try {
  64. packet = JSON.parse(message.data)
  65. } catch (_) {
  66. if (appSettings.debug) console.log('[im] non-json packet ignored')
  67. return
  68. }
  69. uni.$emit('im:packet', packet)
  70. switch (packet?.type) {
  71. case 'ping':
  72. ensureSession(this).send({ type: 'pong' })
  73. return
  74. case 'init':
  75. dispatch('bindClient', packet.client_id).catch(() => {})
  76. return
  77. case 'chat':
  78. uni.$emit('im:chat', packet)
  79. uni.$emit('onMessage', packet)
  80. return
  81. case 'service':
  82. uni.$emit('onService', packet)
  83. return
  84. case 'live':
  85. uni.$emit('onLiveMessage', packet)
  86. return
  87. case 'tip_done':
  88. case 'end':
  89. case 'gift':
  90. uni.$emit('onLiveMessage', packet)
  91. return
  92. default:
  93. break
  94. }
  95. if (isLiveRoomPacket(packet)) {
  96. uni.$emit('onLiveMessage', packet)
  97. }
  98. },
  99. async fetchInbox() {
  100. // 占位:登录后拉取会话列表,页面接入后实现具体接口
  101. }
  102. }
  103. }