/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useState, useEffect, useCallback, useRef } from 'react'; import { Empty as AntEmpty, Tag as AntTag } from 'antd'; import { Modal, Table, Typography, Pagination, Toast } from '@/components/ui'; import i18next from 'i18next'; import { API, renderQuota } from '../../helpers'; import { formatCurrency } from '../../helpers/safeDisplay'; const { Text } = Typography; // 会员状态配置(Ant Design 预设标签色) const STATUS_CONFIG = { active: { label: '生效中', color: 'success' }, expired: { label: '已过期', color: undefined }, pending: { label: '待生效', color: 'warning' }, canceled: { label: '已取消', color: 'error' }, }; // 方案类型映射 const PLAN_TYPE_MAP = { weekly: '周卡', monthly: '月卡', quarterly: '季卡', }; // 档位映射 const TIER_MAP = { lite: '轻度版', pro: '重度版', }; /** * 会员购买记录内容:数据加载、统计、表格与分页。 * 弹窗与在线充值页“会员购买记录”页签共用;visible 为 true 时按原逻辑加载。 */ export const MembershipHistoryContent = ({ visible, t, inline = false }) => { const [loading, setLoading] = useState(false); const [memberships, setMemberships] = useState([]); const [total, setTotal] = useState(0); const [activeCount, setActiveCount] = useState(0); const [pendingCount, setPendingCount] = useState(0); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(10); // 格式化时间戳为日期字符串(使用 useCallback 避免每次渲染都创建新函数) const formatDate = useCallback((timestamp) => { if (!timestamp) return '-'; return new Date(timestamp * 1000).toLocaleDateString(i18next.language, { year: 'numeric', month: '2-digit', day: '2-digit', }); }, []); // 格式化时间戳为完整时间字符串 const formatDateTime = useCallback((timestamp) => { if (!timestamp) return '-'; return new Date(timestamp * 1000).toLocaleString(i18next.language, { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', }); }, []); // 计算会员状态 const getMembershipStatus = (membership) => { const now = Math.floor(Date.now() / 1000); if (membership.status === 'canceled') return 'canceled'; if (membership.status === 'expired') return 'expired'; if (membership.start_date > now && membership.status === 'active') return 'pending'; if (membership.end_date > now && membership.status === 'active') return 'active'; return 'expired'; }; // 加载会员购买历史 const loadMembershipHistory = async (currentPage, currentPageSize) => { setLoading(true); try { const res = await API.get( `/api/user/membership/history?page=${currentPage}&page_size=${currentPageSize}`, ); const { success, data, message } = res.data; if (success) { setMemberships(data.list || []); setTotal(data.total || 0); setActiveCount(data.active_count || 0); setPendingCount(data.pending_count || 0); } else { console.error('加载会员历史失败:', message); Toast.error({ content: message || t('加载失败'), duration: 3 }); } } catch (error) { console.error('Load membership history error:', error); Toast.error({ content: t('加载会员购买记录失败,请稍后重试'), duration: 3, }); } finally { setLoading(false); } }; // 追踪上一次 visible 状态,用于检测弹窗刚打开的时机 const prevVisibleRef = useRef(false); useEffect(() => { if (visible && !prevVisibleRef.current) { // 弹窗刚打开:重置到第一页 prevVisibleRef.current = true; if (page !== 1) { setPage(1); // page 变化会再次触发此 effect,届时加载数据 return; } loadMembershipHistory(1, pageSize); return; } if (!visible) { prevVisibleRef.current = false; return; } // 弹窗已打开状态下翻页 / 切换每页条数 loadMembershipHistory(page, pageSize); // eslint-disable-next-line react-hooks/exhaustive-deps }, [visible, page, pageSize]); // 渲染状态标签 const renderStatusTag = (membership) => { const status = getMembershipStatus(membership); const config = STATUS_CONFIG[status] || STATUS_CONFIG.expired; return ( {t(config.label)} ); }; // 表格列定义(使用 useCallback 的格式化函数作为依赖) const columns = [ { title: t('方案名称'), dataIndex: 'plan_name', key: 'plan_name', width: 180, render: (text, record) => (
{text || '-'} {PLAN_TYPE_MAP[record.plan_type] ? t(PLAN_TYPE_MAP[record.plan_type]) : record.plan_type}{' '} · {TIER_MAP[record.tier] ? t(TIER_MAP[record.tier]) : record.tier}
), }, { title: t('每日额度'), dataIndex: 'daily_quota', key: 'daily_quota', width: 120, render: (quota) => renderQuota(quota), }, { title: t('天数'), key: 'duration_days', width: 80, align: 'center', render: (_, record) => { // duration_days 不在 UserMembership 模型中,需从 end_date - start_date 计算 const days = record.end_date && record.start_date ? Math.round((record.end_date - record.start_date) / 86400) : '-'; return t('{{days}} 天', { days }); }, }, { title: t('购买价格'), dataIndex: 'price_paid', key: 'price_paid', width: 100, render: (price) => formatCurrency(price, '¥', 2, '¥0.00'), }, { title: t('生效日期'), dataIndex: 'start_date', key: 'start_date', width: 110, render: (date) => formatDate(date), }, { title: t('到期日期'), dataIndex: 'end_date', key: 'end_date', width: 110, render: (date) => formatDate(date), }, { title: t('状态'), key: 'status', width: 100, render: (_, record) => renderStatusTag(record), }, { title: t('购买时间'), dataIndex: 'created_at', key: 'created_at', width: 140, render: (time) => formatDateTime(time), }, ]; return (
{/* 统计信息 */} {(inline || total > 0) && (
{!inline && ( {t('总购买次数')} {total} )} {t('生效中')} {activeCount} {t('待生效')} {pendingCount}
)} {/* 表格区域 */}
} /> {/* 分页器 */} {total > 0 && (
setPage(p)} onPageSizeChange={(size) => { setPageSize(size); setPage(1); }} size='small' showTotal={(count) => t('共 {{total}} 条', { total: count })} />
)} ); }; const MembershipHistoryModal = ({ visible, onClose, t }) => ( ); export default MembershipHistoryModal;