index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. watchEffect(() => {
  34. if (!data.value) {
  35. return
  36. }
  37. try {
  38. // 1. 收到心跳
  39. if (data.value === 'pong') {
  40. return
  41. }
  42. // 2.1 解析 type 消息类型
  43. const jsonMessage = JSON.parse(data.value)
  44. const type = jsonMessage.type
  45. if (!type) {
  46. message.error('未知的消息类型:' + data.value)
  47. return
  48. }
  49. // 2.2 消息类型:KEFU_MESSAGE_TYPE
  50. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
  51. const message = JSON.parse(jsonMessage.content)
  52. // 刷新会话列表
  53. kefuStore.updateConversation(message.conversationId)
  54. // 刷新消息列表
  55. keFuChatBoxRef.value?.refreshMessageList(message)
  56. return
  57. }
  58. // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
  59. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
  60. // 更新会话已读
  61. kefuStore.updateConversationStatus(JSON.parse(jsonMessage.content))
  62. }
  63. } catch (error) {
  64. console.error(error)
  65. }
  66. })
  67. // ======================= WebSocket end =======================
  68. /** 加载指定会话的消息列表 */
  69. const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>()
  70. const memberInfoRef = ref<InstanceType<typeof MemberInfo>>()
  71. const handleChange = (conversation: KeFuConversationRespVO) => {
  72. keFuChatBoxRef.value?.getNewMessageList(conversation)
  73. memberInfoRef.value?.initHistory(conversation)
  74. }
  75. /** 初始化 */
  76. onMounted(() => {
  77. /** 加载会话列表 */
  78. kefuStore.setConversationList()
  79. // 打开 websocket 连接
  80. open()
  81. })
  82. /** 销毁 */
  83. onBeforeUnmount(() => {
  84. // 关闭 websocket 连接
  85. close()
  86. })
  87. </script>
  88. <style lang="scss">
  89. .kefu-layout {
  90. position: absolute;
  91. flex: 1;
  92. top: 0;
  93. left: 0;
  94. height: 100%;
  95. width: 100%;
  96. }
  97. /* 定义滚动条样式 */
  98. ::-webkit-scrollbar {
  99. width: 10px;
  100. height: 6px;
  101. }
  102. /* 定义滚动条轨道 内阴影+圆角 */
  103. ::-webkit-scrollbar-track {
  104. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  105. border-radius: 10px;
  106. background-color: #fff;
  107. }
  108. /* 定义滑块 内阴影+圆角 */
  109. ::-webkit-scrollbar-thumb {
  110. border-radius: 10px;
  111. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  112. background-color: rgba(240, 240, 240, 0.5);
  113. }
  114. </style>