Files
gofly-platform-services/zlt-web/react-web-next/src/pages/Welcome.tsx
T

255 lines
11 KiB
TypeScript
Raw Normal View History

2025-10-01 03:51:53 +08:00
import React, { useEffect, useRef, useState } from 'react';
import { Card, Space, Typography, Row, Col, Statistic, Segmented, Button, message } from 'antd';
import { ShoppingCartOutlined, PayCircleOutlined, ReloadOutlined } from '@ant-design/icons';
import { Line, Pie } from '@ant-design/plots';
import TenantScopeFilter from '@/components/TenantScopeFilter';
2025-10-01 03:51:53 +08:00
import { dashboardApi } from '@/services/dashboard';
2025-10-01 03:51:53 +08:00
const { Title, Text } = Typography;
const Welcome: React.FC = () => {
2025-10-01 03:51:53 +08:00
const [tenantId, setTenantId] = useState<string>();
const [includeChildren, setIncludeChildren] = useState<boolean>(true);
const [statType, setStatType] = useState<'day' | 'month' | 'year'>('day');
const [loading, setLoading] = useState<boolean>(false);
const [pageLoading, setPageLoading] = useState<boolean>(true);
const firstLoadRef = useRef(true);
const [stat, setStat] = useState<any>({ orderStats: {}, chartStats: {} });
2025-10-01 03:51:53 +08:00
const load = async () => {
if (firstLoadRef.current) setPageLoading(true);
setLoading(true);
try {
const res: any = await dashboardApi.statistics({ tenantId, includeChildren, statType });
setStat(res?.data || res);
} catch (e) {
message.error('加载统计失败');
} finally {
setLoading(false);
if (firstLoadRef.current) {
firstLoadRef.current = false;
setPageLoading(false);
}
}
};
useEffect(() => {
2025-10-01 03:51:53 +08:00
if (!tenantId) return;
load();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [tenantId, includeChildren, statType]);
2025-10-01 03:51:53 +08:00
const orderStats = stat?.orderStats || {};
const chartStats = stat?.chartStats || {};
2025-10-09 14:33:15 +08:00
// 每日趋势:订单数/订单金额(元)/退款订单数/退款金额(元)
const trendData = chartStats?.dailySummary || [];
2025-10-01 03:51:53 +08:00
const distributionData = chartStats?.orderDistribution || [];
2025-10-09 14:33:15 +08:00
const trendColors: Record<string, string> = {
'订单数': '#1668DC',
'订单金额(元)': '#FA8C16',
'退款订单数': '#A0D911',
'退款金额(元)': '#F5222D',
};
2025-10-01 03:51:53 +08:00
const trendConfig: any = {
data: trendData,
xField: 'date',
yField: 'value',
seriesField: 'type',
smooth: true,
2025-10-09 14:33:15 +08:00
height: 320,
2025-10-01 03:51:53 +08:00
legend: { position: 'top' },
2025-10-09 14:33:15 +08:00
color: ({ type }: any) => trendColors[type] || '#5B8FF9',
lineStyle: { lineWidth: 2 },
point: { size: 2, shape: 'circle' },
tooltip: {
shared: true,
customContent: (title: string, items: any[]) => {
if (!items || !items.length) return '';
const rows = items
.map((it) => {
const name = it?.name || it?.data?.type;
const v = it?.data?.value ?? 0;
const isAmount = name && String(name).includes('金额');
const text = isAmount ? `¥${Number(v).toFixed(2)}` : `${Math.round(Number(v))}`;
const color = it?.color || '#333';
return `<div style="display:flex;align-items:center;gap:8px;">
<span style="display:inline-block;width:8px;height:8px;border-radius:50%;background:${color}"></span>
<span>${name}</span>
<span style="margin-left:auto;font-weight:600;">${text}</span>
</div>`;
})
.join('');
return `<div style="padding:8px 8px 4px 8px;min-width:200px;">
<div style="margin-bottom:6px;font-weight:600;">${title}</div>
${rows}
</div>`;
},
},
2025-10-01 03:51:53 +08:00
animation: false,
};
2025-10-09 14:33:15 +08:00
const distributionPalette = [
'#1668DC', '#5AD8A6', '#F6BD16', '#E8684A', '#6DC8EC', '#9270CA', '#FF9D4D', '#269A99', '#FF99C3', '#1D9ED1',
];
2025-10-01 03:51:53 +08:00
const distributionConfig: any = {
data: distributionData,
angleField: 'value',
colorField: 'type',
radius: 0.9,
2025-10-09 14:33:15 +08:00
color: distributionPalette,
legend: { position: 'right' },
label: {
type: 'spider',
content: (data: any) => {
const total = (distributionData || []).reduce((s: number, d: any) => s + (Number(d.value) || 0), 0);
const pct = total > 0 ? `${((Number(data.value) / total) * 100).toFixed(1)}%` : '0%';
return `${data.type} ${pct} (${data.value})`;
},
},
2025-10-01 03:51:53 +08:00
animation: false,
};
return (
<div className="page-enter" style={{ padding: 24 }}>
2025-10-09 14:33:15 +08:00
{/* 顶部工具区 */}
2025-10-01 03:51:53 +08:00
<Card className="modern-card" style={{ marginBottom: 16 }}>
<Row align="middle" justify="space-between" gutter={[12, 12]}>
<Col flex="none">
<Title level={4} style={{ margin: 0 }}>首页统计</Title>
</Col>
2025-10-01 03:51:53 +08:00
<Col flex="auto">
<Space style={{ width: '100%', justifyContent: 'flex-end', flexWrap: 'wrap' }} size={12}>
<TenantScopeFilter autoApply onChanged={(tid, inc) => { setTenantId(tid); setIncludeChildren(!!inc); }} />
<Space>
<span>统计粒度</span>
<Segmented
options={[
2025-10-01 05:05:23 +08:00
{ label: '周', value: 'day' },
2025-10-01 03:51:53 +08:00
{ label: '月', value: 'month' },
{ label: '年', value: 'year' },
]}
value={statType}
onChange={(val) => setStatType(val as any)}
/>
</Space>
<Button type="primary" icon={<ReloadOutlined />} loading={loading && !pageLoading} onClick={load}>刷新</Button>
</Space>
</Col>
</Row>
</Card>
2025-10-09 14:33:15 +08:00
{/* 第一行:总订单概览 / 收入总览(两侧对称,各3项) */}
2025-10-01 03:51:53 +08:00
<Row gutter={[16, 16]}>
<Col xs={24} md={12}>
2025-10-09 14:33:15 +08:00
<Card loading={pageLoading} style={{ height: '100%' }}>
2025-10-01 03:51:53 +08:00
<Space direction="vertical" style={{ width: '100%' }} size={12}>
<Space align="center" size={8}>
<ShoppingCartOutlined />
2025-10-09 14:33:15 +08:00
<Text strong>总订单概览</Text>
2025-10-01 03:51:53 +08:00
</Space>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
<Col xs={12} sm={10} md={10}><Text type="secondary">总订单数</Text></Col>
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={orderStats.totalOrders || 0} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
2025-10-01 03:51:53 +08:00
</Row>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
<Col xs={12} sm={10} md={10}><Text type="secondary">退款订单数</Text></Col>
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={orderStats.refundedOrders || 0} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
</Row>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
2025-10-09 14:33:15 +08:00
<Col xs={12} sm={10} md={10}><Text type="secondary">净订单数</Text></Col>
2025-10-01 03:51:53 +08:00
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
2025-10-09 14:33:15 +08:00
<Statistic value={(orderStats.totalOrders || 0) - (orderStats.refundedOrders || 0)} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
2025-10-01 03:51:53 +08:00
</Col>
</Row>
</Space>
</Card>
</Col>
<Col xs={24} md={12}>
2025-10-09 14:33:15 +08:00
<Card loading={pageLoading} style={{ height: '100%' }}>
2025-10-01 03:51:53 +08:00
<Space direction="vertical" style={{ width: '100%' }} size={12}>
<Space align="center" size={8}>
<PayCircleOutlined />
2025-10-09 14:33:15 +08:00
<Text strong>收入总览</Text>
2025-10-01 03:51:53 +08:00
</Space>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
2025-10-09 14:33:15 +08:00
<Col xs={12} sm={10} md={10}><Text type="secondary">订单总金额</Text></Col>
2025-10-01 03:51:53 +08:00
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={(orderStats.totalAmount || 0) / 100} precision={2} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
</Row>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
2025-10-09 14:33:15 +08:00
<Col xs={12} sm={10} md={10}><Text type="secondary">退款订单总金额</Text></Col>
2025-10-01 03:51:53 +08:00
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={(orderStats.refundAmount || 0) / 100} precision={2} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
</Row>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
<Col xs={12} sm={10} md={10}><Text type="secondary">净收入(元)</Text></Col>
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={(orderStats.netIncome || 0) / 100} precision={2} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
</Row>
</Space>
</Card>
</Col>
</Row>
2025-10-09 14:33:15 +08:00
{/* 第二行:今日统计 / 状态分布(两侧对称) */}
2025-10-01 03:51:53 +08:00
<Row gutter={[16, 16]} style={{ marginTop: 16 }}>
2025-10-09 14:33:15 +08:00
<Col xs={24} md={12}>
<Card loading={pageLoading} style={{ height: '100%' }}>
<Space direction="vertical" style={{ width: '100%' }} size={12}>
<Space align="center" size={8}>
<PayCircleOutlined />
<Text strong>今日统计</Text>
</Space>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
<Col xs={12} sm={10} md={10}><Text type="secondary">今日订单数</Text></Col>
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={orderStats.todayOrders || 0} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
</Row>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
<Col xs={12} sm={10} md={10}><Text type="secondary">今日退款订单数</Text></Col>
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={orderStats.todayRefundedOrders || 0} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
</Row>
<Row gutter={8} align="middle" style={{ width: '100%' }}>
<Col xs={12} sm={10} md={10}><Text type="secondary">今日收款订单数</Text></Col>
<Col xs={12} sm={14} md={14} style={{ textAlign: 'right' }}>
<Statistic value={orderStats.todayPaidOrders || 0} valueStyle={{ fontWeight: 600, whiteSpace: 'nowrap' }} groupSeparator="," />
</Col>
</Row>
</Space>
2025-10-01 03:51:53 +08:00
</Card>
</Col>
2025-10-09 14:33:15 +08:00
<Col xs={24} md={12}>
<Card title="订单状态分布" loading={pageLoading} style={{ height: '100%' }}>
2025-10-01 03:51:53 +08:00
<Pie {...distributionConfig} />
</Card>
</Col>
</Row>
2025-10-09 14:33:15 +08:00
{/* 第三行:每日趋势(满宽) */}
<Row gutter={[16, 16]} style={{ marginTop: 16 }}>
<Col span={24}>
<Card title="每日趋势:订单数 / 订单金额 / 退款" loading={pageLoading}>
<Line {...trendConfig} />
</Card>
</Col>
</Row>
</div>
);
};
2025-09-29 19:29:41 +08:00
export default Welcome;
2025-10-09 14:33:15 +08:00