index.vue 4.0 KB

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