import { getParticipantArtwork } from '@/utils/artwork' import { formatConversationPreview } from '@/utils/chat-preview' import PageTitle from '@/components/page-title' import { getPageStyle } from '@/utils/page-style' import { setActiveTabRoute } from '@/utils/tab-bar' import { View, Text, ScrollView, Image } from '@tarojs/components' import { useState } from 'react' import Taro, { useDidShow } from '@tarojs/taro' import { getToken } from '@/services/auth' import { getConversations, ChatConversation } from '../../services/chat' import { useSystemBanner } from '@/components/system-banner/use-system-banner' import { usePageShare } from '@/utils/page-share' import './index.scss' export default function Chat() { usePageShare() const systemBanner = useSystemBanner() const [conversations, setConversations] = useState([]) const [loading, setLoading] = useState(true) const [isGuest, setIsGuest] = useState(false) useDidShow(() => { setActiveTabRoute('pages/chat/index') void loadConversations() }) const loadConversations = async () => { if (!getToken()) { setConversations([]) setIsGuest(true) setLoading(false) return } setIsGuest(false) 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}` + `&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'}`, }) } const handleGuestLogin = () => { Taro.setStorageSync('postLoginRedirect', '/pages/chat/index') Taro.navigateTo({ url: '/pages/login/index' }) } const handleGuestPreviewTap = (title: string, content: string) => { Taro.showModal({ title, content, showCancel: false, confirmText: '我知道了', }) } return ( {isGuest ? ( 💬 亲子互动会话 这里会汇总宝贝、家长和 AI 玩伴之间的消息。绑定设备后,真实会话会按宝贝和设备自动归档。 会话示例 handleGuestPreviewTap( '会话预览', '这里展示玩伴页的会话能力。绑定设备后,会显示宝贝、家长和 AI 玩伴的真实消息记录。' )} > 预览 handleGuestPreviewTap( 'AI 玩伴', '绑定设备后,可以在这里查看宝贝与 AI 玩伴的真实会话记录,包括聊天内容、时间和对应宝贝。' )} > AI 玩伴 陪宝贝聊天、讲故事,也能留下可回看记录。 handleGuestPreviewTap( 'AI 玩伴', '绑定设备后,可以在这里查看宝贝与 AI 玩伴的真实会话记录,包括聊天内容、时间和对应宝贝。' )} > 查看 handleGuestPreviewTap( '家长留言', '绑定设备后,家长可以查看消息记录,也可以给宝贝发送语音留言,设备端会收到对应提醒。' )} > 家长留言 家长可查看设备消息,并给宝贝发送语音留言。 handleGuestPreviewTap( '家长留言', '绑定设备后,家长可以查看消息记录,也可以给宝贝发送语音留言,设备端会收到对应提醒。' )} > 查看 开始使用 浏览功能不需要登录;当你需要绑定设备、查看真实会话记录时,再进入登录和绑定流程。 去登录 / 绑定设备 ) : loading ? ( 加载中... ) : conversations.length === 0 ? ( 当前宝贝暂无会话 ) : ( {conversations.map((conversation) => ( handleConversationClick(conversation)} > {conversation.name} {conversation.typeLabel} {conversation.time} {formatConversationPreview(conversation.lastMessage)} {conversation.childName && {conversation.childName}} ))} )} {systemBanner} ) }