Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,405 @@
|
||||
/*
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import React, {
|
||||
useState,
|
||||
useEffect,
|
||||
useCallback,
|
||||
useRef,
|
||||
useMemo,
|
||||
} from 'react';
|
||||
import {
|
||||
Modal,
|
||||
Table,
|
||||
Tag,
|
||||
Empty,
|
||||
Typography,
|
||||
Pagination,
|
||||
Toast,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { Crown, Clock, CheckCircle, XCircle, Calendar } from 'lucide-react';
|
||||
import { API, renderQuota } from '../../helpers';
|
||||
import {
|
||||
IllustrationNoResult,
|
||||
IllustrationNoResultDark,
|
||||
} from '../common/illustrations';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
// 会员状态配置
|
||||
const STATUS_CONFIG = {
|
||||
active: {
|
||||
label: '生效中',
|
||||
type: 'success',
|
||||
icon: CheckCircle,
|
||||
color: 'text-emerald-500',
|
||||
},
|
||||
expired: {
|
||||
label: '已过期',
|
||||
type: 'default',
|
||||
icon: XCircle,
|
||||
color: 'text-gray-400',
|
||||
},
|
||||
pending: {
|
||||
label: '待生效',
|
||||
type: 'warning',
|
||||
icon: Clock,
|
||||
color: 'text-amber-500',
|
||||
},
|
||||
canceled: {
|
||||
label: '已取消',
|
||||
type: 'default',
|
||||
icon: XCircle,
|
||||
color: 'text-red-400',
|
||||
},
|
||||
};
|
||||
|
||||
// 方案类型映射
|
||||
const PLAN_TYPE_MAP = {
|
||||
weekly: '周卡',
|
||||
monthly: '月卡',
|
||||
quarterly: '季卡',
|
||||
};
|
||||
|
||||
// 档位映射
|
||||
const TIER_MAP = {
|
||||
lite: '轻度版',
|
||||
pro: '重度版',
|
||||
};
|
||||
|
||||
const MembershipHistoryModal = ({ visible, onClose, t }) => {
|
||||
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('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
});
|
||||
}, []);
|
||||
|
||||
// 格式化时间戳为完整时间字符串
|
||||
const formatDateTime = useCallback((timestamp) => {
|
||||
if (!timestamp) return '-';
|
||||
return new Date(timestamp * 1000).toLocaleString('zh-CN', {
|
||||
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;
|
||||
const IconComponent = config.icon;
|
||||
|
||||
return (
|
||||
<div className={`flex items-center gap-1.5 ${config.color}`}>
|
||||
<IconComponent size={14} />
|
||||
<span className='text-xs font-medium'>{t(config.label)}</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 表格列定义(使用 useCallback 的格式化函数作为依赖)
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
{
|
||||
title: t('方案名称'),
|
||||
dataIndex: 'plan_name',
|
||||
key: 'plan_name',
|
||||
width: 180,
|
||||
render: (text, record) => (
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className='w-8 h-8 rounded-lg bg-gradient-to-br from-brand-from to-brand-to flex items-center justify-center flex-shrink-0'>
|
||||
<Crown size={14} className='text-white' />
|
||||
</div>
|
||||
<div>
|
||||
<Text strong className='text-sm block'>
|
||||
{text || '-'}
|
||||
</Text>
|
||||
<Text className='text-xs text-gray-400'>
|
||||
{PLAN_TYPE_MAP[record.plan_type] || record.plan_type} ·{' '}
|
||||
{TIER_MAP[record.tier] || record.tier}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('每日额度'),
|
||||
dataIndex: 'daily_quota',
|
||||
key: 'daily_quota',
|
||||
width: 120,
|
||||
render: (quota) => (
|
||||
<Text strong className='text-emerald-600 text-sm'>
|
||||
{renderQuota(quota)}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
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 (
|
||||
<Tag color='blue' size='small'>
|
||||
{days}
|
||||
{t('天')}
|
||||
</Tag>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('购买价格'),
|
||||
dataIndex: 'price_paid',
|
||||
key: 'price_paid',
|
||||
width: 100,
|
||||
render: (price) => (
|
||||
<Text type='danger' className='text-sm'>
|
||||
¥{price?.toFixed(2) || '0.00'}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('生效日期'),
|
||||
dataIndex: 'start_date',
|
||||
key: 'start_date',
|
||||
width: 110,
|
||||
render: (date) => (
|
||||
<div className='flex items-center gap-1'>
|
||||
<Calendar size={12} className='text-gray-400' />
|
||||
<Text size='small' type='tertiary'>
|
||||
{formatDate(date)}
|
||||
</Text>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('到期日期'),
|
||||
dataIndex: 'end_date',
|
||||
key: 'end_date',
|
||||
width: 110,
|
||||
render: (date) => (
|
||||
<div className='flex items-center gap-1'>
|
||||
<Calendar size={12} className='text-gray-400' />
|
||||
<Text size='small' type='tertiary'>
|
||||
{formatDate(date)}
|
||||
</Text>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
key: 'status',
|
||||
width: 100,
|
||||
render: (_, record) => renderStatusTag(record),
|
||||
},
|
||||
{
|
||||
title: t('购买时间'),
|
||||
dataIndex: 'created_at',
|
||||
key: 'created_at',
|
||||
width: 140,
|
||||
render: (time) => (
|
||||
<Text size='small' type='tertiary'>
|
||||
{formatDateTime(time)}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
],
|
||||
[t, formatDate, formatDateTime],
|
||||
);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title={
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className='w-8 h-8 rounded-lg bg-gradient-to-br from-brand-from to-brand-to flex items-center justify-center'>
|
||||
<Crown size={16} className='text-white' />
|
||||
</div>
|
||||
<span>{t('会员购买记录')}</span>
|
||||
</div>
|
||||
}
|
||||
visible={visible}
|
||||
onCancel={onClose}
|
||||
footer={null}
|
||||
width={1100}
|
||||
bodyStyle={{ padding: 0, maxHeight: '70vh' }}
|
||||
style={{ top: 40 }}
|
||||
>
|
||||
<div className='flex flex-col h-full'>
|
||||
{/* 统计信息 */}
|
||||
{total > 0 && (
|
||||
<div className='px-6 py-4 bg-gradient-to-r from-brand-from/5 to-brand-to/5 dark:from-brand-from/20 dark:to-brand-to/20 border-b border-gray-200 dark:border-gray-700'>
|
||||
<div className='flex items-center gap-6'>
|
||||
<div className='flex items-center gap-2'>
|
||||
<Text className='text-gray-500 text-sm'>
|
||||
{t('总购买次数')}:
|
||||
</Text>
|
||||
<Text strong className='text-lg text-brand-deep'>
|
||||
{total}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<Text className='text-gray-500 text-sm'>{t('生效中')}:</Text>
|
||||
<Text strong className='text-lg text-emerald-600'>
|
||||
{activeCount}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<Text className='text-gray-500 text-sm'>{t('待生效')}:</Text>
|
||||
<Text strong className='text-lg text-amber-600'>
|
||||
{pendingCount}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 表格区域 */}
|
||||
<div className='flex-1 overflow-auto p-6'>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={memberships}
|
||||
loading={loading}
|
||||
rowKey='id'
|
||||
size='small'
|
||||
pagination={false}
|
||||
empty={
|
||||
<Empty
|
||||
image={
|
||||
<IllustrationNoResult style={{ width: 100, height: 100 }} />
|
||||
}
|
||||
darkModeImage={
|
||||
<IllustrationNoResultDark
|
||||
style={{ width: 100, height: 100 }}
|
||||
/>
|
||||
}
|
||||
description={t('暂无会员购买记录')}
|
||||
style={{ padding: 40 }}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 分页器 */}
|
||||
{total > 0 && (
|
||||
<div className='px-6 py-4 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-neutral-900'>
|
||||
<Pagination
|
||||
total={total}
|
||||
currentPage={page}
|
||||
pageSize={pageSize}
|
||||
showSizeChanger
|
||||
pageSizeOpts={[10, 20, 50]}
|
||||
onPageChange={(p) => setPage(p)}
|
||||
onPageSizeChange={(size) => {
|
||||
setPageSize(size);
|
||||
setPage(1);
|
||||
}}
|
||||
showTotal
|
||||
style={{ justifyContent: 'flex-end' }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
};
|
||||
|
||||
export default MembershipHistoryModal;
|
||||
Reference in New Issue
Block a user