import { useState } from 'react'; import { View, Text } from '@tarojs/components'; import Taro, { useDidShow } from '@tarojs/taro'; import { listConsultations, ConsultationSession } from '../../services/consultation'; import Loading from '../../components/Loading'; import './index.scss'; type MsgTab = 'consultation' | 'notification'; interface NotificationItem { id: string; title: string; desc: string; time: string; type: string; } export default function Messages() { const [activeTab, setActiveTab] = useState('consultation'); const [sessions, setSessions] = useState([]); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(false); useDidShow(() => { loadData(activeTab); }); const loadData = async (tab: MsgTab) => { setLoading(true); try { if (tab === 'consultation') { const res = await listConsultations({ page: 1, page_size: 20 }); setSessions(res.data || []); } else { // 通知目前从咨询中提取状态变化作为示例 // 后续可对接独立通知 API setNotifications([]); } } catch { if (tab === 'consultation') setSessions([]); else setNotifications([]); } finally { setLoading(false); } }; const handleTabChange = (tab: MsgTab) => { setActiveTab(tab); loadData(tab); }; const formatTime = (dateStr: string | null) => { if (!dateStr) return ''; const d = new Date(dateStr); const now = new Date(); const diffMs = now.getTime() - d.getTime(); const diffMin = Math.floor(diffMs / 60000); if (diffMin < 60) return `${diffMin} 分钟前`; const diffHour = Math.floor(diffMin / 60); if (diffHour < 24) return `${diffHour} 小时前`; return dateStr.slice(0, 10); }; return ( {/* 页头 */} 消息 {/* Tab 切换 */} handleTabChange('consultation')} > 咨询 handleTabChange('notification')} > 通知 {/* 咨询列表 */} {activeTab === 'consultation' && ( {loading ? ( ) : sessions.length === 0 ? ( 暂无咨询消息 ) : ( sessions.map((session) => ( Taro.navigateTo({ url: `/pages/consultation/detail/index?id=${session.id}` })} > {session.consultation_type === 'online' ? '在线咨询' : '门诊咨询'} {session.last_message || session.subject || '暂无消息'} {formatTime(session.last_message_at)} {session.unread_count_patient > 0 && ( {session.unread_count_patient > 99 ? '99+' : session.unread_count_patient} )} )) )} )} {/* 通知列表 */} {activeTab === 'notification' && ( {loading ? ( ) : notifications.length === 0 ? ( 暂无新通知 ) : ( notifications.map((n) => ( {n.title} {n.desc} {n.time} )) )} )} ); }