consumer模块完成95%,在和商家端对接聊天购物闭环
This commit is contained in:
@@ -214,6 +214,7 @@
|
||||
|
||||
<script setup lang="uts">
|
||||
import { ref, reactive, computed, onMounted } from 'vue'
|
||||
import { onShow } from '@dcloudio/uni-app'
|
||||
import { supabaseService, type Notification, type ChatMessage } from '@/utils/supabaseService.uts'
|
||||
|
||||
// 响应式数据
|
||||
@@ -265,7 +266,12 @@ const currentMessages = computed(() => {
|
||||
onMounted(() => {
|
||||
console.log('Messages Page Mounted')
|
||||
initPage()
|
||||
loadMessages()
|
||||
// loadMessages() // 移至 onShow 调用
|
||||
})
|
||||
|
||||
onShow(() => {
|
||||
console.log('Messages Page Show')
|
||||
loadMessages()
|
||||
})
|
||||
|
||||
// 简单的日期格式化
|
||||
@@ -332,37 +338,79 @@ const loadMessages = async () => {
|
||||
|
||||
// 2. 获取客服消息 (Chat)
|
||||
const chats = await supabaseService.getUserChatMessages()
|
||||
if (chats.length > 0) {
|
||||
// 简单处理:将最新一条显示为"在线客服"会话
|
||||
const lastMsg = chats[0]
|
||||
serviceMessages.push({
|
||||
id: lastMsg.id,
|
||||
title: '在线客服',
|
||||
role: '客服专员',
|
||||
content: lastMsg.content,
|
||||
lastMessage: lastMsg.content,
|
||||
time: formatTime(lastMsg.created_at || ''),
|
||||
read: lastMsg.is_read,
|
||||
type: 'service',
|
||||
avatar: '/static/icons/service-avatar.png',
|
||||
online: true,
|
||||
unreadCount: chats.filter((m: ChatMessage) => !m.is_read && !m.is_from_user).length,
|
||||
tags: ['官方客服'],
|
||||
icon: '👩💼',
|
||||
color: '#2196F3',
|
||||
important: false,
|
||||
coupon: '',
|
||||
expiry: '',
|
||||
claimed: false,
|
||||
order_no: '',
|
||||
status: '',
|
||||
statusText: ''
|
||||
})
|
||||
} else {
|
||||
// 如果没有真实数据,保留一个默认客服入口
|
||||
// console.log('Raw chats:', chats)
|
||||
|
||||
if (chats.length > 0) {
|
||||
const currentUserId = supabaseService.getCurrentUserId()
|
||||
const conversations = new Map<string, any>()
|
||||
|
||||
// 1. Group by conversation partner
|
||||
for (const msg of chats) {
|
||||
const partnerId = (msg.sender_id == currentUserId) ? msg.receiver_id : msg.sender_id
|
||||
|
||||
// Skip if partner is null/invalid
|
||||
if (!partnerId) continue;
|
||||
|
||||
if (!conversations.has(partnerId)) {
|
||||
conversations.set(partnerId, {
|
||||
partnerId: partnerId,
|
||||
lastMessage: msg,
|
||||
unreadCount: 0
|
||||
})
|
||||
}
|
||||
|
||||
const conv = conversations.get(partnerId)
|
||||
// Since chats are likely sorted desc, the first one seen is the latest.
|
||||
// Just count unread: if I am the receiver and it's not read
|
||||
if (msg.receiver_id == currentUserId && !msg.is_read) {
|
||||
conv.unreadCount++
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Conversations found:', conversations.size)
|
||||
|
||||
// 2. Fetch shop details for each conversation
|
||||
const convList = Array.from(conversations.values())
|
||||
const promises = convList.map(async (conv) => {
|
||||
const shop = await supabaseService.getShopByMerchantId(conv.partnerId)
|
||||
const shopName = shop ? shop.shop_name : '未知商家'
|
||||
const shopAvatar = (shop && shop.logo_url) ? shop.logo_url : '/static/icons/shop-default.png'
|
||||
|
||||
return {
|
||||
id: conv.partnerId, // Use partnerId as the ID for navigation
|
||||
title: shopName,
|
||||
role: '商家客服',
|
||||
content: conv.lastMessage.content,
|
||||
lastMessage: conv.lastMessage.content,
|
||||
time: formatTime(conv.lastMessage.created_at || ''),
|
||||
read: conv.unreadCount === 0,
|
||||
type: 'service',
|
||||
avatar: shopAvatar,
|
||||
online: true,
|
||||
unreadCount: conv.unreadCount,
|
||||
tags: shop ? ['官方认证'] : [],
|
||||
icon: '🏪',
|
||||
color: '#FF9800',
|
||||
important: false,
|
||||
coupon: '',
|
||||
expiry: '',
|
||||
claimed: false,
|
||||
order_no: '',
|
||||
status: '',
|
||||
statusText: ''
|
||||
}
|
||||
})
|
||||
|
||||
const renderedMessages = await Promise.all(promises)
|
||||
serviceMessages.push(...renderedMessages)
|
||||
|
||||
}
|
||||
|
||||
// 如果没有消息,为了演示效果(或者真的需要),可以保留一个默认的系统客服
|
||||
if (serviceMessages.length === 0) {
|
||||
serviceMessages.push({
|
||||
id: 'default_service',
|
||||
title: '在线客服',
|
||||
title: '平台客服',
|
||||
role: '智能助手',
|
||||
content: '有问题请随时联系我们',
|
||||
lastMessage: '欢迎咨询',
|
||||
@@ -429,8 +477,12 @@ const startChatWithService = (message: any) => {
|
||||
message.unreadCount = 0
|
||||
updateUnreadCount()
|
||||
|
||||
// 这里的 message.id 已经被我们修改为 conversation partner (merchantId)
|
||||
// 所以参数传递需要调整
|
||||
const merchantId = message.id === 'default_service' ? '' : message.id
|
||||
|
||||
uni.navigateTo({
|
||||
url: `/pages/mall/consumer/chat?id=${message.id}&name=${encodeURIComponent(message.title)}&role=${encodeURIComponent(message.role)}`
|
||||
url: `/pages/mall/consumer/chat?merchantId=${merchantId}&merchantName=${encodeURIComponent(message.title)}`
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user