import { View, Text, ScrollView } 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 './index.scss' export default function Chat() { const [conversations, setConversations] = useState([]) const [loading, setLoading] = useState(true) useDidShow(() => { void loadConversations() }) const loadConversations = async () => { 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}` + `&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 ( 玩伴 {loading ? ( 加载中... ) : conversations.length === 0 ? ( 暂无玩伴会话 ) : ( {conversations.map((conversation) => ( handleConversationClick(conversation)} > {conversation.avatar} {conversation.name} {conversation.typeLabel} {conversation.time} {conversation.lastMessage} {conversation.childName && {conversation.childName}} ))} )} ) }