index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <el-container class="kefu-layout">
  3. <!-- 会话列表 -->
  4. <KeFuConversationList ref="keFuConversationRef" @change="handleChange" />
  5. <!-- 会话详情(选中会话的消息列表) -->
  6. <KeFuMessageList ref="keFuChatBoxRef" />
  7. <!-- 会员信息(选中会话的会员信息) -->
  8. <MemberInfo ref="memberInfoRef" />
  9. </el-container>
  10. </template>
  11. <script lang="ts" setup>
  12. import { KeFuConversationList, KeFuMessageList, MemberInfo } from './components'
  13. import { WebSocketMessageTypeConstants } from './components/tools/constants'
  14. import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
  15. import { getRefreshToken } from '@/utils/auth'
  16. import { useWebSocket } from '@vueuse/core'
  17. import { useMallKefuStore } from '@/store/modules/mall/kefu'
  18. defineOptions({ name: 'KeFu' })
  19. const message = useMessage() // 消息弹窗
  20. const kefuStore = useMallKefuStore() // 客服缓存
  21. // ======================= WebSocket start =======================
  22. const server = ref(
  23. (import.meta.env.VITE_BASE_URL + '/infra/ws').replace('http', 'ws') +
  24. '?token=' +
  25. getRefreshToken() // 使用 getRefreshToken() 方法,而不使用 getAccessToken() 方法的原因:WebSocket 无法方便的刷新访问令牌
  26. ) // WebSocket 服务地址
  27. /** 发起 WebSocket 连接 */
  28. const { data, close, open } = useWebSocket(server.value, {
  29. autoReconnect: true,
  30. heartbeat: true
  31. })
  32. /** 监听 WebSocket 数据 */
  33. watch(
  34. () => data.value,
  35. (newData) => {
  36. if (!newData) return
  37. try {
  38. // 1. 收到心跳
  39. if (newData === 'pong') return
  40. // 2.1 解析 type 消息类型
  41. const jsonMessage = JSON.parse(newData)
  42. const type = jsonMessage.type
  43. if (!type) {
  44. message.error('未知的消息类型:' + newData)
  45. return
  46. }
  47. // 2.2 消息类型:KEFU_MESSAGE_TYPE
  48. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
  49. const message = JSON.parse(jsonMessage.content)
  50. // 刷新会话列表
  51. kefuStore.updateConversation(message.conversationId)
  52. // 刷新消息列表
  53. keFuChatBoxRef.value?.refreshMessageList(message)
  54. return
  55. }
  56. // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
  57. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
  58. // 更新会话已读
  59. const message = JSON.parse(jsonMessage.content)
  60. kefuStore.updateConversationStatus(message.conversationId)
  61. }
  62. } catch (error) {
  63. console.error(error)
  64. }
  65. },
  66. {
  67. immediate: false // 不立即执行
  68. }
  69. )
  70. // ======================= WebSocket end =======================
  71. /** 加载指定会话的消息列表 */
  72. const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>()
  73. const memberInfoRef = ref<InstanceType<typeof MemberInfo>>()
  74. const handleChange = (conversation: KeFuConversationRespVO) => {
  75. keFuChatBoxRef.value?.getNewMessageList(conversation)
  76. memberInfoRef.value?.initHistory(conversation)
  77. }
  78. const keFuConversationRef = ref<InstanceType<typeof KeFuConversationList>>()
  79. /** 初始化 */
  80. onMounted(() => {
  81. /** 加载会话列表 */
  82. kefuStore.setConversationList().then(() => {
  83. keFuConversationRef.value?.calculationLastMessageTime()
  84. })
  85. // 打开 websocket 连接
  86. open()
  87. })
  88. /** 销毁 */
  89. onBeforeUnmount(() => {
  90. // 关闭 websocket 连接
  91. close()
  92. })
  93. </script>
  94. <style lang="scss">
  95. .kefu-layout {
  96. position: absolute;
  97. flex: 1;
  98. top: 0;
  99. left: 0;
  100. height: 100%;
  101. width: 100%;
  102. }
  103. /* 定义滚动条样式 */
  104. ::-webkit-scrollbar {
  105. width: 10px;
  106. height: 6px;
  107. }
  108. /* 定义滚动条轨道 内阴影+圆角 */
  109. ::-webkit-scrollbar-track {
  110. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  111. border-radius: 10px;
  112. background-color: #fff;
  113. }
  114. /* 定义滑块 内阴影+圆角 */
  115. ::-webkit-scrollbar-thumb {
  116. border-radius: 10px;
  117. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  118. background-color: rgba(240, 240, 240, 0.5);
  119. }
  120. </style>