index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <el-container class="kefu-layout">
  3. <!-- 会话列表 -->
  4. <KeFuConversationList ref="keFuConversationRef" @change="handleChange" />
  5. <!-- 会话详情(选中会话的消息列表) -->
  6. <KeFuMessageList ref="keFuChatBoxRef" @change="getConversationList" />
  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. defineOptions({ name: 'KeFu' })
  18. const message = useMessage() // 消息弹窗
  19. // ======================= WebSocket start =======================
  20. const server = ref(
  21. (import.meta.env.VITE_BASE_URL + '/infra/ws').replace('http', 'ws') +
  22. '?token=' +
  23. getRefreshToken() // 使用 getRefreshToken() 方法,而不使用 getAccessToken() 方法的原因:WebSocket 无法方便的刷新访问令牌
  24. ) // WebSocket 服务地址
  25. /** 发起 WebSocket 连接 */
  26. const { data, close, open } = useWebSocket(server.value, {
  27. autoReconnect: true,
  28. heartbeat: true
  29. })
  30. /** 监听 WebSocket 数据 */
  31. watchEffect(() => {
  32. if (!data.value) {
  33. return
  34. }
  35. try {
  36. // 1. 收到心跳
  37. if (data.value === 'pong') {
  38. return
  39. }
  40. // 2.1 解析 type 消息类型
  41. const jsonMessage = JSON.parse(data.value)
  42. const type = jsonMessage.type
  43. if (!type) {
  44. message.error('未知的消息类型:' + data.value)
  45. return
  46. }
  47. // 2.2 消息类型:KEFU_MESSAGE_TYPE
  48. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
  49. // 刷新会话列表
  50. // TODO @puhui999:不应该刷新列表,而是根据消息,本地 update 列表的数据;
  51. getConversationList()
  52. // 刷新消息列表
  53. keFuChatBoxRef.value?.refreshMessageList(JSON.parse(jsonMessage.content))
  54. return
  55. }
  56. // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
  57. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
  58. // 刷新会话列表
  59. // TODO @puhui999:不应该刷新列表,而是根据消息,本地 update 列表的数据;
  60. getConversationList()
  61. }
  62. } catch (error) {
  63. console.error(error)
  64. }
  65. })
  66. // ======================= WebSocket end =======================
  67. /** 加载会话列表 */
  68. const keFuConversationRef = ref<InstanceType<typeof KeFuConversationList>>()
  69. const getConversationList = () => {
  70. keFuConversationRef.value?.getConversationList()
  71. }
  72. /** 加载指定会话的消息列表 */
  73. const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>()
  74. const memberInfoRef = ref<InstanceType<typeof MemberInfo>>()
  75. const handleChange = (conversation: KeFuConversationRespVO) => {
  76. keFuChatBoxRef.value?.getNewMessageList(conversation)
  77. memberInfoRef.value?.initHistory(conversation)
  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-layout {
  93. position: absolute;
  94. flex: 1;
  95. top: 0;
  96. left: 0;
  97. height: 100%;
  98. width: 100%;
  99. }
  100. /* 定义滚动条样式 */
  101. ::-webkit-scrollbar {
  102. width: 10px;
  103. height: 6px;
  104. }
  105. /* 定义滚动条轨道 内阴影+圆角 */
  106. ::-webkit-scrollbar-track {
  107. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  108. border-radius: 10px;
  109. background-color: #fff;
  110. }
  111. /* 定义滑块 内阴影+圆角 */
  112. ::-webkit-scrollbar-thumb {
  113. border-radius: 10px;
  114. box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
  115. background-color: rgba(240, 240, 240, 0.5);
  116. }
  117. </style>