import { View, Text } from '@tarojs/components'; import Taro from '@tarojs/taro'; import { safeNavigateTo } from '@/utils/navigate'; import { useState, useCallback } from 'react'; import { useAuthStore } from '../../stores/auth'; import { usePointsStore } from '../../stores/points'; import { useUIStore } from '../../stores/ui'; import { navigateToLogin } from '../../utils/navigate'; import { usePageData } from '@/hooks/usePageData'; import { notificationService } from '@/services/notification'; import Loading from '../../components/Loading'; import PageShell from '@/components/ui/PageShell'; import ContentCard from '@/components/ui/ContentCard'; import './index.scss'; interface MenuItem { label: string; icon: string; bg: string; color: string; path: string; } interface MenuGroup { title: string; items: MenuItem[]; } const LOGGED_IN_GROUPS: MenuGroup[] = [ { title: '健康档案', items: [ { label: '健康档案', icon: '健', bg: 'pri-l', color: 'pri', path: '/pages/pkg-profile/health-records/index' }, { label: '我的报告', icon: '报', bg: 'acc-l', color: 'acc', path: '/pages/pkg-profile/reports/index' }, ], }, { title: '账号', items: [ { label: '设备同步', icon: '设', bg: 'surface-alt', color: 'tx3', path: '/pages/pkg-health/device-sync/index' }, { label: '设置', icon: '齿', bg: 'surface-alt', color: 'tx3', path: '/pages/pkg-profile/settings/index' }, ], }, ]; const GUEST_GROUPS: MenuGroup[] = [ { title: '设置', items: [ { label: '设置', icon: '齿', bg: 'surface-alt', color: 'tx3', path: '/pages/pkg-profile/settings/index' }, ], }, ]; export default function Profile() { const user = useAuthStore((s) => s.user); const logout = useAuthStore((s) => s.logout); const pointsAccount = usePointsStore((s) => s.account); const checkinInfo = usePointsStore((s) => s.checkinStatus); const refreshPoints = usePointsStore((s) => s.refresh); const mode = useUIStore((s) => s.mode); const modeClass = mode === 'elder' ? 'elder-mode' : ''; const isGuest = !user; const groups = isGuest ? GUEST_GROUPS : LOGGED_IN_GROUPS; const [pointsLoading, setPointsLoading] = useState(false); const [unreadCount, setUnreadCount] = useState(0); const fetchPoints = useCallback(async () => { if (!isGuest) { setPointsLoading(true); await refreshPoints(); setPointsLoading(false); try { const res = await notificationService.getUnreadCount(); const count = (res as { data?: { count?: number } })?.data?.count ?? 0; setUnreadCount(count); } catch { /* ignore */ } } }, [isGuest, refreshPoints]); usePageData(fetchPoints, { throttleMs: 5000 }); const handleMenuClick = (item: MenuItem) => { safeNavigateTo(item.path); }; const handleLogout = () => { Taro.showModal({ title: '退出登录', content: '确定要退出登录吗?', }).then((res) => { if (res.confirm) { logout(); } }); }; const displayName = user?.display_name || user?.username || (user?.phone ? `${user.phone.slice(-4)}` : '') || '用户'; const displayInitial = (user?.display_name || user?.username || '用').charAt(0); return ( {/* 用户信息卡片 */} {isGuest ? ( ? 未登录 点击登录,开启健康管理之旅 ) : ( <> {displayInitial} {displayName} {user?.phone ? `${user.phone.slice(0, 3)}****${user.phone.slice(-4)}` : ''} {/* 积分 + 打卡 */} {pointsLoading ? ( ) : ( {(pointsAccount?.balance ?? 0).toLocaleString()} 健康积分 {checkinInfo?.consecutive_days ?? 0} 连续打卡 )} )} {/* 消息通知入口 */} {!isGuest && ( safeNavigateTo('/pages/pkg-profile/notifications/index')} > 消息通知 {unreadCount > 0 && ( {unreadCount > 99 ? '99+' : unreadCount} )} )} {/* 分组菜单 */} {groups.map((group) => ( {group.title} {group.items.map((item, idx) => ( handleMenuClick(item)} > {item.icon} {item.label} {idx < group.items.length - 1 && } ))} ))} {/* 退出登录 / 登录 */} {isGuest ? ( 登录账号 ) : ( 退出登录 )} ); }