feat: 参考原型图 ui优化提交 企业端-企业中心 相关的页面

This commit is contained in:
zhanjunhao
2026-11-06 20:19:43 +08:00
parent bc6af74e9e
commit d58e468969
147 changed files with 9816 additions and 8768 deletions
@@ -19,30 +19,23 @@ For commercial licensing, please contact support@quantumnous.com
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
Table,
Button,
Select,
Empty,
Spin,
Progress,
Typography,
} from '@/components/ui';
import OrgPanel from './OrgPanel';
Button as AntButton,
Card as AntCard,
Segmented,
Select as AntSelect,
Tabs as AntTabs,
Tag as AntTag,
} from 'antd';
import { Table, Empty, Spin, Progress } from '@/components/ui';
import '../../../styles/org-center.css';
import { VChart } from '@visactor/react-vchart';
import { initVChartTheme } from '@/components/ui/chartTheme';
import {
RefreshCw,
PieChart,
TrendingUp,
BarChart3,
Crown,
} from 'lucide-react';
import { RefreshCw } from 'lucide-react';
import { API } from '../../../helpers/api';
import { showError } from '../../../helpers';
import { renderQuota } from '../../../helpers/render';
import { useActualTheme } from '../../../context/Theme';
const { Text } = Typography;
const getOrgHeaders = (orgId) => ({ 'X-Org-Id': String(orgId) });
/* 系列色运行时读取品牌 token(--brand-*),随主题预设整套切换。
@@ -95,37 +88,20 @@ const chartGrid = (tokens) => ({
style: { stroke: tokens.grid, lineDash: [4, 4] },
});
// 环比徽标:本期 vs 上期。增长绿、下降红;无上期数据时灰显。
// 状态色统一走 Semi 语义 token(与页面各处使用进度条同一套红绿,且随明暗主题自适应)。
// 环比徽标:本期 vs 上期。增长绿、下降红;无上期数据时灰显(语义色沿用原实现)。
const DeltaBadge = ({ cur, prev, t }) => {
if (!prev || prev <= 0) {
return <span className='text-xs text-gray-400'>{t('无上期对比')}</span>;
return <small>{t('无上期对比')}</small>;
}
const pct = ((cur - prev) / prev) * 100;
const up = pct >= 0;
return (
<span
className='text-xs font-medium'
style={{
color: up ? 'var(--ui-color-success)' : 'var(--ui-color-danger)',
}}
>
{up ? '▲' : '▼'} {Math.abs(pct).toFixed(1)}%
<span className='text-gray-400 ml-1'>{t('较上期')}</span>
</span>
<small className={up ? 'is-up' : 'is-down'}>
{up ? '↑' : '↓'} {Math.abs(pct).toFixed(1)}%<em>{t('较上期')}</em>
</small>
);
};
const KpiCard = ({ label, value, sub }) => (
<div className='px-4 py-3 rounded-xl bg-[var(--ui-color-fill-0)]'>
<div className='text-xs text-gray-500 mb-1'>{label}</div>
<div className='text-lg font-semibold text-gray-900 dark:text-white truncate'>
{value}
</div>
{sub ? <div className='mt-0.5'>{sub}</div> : null}
</div>
);
// 把 time 维度的小时行聚合成趋势序列:>7 天按天汇总,否则按小时。
const buildTrend = (rows, rangeDays, field) => {
if (!rows || rows.length === 0) return [];
@@ -156,20 +132,28 @@ const localizeAnalyticsRows = (rows, dimension, t) =>
department: t('未分配部门'),
project: t('未绑定项目'),
member: t('未归属成员'),
token: t('未绑定令牌'),
token: t('未绑定密钥'),
}[dimension];
if (zeroName) name = zeroName;
} else if (dimension === 'member' && name === `成员#${row.key}`) {
name = `${t('成员')}#${row.key}`;
} else if (dimension === 'token' && name === `令牌#${row.key}`) {
name = `${t('令牌')}#${row.key}`;
} else if (dimension === 'token' && name === `\u4ee4\u724c#${row.key}`) {
name = `${t('密钥')}#${row.key}`;
}
return name === row.name ? row : { ...row, name };
});
// 多维用量分析面板(§5.6):经营看板视角——KPI+环比、消耗趋势折线、维度分布柱状、明细表。
// 仅 admin + analytics 特性开启时挂载(前端门控),后端接口仍 admin+feature 鉴权。
const OrgAnalyticsPanel = ({ orgId }) => {
// memberUsage:数据看板传入的“成员用量明细”卡,作为明细页签之一(原型 .ea-detail-tabs);
// detailTab / onDetailTabChange:明细页签受控(数据看板“成员用量”按钮切换并定位)。
const OrgAnalyticsPanel = ({
orgId,
memberUsage = null,
detailTab,
onDetailTabChange,
detailsRef,
}) => {
const { t } = useTranslation();
const actualTheme = useActualTheme();
const isDarkMode = actualTheme === 'dark';
@@ -189,6 +173,11 @@ const OrgAnalyticsPanel = ({ orgId }) => {
const [trendRows, setTrendRows] = useState([]);
const [prevRows, setPrevRows] = useState([]);
const [loading, setLoading] = useState(false);
const [detailPage, setDetailPage] = useState(1);
useEffect(() => {
setDetailPage(1);
}, [breakdownRows, dimension, rangeDays]);
useEffect(() => {
initVChartTheme({ isWatchingThemeSwitch: true });
@@ -200,7 +189,7 @@ const OrgAnalyticsPanel = ({ orgId }) => {
{ label: t('项目'), value: 'project' },
{ label: t('部门'), value: 'department' },
{ label: t('成员'), value: 'member' },
{ label: t('令牌'), value: 'token' },
{ label: t('密钥'), value: 'token' },
{ label: t('时间'), value: 'time' },
],
[t],
@@ -298,7 +287,11 @@ const OrgAnalyticsPanel = ({ orgId }) => {
data: [{ id: 'trend', values: trendData }],
xField: 'date',
yField: 'value',
point: { visible: trendData.length <= 60, size: 3 },
point: {
visible: trendData.length <= 60,
size: 3,
style: { fill: brandColors.brand },
},
line: { style: { lineWidth: 2, stroke: brandColors.brand } },
area: {
style: {
@@ -375,7 +368,7 @@ const OrgAnalyticsPanel = ({ orgId }) => {
direction: 'horizontal',
xField: 'value',
yField: 'name',
color: [brandColors.brand2],
color: [brandColors.brand],
barMaxWidth: 22,
label: {
visible: true,
@@ -428,33 +421,52 @@ const OrgAnalyticsPanel = ({ orgId }) => {
);
const columns = useMemo(
() => [
{ title: dimLabel, dataIndex: 'name', width: 200 },
{
title: t('消耗占比'),
title: dimLabel,
dataIndex: 'name',
width: 228,
render: (v) => <span className='oc-ea-model-name'>{v}</span>,
},
{
title: t('消费占比'),
width: 300,
render: (_, r) => {
const pct =
breakdownTotal > 0 ? ((r.quota || 0) / breakdownTotal) * 100 : 0;
return (
<div className='flex items-center gap-2'>
<div className='oc-ea-cost-share'>
<div>
<strong>{renderQuota(r.quota || 0, 2)}</strong>
<span>{pct.toFixed(1)}%</span>
</div>
<Progress
percent={Math.round(pct)}
style={{ flex: 1 }}
stroke={brandColors.brand2}
size='small'
stroke={brandColors.brand}
showInfo={false}
/>
<span className='text-xs text-gray-500 whitespace-nowrap'>
{renderQuota(r.quota || 0, 2)} ({pct.toFixed(1)}%)
</span>
</div>
);
},
},
{ title: t('请求数'), dataIndex: 'request_count', width: 100 },
{
title: t('Token (入/出)'),
width: 160,
render: (_, r) =>
`${r.prompt_tokens || 0} / ${r.completion_tokens || 0}`,
title: t('请求数'),
dataIndex: 'request_count',
width: 110,
align: 'right',
render: (v) => Number(v || 0).toLocaleString(),
},
{
title: t('Token(入 / 出)'),
width: 240,
align: 'right',
render: (_, r) => (
<span className='oc-ea-token-pair'>
{Number(r.prompt_tokens || 0).toLocaleString()}
<small> / </small>
{Number(r.completion_tokens || 0).toLocaleString()}
</span>
),
},
],
[dimLabel, breakdownTotal, t, brandColors],
@@ -466,180 +478,215 @@ const OrgAnalyticsPanel = ({ orgId }) => {
{ label: t('近 90 天'), value: 90 },
];
const [innerTab, setInnerTab] = useState('models');
const activeDetail = detailTab ?? innerTab;
const changeDetail = (key) => {
setInnerTab(key);
onDetailTabChange?.(key);
};
const detailCard = (
<AntCard className='oc-ea-detail-card'>
<div className='oc-ea-table-toolbar'>
<h2>{t('{{dim}}明细', { dim: dimLabel })}</h2>
</div>
{loading ? (
<div className='oc-ea-loading'>
<Spin size='large' />
</div>
) : breakdownRows.length === 0 ? (
<Empty
description={t('该时间范围暂无用量数据')}
style={{ padding: 40 }}
/>
) : (
<Table
columns={columns}
dataSource={breakdownRows}
rowKey='key'
scroll={{ x: 880 }}
pagination={{
currentPage: Math.min(
detailPage,
Math.max(1, Math.ceil(breakdownRows.length / 10)),
),
pageSize: 10,
showSizeChanger: false,
onPageChange: setDetailPage,
showTotal: (n) => t('共 {{total}} 条', { total: n }),
}}
/>
)}
</AntCard>
);
return (
<div className='space-y-4'>
{/* 控制区 */}
<OrgPanel>
<div className='flex flex-wrap items-center justify-between gap-3'>
<div className='flex items-center gap-2'>
<PieChart size={18} className='text-brand-deep' />
<Text strong>{t('企业用量分析')}</Text>
<Text type='tertiary' size='small'>
{t('趋势 · 结构 · 与消耗明细一致')}
</Text>
<>
{/* 多维用量分析(原型 .ea-analysis):标题 + 维度 / 时间 / 刷新 */}
<section className='oc-ea-analysis'>
<div className='oc-ea-analysis-heading'>
<div>
<h2>{t('多维用量分析')}</h2>
<p>{t('趋势、结构与调用明细')}</p>
</div>
<div className='flex items-center gap-2'>
<Select
<div className='oc-ea-filters'>
<AntSelect
className='oc-ea-dimension-select'
aria-label={t('维度')}
value={dimension}
onChange={setDimension}
optionList={dimensions}
style={{ width: 110 }}
options={dimensions}
/>
<Select
<AntSelect
className='oc-ea-range-select'
aria-label={t('时间范围')}
value={rangeDays}
onChange={setRangeDays}
optionList={rangeOptions}
style={{ width: 110 }}
options={rangeOptions}
/>
<Button
theme='light'
icon={<RefreshCw size={14} />}
<AntButton
className='oc-ea-refresh'
aria-label={t('刷新分析')}
icon={<RefreshCw size={16} />}
loading={loading}
onClick={() => fetchAll(true)}
>
{t('刷新')}
</Button>
</div>
</div>
{/* KPI + 环比 */}
<div className='grid grid-cols-2 lg:grid-cols-4 gap-4 mt-4'>
<KpiCard
label={t('本期总消耗')}
value={renderQuota(curQuota, 2)}
sub={<DeltaBadge cur={curQuota} prev={prevQuota} t={t} />}
/>
<KpiCard
label={t('本期总请求')}
value={curReq.toLocaleString()}
sub={<DeltaBadge cur={curReq} prev={prevReq} t={t} />}
/>
<KpiCard
label={t('日均消耗')}
value={renderQuota(rangeDays > 0 ? curQuota / rangeDays : 0, 2)}
sub={
<span className='text-xs text-gray-400'>
{t('近 {{n}} 天均值', { n: rangeDays })}
</span>
}
/>
<KpiCard
label={t('消耗最高')}
value={topRow ? topRow.name : '-'}
sub={
topRow ? (
<span className='text-xs text-amber-500 inline-flex items-center gap-1'>
<Crown size={11} /> {renderQuota(topRow.quota || 0, 2)}
</span>
) : (
<span className='text-xs text-gray-400'>
{dimension === 'time' ? t('按时间维度无排名') : '-'}
</span>
)
}
/>
</div>
</OrgPanel>
{/* 消耗趋势折线 */}
<OrgPanel>
<div className='flex items-center justify-between mb-3'>
<div className='flex items-center gap-2'>
<TrendingUp size={16} className='text-brand-deep' />
<Text strong>{t('用量趋势')}</Text>
</div>
<Select
value={trendMetric}
onChange={setTrendMetric}
optionList={[
{ label: t('消耗'), value: 'quota' },
{ label: t('请求数'), value: 'requests' },
]}
style={{ width: 100 }}
size='small'
/>
</div>
{loading ? (
<div className='flex justify-center py-12'>
<Spin size='large' />
</div>
) : trendSpec ? (
<div className='rounded-2xl border border-gray-100/70 dark:border-white/5 bg-white/60 dark:bg-slate-950/25 p-2'>
<VChart
key={`trend-${actualTheme}`}
spec={trendSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: 280 }}
/>
</div>
) : (
<Empty
description={t('该时间范围暂无用量数据')}
style={{ padding: 40 }}
/>
)}
</OrgPanel>
</div>
{/* 维度分布柱状(time 维度不展示,趋势图已覆盖) */}
{dimension !== 'time' && (
<OrgPanel>
<div className='flex items-center gap-2 mb-3'>
<BarChart3 size={16} className='text-brand-deep' />
<Text strong>{t('{{dim}}消耗分布', { dim: dimLabel })}</Text>
<Text type='tertiary' size='small'>
{t('Top 10')}
</Text>
{/* 汇总指标(原型 .ea-summary) */}
<div className='oc-ea-summary'>
<div>
<span>{t('本期总消费')}</span>
<strong>{renderQuota(curQuota, 2)}</strong>
<DeltaBadge cur={curQuota} prev={prevQuota} t={t} />
</div>
{loading ? (
<div className='flex justify-center py-12'>
<Spin size='large' />
</div>
) : barSpec ? (
<div className='rounded-2xl border border-gray-100/70 dark:border-white/5 bg-white/60 dark:bg-slate-950/25 p-2'>
<VChart
key={`bar-${actualTheme}`}
spec={barSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: Math.max(220, barData.length * 34 + 60) }}
<div>
<span>{t('本期总请求')}</span>
<strong>{curReq.toLocaleString()}</strong>
<DeltaBadge cur={curReq} prev={prevReq} t={t} />
</div>
<div>
<span>{t('日均消费')}</span>
<strong>
{renderQuota(rangeDays > 0 ? curQuota / rangeDays : 0, 2)}
</strong>
<small>{t('近 {{n}} 天均值', { n: rangeDays })}</small>
</div>
<div>
<span>{t('消费最高')}</span>
<strong className='oc-ea-top'>{topRow ? topRow.name : '-'}</strong>
<small>
{topRow
? renderQuota(topRow.quota || 0, 2)
: dimension === 'time'
? t('按时间维度无排名')
: '-'}
</small>
</div>
</div>
<div
className={`oc-ea-charts${dimension === 'time' ? ' is-single' : ''}`}
>
{/* 用量趋势 */}
<AntCard className='oc-ea-trend'>
<div className='oc-ea-panel-heading'>
<div>
<h2>{t('用量趋势')}</h2>
<span>
{rangeDays > 7
? t('按天查看企业调用消耗')
: t('逐小时查看企业调用消耗')}
</span>
</div>
<Segmented
size='small'
value={trendMetric}
onChange={setTrendMetric}
options={[
{ label: t('消费'), value: 'quota' },
{ label: t('请求数'), value: 'requests' },
]}
/>
</div>
) : (
<Empty
description={t('该时间范围暂无用量数据')}
style={{ padding: 40 }}
/>
)}
</OrgPanel>
)}
{loading ? (
<div className='oc-ea-loading'>
<Spin size='large' />
</div>
) : trendSpec ? (
<VChart
key={`trend-${actualTheme}`}
spec={trendSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: 306 }}
/>
) : (
<Empty
description={t('该时间范围暂无用量数据')}
style={{ padding: 40 }}
/>
)}
</AntCard>
{/* 明细表 */}
<OrgPanel>
<div className='flex items-center gap-2 mb-3'>
<Text strong>{t('{{dim}}明细', { dim: dimLabel })}</Text>
{/* 维度分布柱状(time 维度不展示,趋势图已覆盖) */}
{dimension !== 'time' && (
<AntCard className='oc-ea-rank'>
<div className='oc-ea-panel-heading'>
<div>
<h2>{t('{{dim}}消费分布', { dim: dimLabel })}</h2>
<span>{t('Top 10')}</span>
</div>
<AntTag bordered={false}>{t('按消费排序')}</AntTag>
</div>
{loading ? (
<div className='oc-ea-loading'>
<Spin size='large' />
</div>
) : barSpec ? (
<VChart
key={`bar-${actualTheme}`}
spec={barSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: Math.max(306, barData.length * 34 + 60) }}
/>
) : (
<Empty
description={t('该时间范围暂无用量数据')}
style={{ padding: 40 }}
/>
)}
</AntCard>
)}
</div>
{loading ? (
<div className='flex justify-center py-12'>
<Spin size='large' />
</div>
) : breakdownRows.length === 0 ? (
<Empty
description={t('该时间范围暂无用量数据')}
style={{ padding: 40 }}
/>
) : (
<Table
columns={columns}
dataSource={breakdownRows}
rowKey='key'
pagination={
breakdownRows.length > 20
? { pageSize: 20, showSizeChanger: false }
: false
}
/>
)}
</OrgPanel>
</div>
</section>
{/* 明细(原型 .ea-details:维度明细 / 成员用量 页签) */}
<section className='oc-ea-details' ref={detailsRef}>
<AntTabs
className='oc-ea-detail-tabs'
activeKey={memberUsage ? activeDetail : 'models'}
onChange={changeDetail}
items={[
{
key: 'models',
label: t('{{dim}}明细', { dim: dimLabel }),
children: detailCard,
},
...(memberUsage
? [
{
key: 'members',
label: t('成员用量'),
forceRender: true,
children: memberUsage,
},
]
: []),
]}
/>
</section>
</>
);
};