// ============================================================ // Test2 — 账户信息展示 // 数据来源:AuthAPI.getUserInfo() → GET /api/v1/auth/me // ============================================================ import { useEffect, useState, useCallback } from 'react'; import { Card, Descriptions, Button, Typography, Skeleton, Result, Space, Tag, Divider, theme as antTheme, } from 'antd'; import { UserOutlined, WalletOutlined, CrownOutlined, BarChartOutlined, RightOutlined, } from '@ant-design/icons'; import { AuthAPI, type UserInfoBody } from '@/services/modules/auth'; const { Text, Title } = Typography; /** 将分转为元,保留两位小数 */ function centToYuan(cent: number): string { return (cent / 100).toFixed(2); } /** 格式化日期 */ function formatDate(date: Date | string | undefined): string { if (!date) return '—'; const d = typeof date === 'string' ? new Date(date) : date; return d.toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }); } export function Test2() { const { token } = antTheme.useToken(); const [userInfo, setUserInfo] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const load = useCallback(async () => { setLoading(true); setError(null); try { const info = await AuthAPI.getUserInfo(); setUserInfo(info); } catch (err) { setError((err as Error).message || '加载账户信息失败'); } finally { setLoading(false); } }, []); useEffect(() => { void load(); }, [load]); // ======== 加载态 ======== if (loading) { return (
); } // ======== 错误态 ======== if (error || !userInfo) { return ( 重试 } /> ); } // ======== 数据展示 ======== const totalBalance = userInfo.balance_cent + userInfo.gift_balance_cent; return (
{/* ======== 标题:账户 ======== */}
{userInfo.display_name || userInfo.username} ID: {userInfo.id}
{userInfo.role !== 'user' && ( {userInfo.role === 'admin' ? '管理员' : '超级管理员'} )}
{/* ======== 积分栏 ======== */} 账户总积分 {centToYuan(totalBalance)} <Text type="secondary" style={{ fontSize: 14, marginLeft: 4 }}>积分</Text>
主积分
{centToYuan(userInfo.balance_cent)}
赠送积分
{centToYuan(userInfo.gift_balance_cent)}
{userInfo.discount_rate > 0 && userInfo.discount_rate < 1 && (
折扣率
{Math.round(userInfo.discount_rate * 100)}%
)}
{/* ======== 套餐信息 ======== */}
当前套餐
{userInfo.subscription_plan || '免费版'} {formatDate(userInfo.subscription_expires_at)} {userInfo.seat_limit || '—'}
{/* ======== 今日统计 ======== */} 今日使用 统计数据将在次日更新 {/* ======== 查看更多权益 ======== */}
); }