consultationRecords.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <s-layout :bgStyle="{ color: '#f2f2f2' }" title="咨询记录">
  3. <scroll-view scroll-y="true" refresher-enabled="true" :refresher-triggered="triggered"
  4. @refresherrefresh="onRefresh">
  5. <view class="chat-list">
  6. <view v-for="item in chatList" :key="item.id" class="chat-item" @click="goToChat(item)">
  7. <view class="avatar">
  8. <image :src="item.userAvatar || activityright" mode="aspectFill"></image>
  9. </view>
  10. <view class="content">
  11. <view class="top">
  12. <text class="nickname">{{ item.userNickname }}</text>
  13. <text class="time">{{ formatTime(item.lastMessageTime) }}</text>
  14. </view>
  15. <view class="message">{{ parseMessage(item.lastMessageContent) }}</view>
  16. </view>
  17. <view v-if="item.relUserId == userInfo.id ? item.relUnreadMessageCount : item.adminUnreadMessageCount"
  18. class="unread-count">
  19. {{ item.relUserId == userInfo.id ? item.relUnreadMessageCount : item.adminUnreadMessageCount }}
  20. </view>
  21. </view>
  22. </view>
  23. </scroll-view>
  24. </s-layout>
  25. </template>
  26. <script setup>
  27. import sheep from '@/sheep';
  28. import { reactive, ref, toRefs, computed } from 'vue';
  29. import { useWebSocket } from '@/sheep/hooks/useWebSocket';
  30. import { onLoad, onShow } from '@dcloudio/uni-app';
  31. import SpuHistoryApi from '@/sheep/api/product/history';
  32. import activityright from '@/static/activity-right.png'
  33. import {
  34. KeFuMessageContentTypeEnum,
  35. WebSocketMessageTypeConstants,
  36. } from '@/pages/chat/util/constants';
  37. const chatList = ref([]);
  38. const triggered = ref(false);
  39. const conversationListAxios = async () => {
  40. const res = await SpuHistoryApi.conversationList();
  41. if (res.code === 0) {
  42. chatList.value = res.data;
  43. }
  44. }
  45. const onRefresh = async () => {
  46. triggered.value = true;
  47. await conversationListAxios();
  48. triggered.value = false;
  49. }
  50. const formatTime = (timestamp) => {
  51. const date = new Date(timestamp);
  52. const now = new Date();
  53. if (date.toDateString() === now.toDateString()) {
  54. return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
  55. }
  56. return date.toLocaleDateString('zh-CN');
  57. }
  58. const parseMessage = (content) => {
  59. try {
  60. const message = JSON.parse(content);
  61. return message.text.replace(/\[.*?\]/g, ''); // 移除表情符号
  62. } catch (e) {
  63. return content;
  64. }
  65. }
  66. const userInfo = computed(() => sheep.$store('user').userInfo);
  67. console.log(userInfo.value.id, 44444)
  68. const goToChat = (item) => {
  69. console.log(item, 44444)
  70. SpuHistoryApi.updateKefuMessageReadStatus({
  71. conversationId: item.id,
  72. })
  73. sheep.$router.go('/pages/chat/index', { conversationId: item.id, relUserId: userInfo.value.id === item.relUserId ? item.userId : item.relUserId })
  74. }
  75. const { options } = useWebSocket({
  76. // 连接成功
  77. onConnected: async () => { },
  78. // 收到消息
  79. onMessage: async (data) => {
  80. const type = data.type;
  81. if (!type) {
  82. console.error('未知的消息类型:' + data);
  83. return;
  84. }
  85. // 2.2 消息类型:KEFU_MESSAGE_TYPE
  86. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
  87. conversationListAxios();
  88. return;
  89. }
  90. // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
  91. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
  92. console.log('管理员已读消息');
  93. }
  94. },
  95. });
  96. const isReconnecting = toRefs(options).isReconnecting; // 重连状态
  97. onShow(() => {
  98. conversationListAxios();
  99. })
  100. onLoad(() => {
  101. // conversationListAxios();
  102. });
  103. </script>
  104. <style lang="scss" scoped>
  105. .chat-list {
  106. padding: 20rpx;
  107. height: calc(100vh - var(--window-top));
  108. .chat-item {
  109. display: flex;
  110. padding: 20rpx;
  111. background: #fff;
  112. border-radius: 12rpx;
  113. margin-bottom: 20rpx;
  114. position: relative;
  115. .avatar {
  116. width: 96rpx;
  117. height: 96rpx;
  118. margin-right: 20rpx;
  119. image {
  120. width: 100%;
  121. height: 100%;
  122. border-radius: 50%;
  123. }
  124. }
  125. .content {
  126. flex: 1;
  127. .top {
  128. display: flex;
  129. justify-content: space-between;
  130. margin-bottom: 10rpx;
  131. .nickname {
  132. font-size: 32rpx;
  133. color: #333;
  134. }
  135. .time {
  136. font-size: 24rpx;
  137. color: #999;
  138. }
  139. }
  140. .message {
  141. font-size: 28rpx;
  142. color: #666;
  143. }
  144. }
  145. .unread-count {
  146. position: absolute;
  147. top: 10rpx;
  148. right: 10rpx;
  149. min-width: 32rpx;
  150. height: 32rpx;
  151. line-height: 32rpx;
  152. text-align: center;
  153. background: #ff4d4f;
  154. color: #fff;
  155. border-radius: 16rpx;
  156. font-size: 20rpx;
  157. padding: 0 6rpx;
  158. }
  159. }
  160. }
  161. </style>