index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <el-row :gutter="10">
  3. <!-- 会话列表 -->
  4. <el-col :span="8">
  5. <ContentWrap>
  6. <KeFuConversationList ref="keFuConversationRef" @change="handleChange" />
  7. </ContentWrap>
  8. </el-col>
  9. <!-- 会话详情(选中会话的消息列表) -->
  10. <el-col :span="16">
  11. <ContentWrap>
  12. <KeFuMessageList ref="keFuChatBoxRef" @change="getConversationList" />
  13. </ContentWrap>
  14. </el-col>
  15. </el-row>
  16. </template>
  17. <script lang="ts" setup>
  18. import { KeFuConversationList, KeFuMessageList } from './components'
  19. import { WebSocketMessageTypeConstants } from './components/tools/constants'
  20. import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
  21. import { getAccessToken } from '@/utils/auth'
  22. import { useWebSocket } from '@vueuse/core'
  23. defineOptions({ name: 'KeFu' })
  24. const message = useMessage() // 消息弹窗
  25. // ======================= WebSocket start =======================
  26. const server = ref(
  27. (import.meta.env.VITE_BASE_URL + '/infra/ws').replace('http', 'ws') + '?token=' + getAccessToken()
  28. ) // WebSocket 服务地址
  29. /** 发起 WebSocket 连接 */
  30. const { data, close, open } = useWebSocket(server.value, {
  31. autoReconnect: true,
  32. heartbeat: true
  33. })
  34. /** 监听 WebSocket 数据 */
  35. watchEffect(() => {
  36. if (!data.value) {
  37. return
  38. }
  39. try {
  40. // 1. 收到心跳
  41. if (data.value === 'pong') {
  42. return
  43. }
  44. // 2.1 解析 type 消息类型
  45. const jsonMessage = JSON.parse(data.value)
  46. const type = jsonMessage.type
  47. if (!type) {
  48. message.error('未知的消息类型:' + data.value)
  49. return
  50. }
  51. // 2.2 消息类型:KEFU_MESSAGE_TYPE
  52. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
  53. // 刷新会话列表
  54. getConversationList()
  55. // 刷新消息列表
  56. keFuChatBoxRef.value?.refreshMessageList()
  57. return
  58. }
  59. // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
  60. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
  61. // 刷新会话列表
  62. getConversationList()
  63. }
  64. } catch (error) {
  65. console.error(error)
  66. }
  67. })
  68. // ======================= WebSocket end =======================
  69. /** 加载会话列表 */
  70. const keFuConversationRef = ref<InstanceType<typeof KeFuConversationList>>()
  71. const getConversationList = () => {
  72. keFuConversationRef.value?.getConversationList()
  73. }
  74. /** 加载指定会话的消息列表 */
  75. const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>()
  76. const handleChange = (conversation: KeFuConversationRespVO) => {
  77. keFuChatBoxRef.value?.getMessageList(conversation, true)
  78. }
  79. /** 初始化 */
  80. onMounted(() => {
  81. getConversationList()
  82. // 打开 websocket 连接
  83. open()
  84. })
  85. /** 销毁 */
  86. onBeforeUnmount(() => {
  87. // 关闭 websocket 连接
  88. close()
  89. })
  90. </script>
  91. <style lang="scss">
  92. .kefu {
  93. height: calc(100vh - 165px);
  94. overflow: auto; /* 确保内容可滚动 */
  95. }
  96. /* 定义滚动条样式 */
  97. ::-webkit-scrollbar {
  98. width: 10px;
  99. height: 6px;
  100. }
  101. /* 定义滚动条轨道 内阴影+圆角 */
  102. ::-webkit-scrollbar-track {
  103. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  104. border-radius: 10px;
  105. background-color: #fff;
  106. }
  107. /* 定义滑块 内阴影+圆角 */
  108. ::-webkit-scrollbar-thumb {
  109. border-radius: 10px;
  110. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  111. background-color: rgba(240, 240, 240, 0.5);
  112. }
  113. </style>