feat(小程序): 优化玩伴会话与家长发消息
This commit is contained in:
@@ -1,63 +1,102 @@
|
||||
import { View, Text, ScrollView } from '@tarojs/components'
|
||||
import { useState, useEffect } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import { useState } from 'react'
|
||||
import Taro, { useDidShow } from '@tarojs/taro'
|
||||
import { getToken } from '@/services/auth'
|
||||
import { getConversations, ChatConversation } from '../../services/chat'
|
||||
import './index.scss'
|
||||
|
||||
export default function Chat() {
|
||||
const [conversations, setConversations] = useState<ChatConversation[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
loadConversations()
|
||||
}, [])
|
||||
useDidShow(() => {
|
||||
void loadConversations()
|
||||
})
|
||||
|
||||
const loadConversations = async () => {
|
||||
const data = await getConversations()
|
||||
setConversations(data)
|
||||
if (!getToken()) {
|
||||
Taro.reLaunch({ url: '/pages/login/index' })
|
||||
return
|
||||
}
|
||||
|
||||
setLoading(true)
|
||||
try {
|
||||
const data = await getConversations()
|
||||
setConversations(data)
|
||||
} catch (error: any) {
|
||||
console.error('[chat] load failed:', error)
|
||||
Taro.showToast({
|
||||
title: error?.message || '加载失败,请稍后重试',
|
||||
icon: 'none',
|
||||
})
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleConversationClick = (conversation: ChatConversation) => {
|
||||
Taro.navigateTo({
|
||||
url: `/pages/chat/detail/index?id=${conversation.id}&name=${encodeURIComponent(conversation.name)}`
|
||||
url:
|
||||
`/pages/chat/detail/index?id=${conversation.id}` +
|
||||
`&source=${conversation.source}` +
|
||||
`&name=${encodeURIComponent(conversation.name)}` +
|
||||
`&peerKind=${conversation.peerKind}` +
|
||||
`&roleKey=${encodeURIComponent(conversation.roleKey || '')}` +
|
||||
`&conversationTypeName=${encodeURIComponent(conversation.conversationTypeName || '')}` +
|
||||
`&peerId=${encodeURIComponent(conversation.peerId || '')}` +
|
||||
`&childId=${conversation.childId || ''}` +
|
||||
`&childName=${encodeURIComponent(conversation.childName || '')}` +
|
||||
`&parentUserId=${conversation.parentUserId || ''}` +
|
||||
`&deviceId=${encodeURIComponent(conversation.deviceId || '')}` +
|
||||
`&channelLabel=${encodeURIComponent(conversation.channelLabel || '')}` +
|
||||
`&canSend=${conversation.canSend ? '1' : '0'}`,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<View className='chat-page'>
|
||||
<View className='page-header'>
|
||||
<Text className='page-title'>AI 玩伴</Text>
|
||||
<Text className='page-subtitle'>选择玩伴开始对话</Text>
|
||||
<Text className='page-title'>玩伴</Text>
|
||||
</View>
|
||||
|
||||
<ScrollView className='conversation-list' scrollY>
|
||||
{conversations.map((conversation) => (
|
||||
<View
|
||||
key={conversation.id}
|
||||
className='conversation-item'
|
||||
onClick={() => handleConversationClick(conversation)}
|
||||
>
|
||||
<View className='conversation-avatar'>
|
||||
<Text className='avatar-icon'>{conversation.avatar}</Text>
|
||||
</View>
|
||||
<View className='conversation-content'>
|
||||
<View className='conversation-header'>
|
||||
<Text className='conversation-name'>{conversation.name}</Text>
|
||||
<Text className='conversation-time'>{conversation.time}</Text>
|
||||
{loading ? (
|
||||
<View className='loading'>
|
||||
<Text>加载中...</Text>
|
||||
</View>
|
||||
) : conversations.length === 0 ? (
|
||||
<View className='empty-card'>
|
||||
<Text className='empty-title'>暂无玩伴会话</Text>
|
||||
</View>
|
||||
) : (
|
||||
<ScrollView className='conversation-list' scrollY>
|
||||
{conversations.map((conversation) => (
|
||||
<View
|
||||
key={conversation.key}
|
||||
className='conversation-item'
|
||||
onClick={() => handleConversationClick(conversation)}
|
||||
>
|
||||
<View className='conversation-avatar'>
|
||||
<Text className='avatar-icon'>{conversation.avatar}</Text>
|
||||
</View>
|
||||
<View className='conversation-footer'>
|
||||
<Text className='last-message' numberOfLines={1}>
|
||||
{conversation.lastMessage}
|
||||
</Text>
|
||||
{conversation.unreadCount && conversation.unreadCount > 0 && (
|
||||
<View className='unread-badge'>
|
||||
<Text className='unread-count'>{conversation.unreadCount}</Text>
|
||||
<View className='conversation-content'>
|
||||
<View className='conversation-header'>
|
||||
<View className='conversation-name-wrap'>
|
||||
<Text className='conversation-name'>{conversation.name}</Text>
|
||||
<Text className={`conversation-tag ${conversation.peerKind}`}>{conversation.typeLabel}</Text>
|
||||
</View>
|
||||
)}
|
||||
<Text className='conversation-time'>{conversation.time}</Text>
|
||||
</View>
|
||||
<View className='conversation-footer'>
|
||||
<Text className='last-message' numberOfLines={1}>
|
||||
{conversation.lastMessage}
|
||||
</Text>
|
||||
</View>
|
||||
{conversation.childName && <Text className='conversation-meta'>{conversation.childName}</Text>}
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
))}
|
||||
</ScrollView>
|
||||
))}
|
||||
</ScrollView>
|
||||
)}
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user