consultationRecords.vue 4.0 KB

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