consultationRecords.vue 4.5 KB

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