import { useState, useRef } from 'react'; import { View, Text } from '@tarojs/components'; import Taro, { useDidShow, usePullDownRefresh, useReachBottom } from '@tarojs/taro'; import { listConsultations, ConsultationSession } from '@/services/consultation'; import Loading from '../../components/Loading'; import './index.scss'; function getStatusTag(status: string) { if (status === 'active') return { label: '进行中', cls: 'tag-ok' }; if (status === 'pending') return { label: '等待接诊', cls: 'tag-warn' }; return { label: { closed: '已结束', cancelled: '已取消' }[status] || status, cls: 'tag-default' }; } function formatTime(iso: string): string { if (!iso) return ''; const d = new Date(iso); const now = new Date(); const diffMin = Math.floor((now.getTime() - d.getTime()) / 60000); if (diffMin < 1) return '刚刚'; if (diffMin < 60) return `${diffMin}分钟前`; const diffHour = Math.floor(diffMin / 60); if (diffHour < 24) return `${diffHour}小时前`; const diffDay = Math.floor(diffHour / 24); if (diffDay < 7) return `${diffDay}天前`; const m = String(d.getMonth() + 1).padStart(2, '0'); const day = String(d.getDate()).padStart(2, '0'); return `${m}-${day}`; } export default function Consultation() { const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); const [page, setPage] = useState(1); const [total, setTotal] = useState(0); const loadingRef = useRef(false); const loadSessions = async (pageNum: number, isRefresh = false) => { if (loadingRef.current) return; loadingRef.current = true; if (isRefresh) setLoading(true); setError(''); try { const resp = await listConsultations({ page: pageNum, page_size: 20 }); const list = resp.data || []; if (isRefresh) { setSessions(list); } else { setSessions((prev) => [...prev, ...list]); } setTotal(resp.total || 0); setPage(pageNum); } catch { setError('加载失败,请稍后重试'); } finally { setLoading(false); loadingRef.current = false; } }; useDidShow(() => { Taro.setNavigationBarTitle({ title: '在线咨询' }); loadSessions(1, true); }); usePullDownRefresh(() => { loadSessions(1, true).finally(() => { Taro.stopPullDownRefresh(); }); }); useReachBottom(() => { if (!loading && sessions.length < total) { loadSessions(page + 1); } }); const handleTapSession = (session: ConsultationSession) => { Taro.navigateTo({ url: `/pages/consultation/detail/index?id=${session.id}` }); }; return ( {/* 页头 */} 在线咨询 随时随地,连接专业医生 {/* 内容区 */} {loading ? ( ) : error ? ( {error} ) : sessions.length === 0 ? ( 暂无咨询记录 发起咨询后即可在这里与医生交流 ) : ( {sessions.map((session) => { const tag = getStatusTag(session.status); return ( handleTapSession(session)} > {session.subject || '在线咨询'} {tag.label} {session.last_message || '暂无消息'} {session.last_message_at ? formatTime(session.last_message_at) : formatTime(session.created_at)} {session.unread_count_patient > 0 && ( {session.unread_count_patient > 99 ? '99+' : session.unread_count_patient} )} ); })} )} ); }