817 lines
26 KiB
React
817 lines
26 KiB
React
/*
|
||
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, {
|
||
useEffect,
|
||
useMemo,
|
||
useState,
|
||
useCallback,
|
||
useContext,
|
||
useRef,
|
||
} from 'react';
|
||
import { timestamp2string } from '../../helpers/utils';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { Link, useNavigate } from 'react-router-dom';
|
||
import {
|
||
Users,
|
||
Key,
|
||
Wallet,
|
||
ArrowRight,
|
||
RefreshCw,
|
||
Search,
|
||
FileText,
|
||
Activity,
|
||
} from 'lucide-react';
|
||
import {
|
||
Avatar as AntAvatar,
|
||
Card as AntCard,
|
||
Tag as AntTag,
|
||
Typography,
|
||
} from 'antd';
|
||
import {
|
||
Progress,
|
||
Table,
|
||
Button,
|
||
DatePicker,
|
||
Input,
|
||
Spin,
|
||
Tooltip,
|
||
} from '@/components/ui';
|
||
import { useOrgDashboard, useOrgQuota } from '../../hooks/org/useOrgData';
|
||
import { renderQuota } from '../../helpers/render';
|
||
import { API, showError } from '../../helpers';
|
||
import Loading from '../../components/common/ui/Loading';
|
||
import OrgBreadcrumb from './components/OrgBreadcrumb';
|
||
import OrgAnalyticsPanel from './components/OrgAnalyticsPanel';
|
||
import '../../styles/org-center.css';
|
||
import OrgSideSheet from './components/OrgSideSheet';
|
||
import OrgStateBlock from './components/OrgStateBlock';
|
||
import { StatusContext } from '../../context/Status';
|
||
import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules';
|
||
import { getOrgQuotaAvailability } from './orgQuotaAvailability';
|
||
|
||
const getOrgHeaders = (orgId) => ({ 'X-Org-Id': String(orgId) });
|
||
|
||
// 周期配额:daily/weekly/monthly 才有「周期内已用/重置」概念;none 为终身累计。
|
||
const isPeriodicQuotaPeriod = (period) =>
|
||
period === 'daily' || period === 'weekly' || period === 'monthly';
|
||
|
||
const safeQuotaNumber = (value) => Math.max(0, Number(value || 0));
|
||
|
||
// 用量阈值 → 语义色(Semi token,随明暗主题自适应)。
|
||
// 页面内所有「使用进度」类指示(统计卡进度条 / 使用率大进度条 / 成员表进度条)
|
||
// 统一走这一套:>80% 危险、>60% 警告、其余正常。
|
||
const usageStrokeColor = (percent) =>
|
||
percent > 80
|
||
? 'var(--ui-color-danger)'
|
||
: percent > 60
|
||
? 'var(--ui-color-warning)'
|
||
: 'var(--ui-color-success)';
|
||
|
||
const quotaPeriodLimitLabel = (t, period) => {
|
||
switch (period) {
|
||
case 'daily':
|
||
return t('每日个人上限');
|
||
case 'weekly':
|
||
return t('每周个人上限');
|
||
case 'monthly':
|
||
return t('每月个人上限');
|
||
default:
|
||
return t('个人终身上限');
|
||
}
|
||
};
|
||
|
||
const isFeatureEnabled = (flags, key) => {
|
||
const value = flags?.[key];
|
||
return value === true || value === 'true' || value === '1' || value === 1;
|
||
};
|
||
|
||
// 状态配置
|
||
const getStatusConfig = (status, t) => {
|
||
const map = {
|
||
1: { label: t('正常'), tagLabel: t('企业运行正常'), color: 'green' },
|
||
2: { label: t('冻结'), tagLabel: t('已被冻结'), color: 'red' },
|
||
3: { label: t('欠费'), tagLabel: t('额度不足'), color: 'orange' },
|
||
};
|
||
return map[status] || { label: '-', tagLabel: '-', color: 'grey' };
|
||
};
|
||
|
||
// 成员用量明细抽屉组件
|
||
const MemberUsageSheet = ({ visible, onClose, member, orgId, t }) => {
|
||
const [logs, setLogs] = useState([]);
|
||
const [total, setTotal] = useState(0);
|
||
const [loading, setLoading] = useState(false);
|
||
const [page, setPage] = useState(1);
|
||
const [dateRange, setDateRange] = useState([]);
|
||
const [modelName, setModelName] = useState('');
|
||
const [appliedFilters, setAppliedFilters] = useState({
|
||
dateRange: [],
|
||
modelName: '',
|
||
});
|
||
const requestSeq = useRef(0);
|
||
const pageSize = 20;
|
||
|
||
const fetchLogs = useCallback(
|
||
async (p = 1, filters = appliedFilters) => {
|
||
if (!member || !orgId) return;
|
||
const seq = ++requestSeq.current;
|
||
setLoading(true);
|
||
try {
|
||
let url = `/api/org/logs?user_id=${member.user_id}&p=${p}&page_size=${pageSize}`;
|
||
if (filters.dateRange && filters.dateRange.length === 2) {
|
||
if (filters.dateRange[0])
|
||
url += `&start_timestamp=${Math.floor(filters.dateRange[0].getTime() / 1000)}`;
|
||
if (filters.dateRange[1])
|
||
url += `&end_timestamp=${Math.floor(filters.dateRange[1].getTime() / 1000)}`;
|
||
}
|
||
if (filters.modelName)
|
||
url += `&model_name=${encodeURIComponent(filters.modelName)}`;
|
||
const res = await API.get(url, { headers: getOrgHeaders(orgId) });
|
||
if (seq !== requestSeq.current) return;
|
||
if (res.data.success) {
|
||
setLogs(res.data.data?.items || []);
|
||
// 后端在翻页(page>1)时跳过 COUNT 并返回 total=-1,保留首页已知总数,
|
||
// 否则 total 变 -1 会让"total > pageSize"不成立、分页器消失(与 useOrgLogs 同款约定)
|
||
const nextTotal = res.data.data?.total;
|
||
if (typeof nextTotal === 'number' && nextTotal >= 0) {
|
||
setTotal(nextTotal);
|
||
}
|
||
setPage(p);
|
||
} else {
|
||
showError(res.data.message);
|
||
}
|
||
} catch (err) {
|
||
if (seq === requestSeq.current) showError(err.message);
|
||
} finally {
|
||
if (seq === requestSeq.current) setLoading(false);
|
||
}
|
||
},
|
||
[member, orgId, appliedFilters],
|
||
);
|
||
|
||
useEffect(() => {
|
||
if (visible && member && orgId) {
|
||
// 重置过滤条件和分页状态
|
||
setDateRange([]);
|
||
setModelName('');
|
||
setAppliedFilters({ dateRange: [], modelName: '' });
|
||
setPage(1);
|
||
setLogs([]);
|
||
setTotal(0);
|
||
// 直接发起无过滤的请求(不依赖 state 更新)
|
||
const loadInitial = async () => {
|
||
const seq = ++requestSeq.current;
|
||
setLoading(true);
|
||
try {
|
||
const url = `/api/org/logs?user_id=${member.user_id}&p=1&page_size=${pageSize}`;
|
||
const res = await API.get(url, { headers: getOrgHeaders(orgId) });
|
||
if (seq !== requestSeq.current) return;
|
||
if (res.data.success) {
|
||
setLogs(res.data.data?.items || []);
|
||
setTotal(res.data.data?.total || 0);
|
||
} else {
|
||
showError(res.data.message);
|
||
}
|
||
} catch (err) {
|
||
if (seq === requestSeq.current) showError(err.message);
|
||
} finally {
|
||
if (seq === requestSeq.current) setLoading(false);
|
||
}
|
||
};
|
||
loadInitial();
|
||
}
|
||
}, [visible, member, orgId]);
|
||
|
||
const handleSearch = () => {
|
||
const filters = { dateRange, modelName: modelName.trim() };
|
||
setAppliedFilters(filters);
|
||
fetchLogs(1, filters);
|
||
};
|
||
|
||
const logColumns = useMemo(
|
||
() => [
|
||
{
|
||
title: t('时间'),
|
||
dataIndex: 'created_at',
|
||
width: 160,
|
||
render: (v) => (v ? timestamp2string(v) : '-'),
|
||
},
|
||
{
|
||
title: t('模型'),
|
||
dataIndex: 'model_name',
|
||
width: 180,
|
||
ellipsis: true,
|
||
render: (v) => (
|
||
<code className='text-xs px-1.5 py-0.5 bg-gray-100 dark:bg-neutral-800 rounded'>
|
||
{v || '-'}
|
||
</code>
|
||
),
|
||
},
|
||
{
|
||
title: t('Token 用量'),
|
||
width: 120,
|
||
render: (_, record) => (
|
||
<div className='text-xs text-gray-600 dark:text-gray-400'>
|
||
<span title='Prompt'>{record.prompt_tokens || 0}</span>
|
||
<span className='mx-1 text-gray-300 dark:text-gray-600'>/</span>
|
||
<span title='Completion'>{record.completion_tokens || 0}</span>
|
||
</div>
|
||
),
|
||
},
|
||
{
|
||
title: t('消耗额度'),
|
||
dataIndex: 'quota',
|
||
width: 100,
|
||
align: 'right',
|
||
render: (v) => (
|
||
<span className='text-orange-600 dark:text-orange-400 font-medium text-sm'>
|
||
{renderQuota(v, 6)}
|
||
</span>
|
||
),
|
||
},
|
||
],
|
||
[t],
|
||
);
|
||
|
||
return (
|
||
<OrgSideSheet
|
||
title={
|
||
<div className='flex items-center gap-2'>
|
||
<FileText size={18} className='text-brand-deep' />
|
||
<span>{member?.display_name || member?.username || ''}</span>
|
||
<span className='text-sm text-gray-400 font-normal'>
|
||
- {t('用量明细')}
|
||
</span>
|
||
</div>
|
||
}
|
||
visible={visible}
|
||
onCancel={onClose}
|
||
placement='right'
|
||
width={640}
|
||
bodyStyle={{ background: 'var(--ui-color-bg-0)' }}
|
||
style={{ background: 'var(--ui-color-bg-0)' }}
|
||
mask={true}
|
||
maskClosable={true}
|
||
maskStyle={{
|
||
backgroundColor: 'rgba(0, 0, 0, 0.45)',
|
||
backdropFilter: 'blur(4px)',
|
||
WebkitBackdropFilter: 'blur(4px)',
|
||
}}
|
||
>
|
||
<div className='space-y-4'>
|
||
{/* 过滤器 */}
|
||
<div className='flex flex-wrap items-end gap-3 p-3 bg-white dark:bg-neutral-900 rounded-lg border border-gray-200 dark:border-neutral-800'>
|
||
<div className='flex-1 min-w-[200px]'>
|
||
<div className='text-xs text-gray-500 dark:text-gray-400 mb-1'>
|
||
{t('时间范围')}
|
||
</div>
|
||
<DatePicker
|
||
type='dateTimeRange'
|
||
density='compact'
|
||
value={dateRange}
|
||
onChange={setDateRange}
|
||
placeholder={[t('开始时间'), t('结束时间')]}
|
||
style={{ width: '100%' }}
|
||
/>
|
||
</div>
|
||
<div className='min-w-[140px]'>
|
||
<div className='text-xs text-gray-500 dark:text-gray-400 mb-1'>
|
||
{t('模型名称')}
|
||
</div>
|
||
<Input
|
||
size='small'
|
||
placeholder={t('输入模型名称')}
|
||
value={modelName}
|
||
onChange={setModelName}
|
||
onEnterPress={handleSearch}
|
||
/>
|
||
</div>
|
||
<Button
|
||
size='small'
|
||
theme='solid'
|
||
icon={<Search size={14} />}
|
||
onClick={handleSearch}
|
||
loading={loading}
|
||
>
|
||
{t('查询')}
|
||
</Button>
|
||
</div>
|
||
|
||
{/* 统计 */}
|
||
<div className='text-xs text-gray-400 dark:text-gray-500'>
|
||
{t('共 {{count}} 条记录', { count: total })}
|
||
</div>
|
||
|
||
{/* 日志表格 */}
|
||
{loading && logs.length === 0 ? (
|
||
<div className='flex items-center justify-center py-12'>
|
||
<Spin />
|
||
</div>
|
||
) : (
|
||
<Table
|
||
columns={logColumns}
|
||
dataSource={logs}
|
||
rowKey='id'
|
||
size='small'
|
||
loading={loading}
|
||
pagination={
|
||
total > pageSize
|
||
? {
|
||
currentPage: page,
|
||
pageSize,
|
||
total,
|
||
onPageChange: (p) => fetchLogs(p),
|
||
}
|
||
: false
|
||
}
|
||
empty={
|
||
<div className='py-8 text-center'>
|
||
<div className='inline-flex p-3 rounded-full bg-gray-100 dark:bg-neutral-800 mb-2'>
|
||
<FileText
|
||
size={20}
|
||
className='text-gray-400 dark:text-gray-500'
|
||
/>
|
||
</div>
|
||
<p className='text-gray-400 dark:text-gray-500 text-sm'>
|
||
{t('暂无用量记录')}
|
||
</p>
|
||
</div>
|
||
}
|
||
/>
|
||
)}
|
||
</div>
|
||
</OrgSideSheet>
|
||
);
|
||
};
|
||
|
||
// 原型 .ea-budget-primary strong small:货币符号缩小为独立 small,数值格式仍来自 renderQuota
|
||
const renderBudgetAmount = (quota) => {
|
||
const text = String(renderQuota(quota, 2) ?? '');
|
||
const m = text.match(/^([^\d\-−]+)(.*)$/);
|
||
if (!m) return text;
|
||
return (
|
||
<>
|
||
<small>{m[1].trim()}</small>
|
||
{m[2]}
|
||
</>
|
||
);
|
||
};
|
||
|
||
const OrgDashboard = ({ orgId, orgRole, orgName, featureFlags }) => {
|
||
const { t } = useTranslation();
|
||
const navigate = useNavigate();
|
||
const [statusState] = useContext(StatusContext);
|
||
const { data, loading, fetch } = useOrgDashboard(orgId);
|
||
const {
|
||
data: quotaData,
|
||
loading: quotaLoading,
|
||
fetch: fetchQuota,
|
||
} = useOrgQuota(orgId);
|
||
const [usageSheetVisible, setUsageSheetVisible] = useState(false);
|
||
const [selectedMember, setSelectedMember] = useState(null);
|
||
|
||
const isAdmin =
|
||
orgRole === 'owner' || orgRole === 'admin' || orgRole === 'super_admin';
|
||
const analyticsEnabled = isFeatureEnabled(featureFlags, 'analytics');
|
||
const analyticsModuleOn = useMemo(() => {
|
||
const cfg = normalizeSidebarModulesAdmin(
|
||
statusState?.status?.SidebarModulesAdmin,
|
||
).orgCenter;
|
||
return cfg?.enabled !== false && cfg?.analytics !== false;
|
||
}, [statusState?.status?.SidebarModulesAdmin]);
|
||
const showAnalytics = isAdmin && analyticsEnabled && analyticsModuleOn;
|
||
|
||
const handleViewMemberUsage = useCallback((member) => {
|
||
setSelectedMember(member);
|
||
setUsageSheetVisible(true);
|
||
}, []);
|
||
|
||
// 成员额度表格列(useMemo 必须在所有早返回之前,保证 hooks 调用顺序一致)
|
||
const memberColumns = useMemo(
|
||
() => [
|
||
{
|
||
title: t('成员'),
|
||
dataIndex: 'display_name',
|
||
width: 170,
|
||
render: (text, record) => {
|
||
const name = text || record.username;
|
||
return (
|
||
<div className='oc-ea-member'>
|
||
<AntAvatar size={28}>
|
||
{(name || '?').charAt(0).toUpperCase()}
|
||
</AntAvatar>
|
||
{isAdmin ? (
|
||
<button
|
||
type='button'
|
||
className='oc-ea-member-link'
|
||
onClick={() => handleViewMemberUsage(record)}
|
||
>
|
||
{name}
|
||
</button>
|
||
) : (
|
||
<span>{name}</span>
|
||
)}
|
||
</div>
|
||
);
|
||
},
|
||
},
|
||
{
|
||
title: t('角色'),
|
||
dataIndex: 'role',
|
||
width: 132,
|
||
render: (role) => {
|
||
const config = {
|
||
owner: { label: t('所有者'), color: 'volcano' },
|
||
admin: { label: t('管理员'), color: 'geekblue' },
|
||
dept_admin: { label: t('部门管理员'), color: 'cyan' },
|
||
super_admin: { label: t('超级管理员'), color: 'purple' },
|
||
member: { label: t('成员'), color: 'default' },
|
||
};
|
||
const c = config[role] || config.member;
|
||
return <AntTag color={c.color}>{c.label}</AntTag>;
|
||
},
|
||
},
|
||
{
|
||
title: t('周期内已用'),
|
||
dataIndex: 'quota_used_in_period',
|
||
width: 130,
|
||
align: 'right',
|
||
render: (v, record) =>
|
||
isPeriodicQuotaPeriod(record.quota_period)
|
||
? renderQuota(safeQuotaNumber(v), 2)
|
||
: '—',
|
||
},
|
||
{
|
||
title: t('总已用额度'),
|
||
dataIndex: 'used_quota',
|
||
width: 130,
|
||
align: 'right',
|
||
render: (v) => renderQuota(safeQuotaNumber(v), 2),
|
||
},
|
||
{
|
||
title: t('额度上限 / 周期'),
|
||
dataIndex: 'quota_limit',
|
||
width: 150,
|
||
render: (v, record) => {
|
||
const quotaLimit = safeQuotaNumber(v);
|
||
return quotaLimit > 0 ? (
|
||
<div className='oc-ea-limit'>
|
||
{renderQuota(quotaLimit, 2)}
|
||
<small>
|
||
{quotaPeriodLimitLabel(t, record.quota_period)}
|
||
{!isPeriodicQuotaPeriod(record.quota_period) &&
|
||
` · ${t('不重置')}`}
|
||
</small>
|
||
</div>
|
||
) : (
|
||
t('不限')
|
||
);
|
||
},
|
||
},
|
||
{
|
||
title: t('使用进度'),
|
||
width: 150,
|
||
render: (_, record) => {
|
||
const quotaLimit = safeQuotaNumber(record.quota_limit);
|
||
if (quotaLimit <= 0) return '—';
|
||
// 周期模式按「周期内已用 / 上限」;终身模式按「总已用 / 上限」。
|
||
const usedForProgress = isPeriodicQuotaPeriod(record.quota_period)
|
||
? safeQuotaNumber(record.quota_used_in_period)
|
||
: safeQuotaNumber(record.used_quota);
|
||
const percent = Math.min(
|
||
100,
|
||
Math.round((usedForProgress / quotaLimit) * 100),
|
||
);
|
||
return (
|
||
<Progress
|
||
percent={percent}
|
||
size='small'
|
||
stroke={usageStrokeColor(percent)}
|
||
/>
|
||
);
|
||
},
|
||
},
|
||
],
|
||
[t, isAdmin, handleViewMemberUsage],
|
||
);
|
||
|
||
// “成员用量”按钮:切换到成员用量明细并在页内定位(不发请求)
|
||
const membersAnchorRef = useRef(null);
|
||
const [detailTab, setDetailTab] = useState('models');
|
||
const showMembers = () => {
|
||
setDetailTab('members');
|
||
window.setTimeout(() => {
|
||
const reduce = window.matchMedia?.(
|
||
'(prefers-reduced-motion: reduce)',
|
||
)?.matches;
|
||
membersAnchorRef.current?.scrollIntoView({
|
||
behavior: reduce ? 'auto' : 'smooth',
|
||
block: 'start',
|
||
});
|
||
}, 60);
|
||
};
|
||
|
||
useEffect(() => {
|
||
fetch();
|
||
fetchQuota();
|
||
}, [fetch, fetchQuota]);
|
||
|
||
if (loading && !data) {
|
||
return <Loading />;
|
||
}
|
||
|
||
// 额度使用率(防御性处理)
|
||
const totalQuota = data
|
||
? Number(data.quota || 0) + Number(data.used_quota || 0)
|
||
: 0;
|
||
const usedQuota = Number(data?.used_quota || 0);
|
||
const usedPercent =
|
||
totalQuota > 0
|
||
? Math.min(100, Math.round((usedQuota / totalQuota) * 100))
|
||
: 0;
|
||
|
||
// 成员/密钥容量占用率:与其他使用进度共用同一套阈值语义色
|
||
const memberPercent =
|
||
data?.max_members > 0
|
||
? Math.min(
|
||
100,
|
||
Math.round((Number(data.member_count || 0) / data.max_members) * 100),
|
||
)
|
||
: 0;
|
||
const tokenPercent =
|
||
data?.max_tokens > 0
|
||
? Math.min(
|
||
100,
|
||
Math.round((Number(data.token_count || 0) / data.max_tokens) * 100),
|
||
)
|
||
: 0;
|
||
|
||
const statusConfig = getStatusConfig(data?.status, t);
|
||
const quotaAvailability = getOrgQuotaAvailability(data);
|
||
|
||
// 成员明细对账:成员合计(终身已用)与企业池已用(org.UsedQuota)通常不相等,
|
||
// 差额为「未归属」消费(密钥未绑定成员 / 管理员直接使用 / 成员跟踪前的历史),
|
||
// 显式列出避免用户误以为成员明细加总应等于顶部「已用额度」。
|
||
const memberQuotaRows = quotaData?.member_quotas || [];
|
||
const memberQuotaPage = Number(quotaData?.member_page || 1);
|
||
const memberQuotaPageSize = Number(quotaData?.member_page_size || 10);
|
||
const memberQuotaTotal = Number(
|
||
quotaData?.member_total ?? memberQuotaRows.length,
|
||
);
|
||
const memberUsedSum = Number(
|
||
quotaData?.member_used_quota ??
|
||
memberQuotaRows.reduce(
|
||
(sum, m) => sum + Math.max(0, Number(m.used_quota || 0)),
|
||
0,
|
||
),
|
||
);
|
||
const poolUsedQuota = Number(quotaData?.used_quota ?? data?.used_quota ?? 0);
|
||
const unattributedQuota = Math.max(0, poolUsedQuota - memberUsedSum);
|
||
|
||
const statusTone = data?.status === 1 ? 'ok' : data?.status ? 'warn' : 'none';
|
||
|
||
const memberUsageCard = (
|
||
<AntCard className='oc-ea-detail-card oc-ea-members'>
|
||
<div className='oc-ea-table-toolbar'>
|
||
<h2>{t('成员用量明细')}</h2>
|
||
<Link to='/org/members' className='oc-ea-view-all'>
|
||
{t('查看全部')}
|
||
<ArrowRight size={12} />
|
||
</Link>
|
||
</div>
|
||
<Table
|
||
columns={memberColumns}
|
||
dataSource={memberQuotaRows}
|
||
loading={quotaLoading}
|
||
rowKey='member_id'
|
||
scroll={{ x: 860 }}
|
||
pagination={
|
||
memberQuotaTotal > memberQuotaPageSize
|
||
? {
|
||
currentPage: memberQuotaPage,
|
||
pageSize: memberQuotaPageSize,
|
||
total: memberQuotaTotal,
|
||
showSizeChanger: false,
|
||
onPageChange: (page) => fetchQuota(page, memberQuotaPageSize),
|
||
showTotal: (n) => t('共 {{total}} 条', { total: n }),
|
||
}
|
||
: false
|
||
}
|
||
empty={<OrgStateBlock icon={Users} title={t('暂无成员数据')} />}
|
||
/>
|
||
{memberQuotaRows.length > 0 && (
|
||
<div className='oc-ea-member-total'>
|
||
<span>
|
||
{t('成员合计已用')}
|
||
<strong>{renderQuota(memberUsedSum, 2)}</strong>
|
||
</span>
|
||
<span>
|
||
{t('企业池已用')}
|
||
<strong>{renderQuota(poolUsedQuota, 2)}</strong>
|
||
</span>
|
||
{unattributedQuota > 0 && (
|
||
<Tooltip
|
||
content={t(
|
||
'企业池已用包含未绑定到具体成员的消费(如管理员直接使用、密钥未分配成员、成员跟踪前的历史数据),因此成员合计通常小于企业池已用。',
|
||
)}
|
||
>
|
||
<span className='oc-ea-unattributed'>
|
||
{t('未归属差额')}
|
||
<strong>{renderQuota(unattributedQuota, 2)}</strong>
|
||
</span>
|
||
</Tooltip>
|
||
)}
|
||
</div>
|
||
)}
|
||
</AntCard>
|
||
);
|
||
|
||
return (
|
||
<div className='uc-page oc-page oc-dashboard'>
|
||
<div className='oc-dash-breadcrumb'>
|
||
<OrgBreadcrumb current={t('数据看板')} />
|
||
</div>
|
||
|
||
{/* 企业概况(原型 .ea-hero) */}
|
||
<header className='oc-dash-hero'>
|
||
<div className={`oc-dash-health is-${statusTone}`}>
|
||
<span>
|
||
<i aria-hidden='true' />
|
||
{statusConfig.tagLabel}
|
||
</span>
|
||
{orgName && <div className='oc-dash-org-name'>{orgName}</div>}
|
||
</div>
|
||
<div className='oc-dash-heading'>
|
||
<div>
|
||
<h1>{t('企业数据看板')}</h1>
|
||
<p>{t('掌握企业额度、组织规模与模型消耗')}</p>
|
||
</div>
|
||
<div className='oc-dash-actions'>
|
||
<Button onClick={showMembers}>{t('成员用量')}</Button>
|
||
<Button
|
||
icon={<RefreshCw size={16} />}
|
||
onClick={() => {
|
||
fetch();
|
||
fetchQuota(memberQuotaPage, memberQuotaPageSize);
|
||
}}
|
||
loading={loading || quotaLoading}
|
||
>
|
||
{t('刷新')}
|
||
</Button>
|
||
{isAdmin && (
|
||
<Button
|
||
icon={<Wallet size={16} />}
|
||
theme='solid'
|
||
type='primary'
|
||
onClick={() => navigate('/org/recharge')}
|
||
>
|
||
{t('立即充值')}
|
||
</Button>
|
||
)}
|
||
</div>
|
||
</div>
|
||
<div className='oc-dash-overview'>
|
||
<AntCard className='oc-dash-budget'>
|
||
<div className='oc-dash-budget-heading'>
|
||
<h2>{t('企业额度')}</h2>
|
||
<span>{t('累计额度使用情况')}</span>
|
||
</div>
|
||
<div className='oc-dash-budget-primary'>
|
||
<span>{t('剩余额度')}</span>
|
||
<strong>
|
||
{data ? renderBudgetAmount(Number(data.quota || 0)) : '-'}
|
||
</strong>
|
||
{quotaAvailability && (
|
||
<div
|
||
className={`oc-dash-budget-availability is-${quotaAvailability.type}`}
|
||
role='status'
|
||
>
|
||
<Typography.Text type={quotaAvailability.type}>
|
||
{t(quotaAvailability.label)}
|
||
</Typography.Text>
|
||
</div>
|
||
)}
|
||
</div>
|
||
<div className='oc-dash-budget-secondary'>
|
||
<div>
|
||
<span>{t('总额度')}</span>
|
||
<strong>{data ? renderQuota(totalQuota, 2) : '-'}</strong>
|
||
</div>
|
||
<div>
|
||
<span>{t('已用额度')}</span>
|
||
<strong>{data ? renderQuota(usedQuota, 2) : '-'}</strong>
|
||
</div>
|
||
</div>
|
||
<div className='oc-dash-budget-progress'>
|
||
<div>
|
||
<span>{t('累计使用率')}</span>
|
||
<strong style={{ color: usageStrokeColor(usedPercent) }}>
|
||
{usedPercent}%
|
||
</strong>
|
||
</div>
|
||
<Progress
|
||
percent={usedPercent}
|
||
size='small'
|
||
showInfo={false}
|
||
stroke={usageStrokeColor(usedPercent)}
|
||
/>
|
||
</div>
|
||
</AntCard>
|
||
|
||
<div className='oc-dash-org'>
|
||
<div>
|
||
<span>
|
||
<Users size={16} />
|
||
{t('企业成员')}
|
||
</span>
|
||
<strong>
|
||
{data?.member_count ?? 0}
|
||
<small>
|
||
{data?.max_members > 0
|
||
? `/ ${Number(data.max_members).toLocaleString()}`
|
||
: `/ ${t('不限')}`}
|
||
</small>
|
||
</strong>
|
||
{data?.max_members > 0 && (
|
||
<Progress
|
||
percent={memberPercent}
|
||
size='small'
|
||
showInfo={false}
|
||
stroke={usageStrokeColor(memberPercent)}
|
||
/>
|
||
)}
|
||
</div>
|
||
<div>
|
||
<span>
|
||
<Key size={16} />
|
||
{t('企业密钥')}
|
||
</span>
|
||
<strong>
|
||
{data?.token_count ?? 0}
|
||
<small>
|
||
{data?.max_tokens > 0
|
||
? `/ ${Number(data.max_tokens).toLocaleString()}`
|
||
: `/ ${t('不限')}`}
|
||
</small>
|
||
</strong>
|
||
{data?.max_tokens > 0 && (
|
||
<Progress
|
||
percent={tokenPercent}
|
||
size='small'
|
||
showInfo={false}
|
||
stroke={usageStrokeColor(tokenPercent)}
|
||
/>
|
||
)}
|
||
</div>
|
||
<div className='oc-dash-status'>
|
||
<span>
|
||
<Activity size={16} />
|
||
{t('企业状态')}
|
||
</span>
|
||
<strong>{statusConfig.label}</strong>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
{showAnalytics ? (
|
||
<OrgAnalyticsPanel
|
||
orgId={orgId}
|
||
memberUsage={memberUsageCard}
|
||
detailTab={detailTab}
|
||
onDetailTabChange={setDetailTab}
|
||
detailsRef={membersAnchorRef}
|
||
/>
|
||
) : (
|
||
<div ref={membersAnchorRef}>{memberUsageCard}</div>
|
||
)}
|
||
|
||
{/* 成员用量明细抽屉 */}
|
||
<MemberUsageSheet
|
||
visible={usageSheetVisible}
|
||
onClose={() => setUsageSheetVisible(false)}
|
||
member={selectedMember}
|
||
orgId={orgId}
|
||
t={t}
|
||
/>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default OrgDashboard;
|