Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,895 @@
|
||||
/*
|
||||
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 {
|
||||
BarChart2,
|
||||
Users,
|
||||
Key,
|
||||
Wallet,
|
||||
TrendingUp,
|
||||
ArrowRight,
|
||||
ShieldCheck,
|
||||
Crown,
|
||||
Shield,
|
||||
PiggyBank,
|
||||
Flame,
|
||||
Coins,
|
||||
RefreshCw,
|
||||
Search,
|
||||
FileText,
|
||||
CreditCard,
|
||||
Activity,
|
||||
} from 'lucide-react';
|
||||
import {
|
||||
Progress,
|
||||
Table,
|
||||
Tag,
|
||||
Button,
|
||||
DatePicker,
|
||||
Input,
|
||||
Spin,
|
||||
Tooltip,
|
||||
} from '@douyinfe/semi-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 OrgPageHeader from './components/OrgPageHeader';
|
||||
import OrgAnalyticsPanel from './components/OrgAnalyticsPanel';
|
||||
import StatCard from './components/OrgStatCard';
|
||||
import OrgPanel from './components/OrgPanel';
|
||||
import OrgSideSheet from './components/OrgSideSheet';
|
||||
import OrgStateBlock from './components/OrgStateBlock';
|
||||
import { StatusContext } from '../../context/Status';
|
||||
import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules';
|
||||
|
||||
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(--semi-color-danger)'
|
||||
: percent > 60
|
||||
? 'var(--semi-color-warning)'
|
||||
: 'var(--semi-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(--semi-color-bg-0)' }}
|
||||
style={{ background: 'var(--semi-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>
|
||||
);
|
||||
};
|
||||
|
||||
const OrgDashboard = ({ orgId, orgRole, 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: 200,
|
||||
render: (text, record) => (
|
||||
<div className='flex items-center gap-2.5'>
|
||||
<div className='flex-shrink-0 w-8 h-8 rounded-full bg-brand/10 text-brand flex items-center justify-center text-xs font-semibold'>
|
||||
{(text || record.username || '?').charAt(0).toUpperCase()}
|
||||
</div>
|
||||
<div className='min-w-0'>
|
||||
{isAdmin ? (
|
||||
<div
|
||||
className='font-medium text-brand-deep truncate text-sm cursor-pointer hover:text-brand hover:underline transition-colors'
|
||||
onClick={() => handleViewMemberUsage(record)}
|
||||
>
|
||||
{text || record.username}
|
||||
</div>
|
||||
) : (
|
||||
<div className='font-medium text-gray-900 dark:text-white truncate text-sm'>
|
||||
{text || record.username}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('角色'),
|
||||
dataIndex: 'role',
|
||||
width: 100,
|
||||
align: 'center',
|
||||
render: (role) => {
|
||||
const config = {
|
||||
owner: { label: t('所有者'), color: 'red', icon: Crown },
|
||||
admin: { label: t('管理员'), color: 'blue', icon: ShieldCheck },
|
||||
dept_admin: {
|
||||
label: t('部门管理员'),
|
||||
color: 'cyan',
|
||||
icon: ShieldCheck,
|
||||
},
|
||||
super_admin: {
|
||||
label: t('超级管理员'),
|
||||
color: 'violet',
|
||||
icon: ShieldCheck,
|
||||
},
|
||||
member: { label: t('成员'), color: 'grey', icon: Shield },
|
||||
};
|
||||
const c = config[role] || config.member;
|
||||
return (
|
||||
<div className='flex items-center justify-center gap-1'>
|
||||
<c.icon
|
||||
size={12}
|
||||
className={
|
||||
role === 'owner'
|
||||
? 'text-amber-500'
|
||||
: role === 'admin' ||
|
||||
role === 'dept_admin' ||
|
||||
role === 'super_admin'
|
||||
? 'text-brand-deep'
|
||||
: 'text-gray-400 dark:text-gray-500'
|
||||
}
|
||||
/>
|
||||
<Tag size='small' color={c.color}>
|
||||
{c.label}
|
||||
</Tag>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('周期内已用'),
|
||||
dataIndex: 'quota_used_in_period',
|
||||
width: 120,
|
||||
align: 'right',
|
||||
render: (v, record) =>
|
||||
isPeriodicQuotaPeriod(record.quota_period) ? (
|
||||
<span className='text-gray-700 dark:text-gray-300 font-medium'>
|
||||
{renderQuota(safeQuotaNumber(v), 2)}
|
||||
</span>
|
||||
) : (
|
||||
<span className='text-xs text-gray-400 dark:text-gray-500'>—</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('总已用额度'),
|
||||
dataIndex: 'used_quota',
|
||||
width: 120,
|
||||
align: 'right',
|
||||
render: (v) => (
|
||||
<span className='text-gray-700 dark:text-gray-300 font-medium'>
|
||||
{renderQuota(safeQuotaNumber(v), 2)}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('额度上限/周期'),
|
||||
dataIndex: 'quota_limit',
|
||||
width: 150,
|
||||
align: 'right',
|
||||
render: (v, record) => {
|
||||
const quotaLimit = safeQuotaNumber(v);
|
||||
return quotaLimit > 0 ? (
|
||||
<div className='text-right'>
|
||||
<div className='text-gray-700 dark:text-gray-300'>
|
||||
{renderQuota(quotaLimit, 2)}
|
||||
</div>
|
||||
<div
|
||||
className={`text-[11px] ${
|
||||
isPeriodicQuotaPeriod(record.quota_period)
|
||||
? 'text-gray-400 dark:text-gray-500'
|
||||
: 'text-amber-500'
|
||||
}`}
|
||||
>
|
||||
{quotaPeriodLimitLabel(t, record.quota_period)}
|
||||
{!isPeriodicQuotaPeriod(record.quota_period) &&
|
||||
` · ${t('不重置')}`}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<span className='text-gray-400 dark:text-gray-500'>
|
||||
{t('不限')}
|
||||
</span>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('使用进度'),
|
||||
width: 160,
|
||||
render: (_, record) => {
|
||||
const quotaLimit = safeQuotaNumber(record.quota_limit);
|
||||
if (quotaLimit <= 0) {
|
||||
return (
|
||||
<span className='text-xs text-gray-400 dark:text-gray-500'>
|
||||
-
|
||||
</span>
|
||||
);
|
||||
}
|
||||
// 周期模式按「周期内已用 / 上限」;终身模式按「总已用 / 上限」。
|
||||
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 (
|
||||
<div className='flex items-center gap-2'>
|
||||
<Progress
|
||||
percent={percent}
|
||||
size='small'
|
||||
style={{ width: 100 }}
|
||||
stroke={usageStrokeColor(percent)}
|
||||
/>
|
||||
<span className='text-xs text-gray-500 dark:text-gray-400 w-8 text-right'>
|
||||
{percent}%
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
],
|
||||
[t, isAdmin, handleViewMemberUsage],
|
||||
);
|
||||
|
||||
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);
|
||||
|
||||
// 成员明细对账:成员合计(终身已用)与企业池已用(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);
|
||||
|
||||
return (
|
||||
<div className='p-4 sm:p-6 lg:p-8 space-y-6'>
|
||||
{/* Header */}
|
||||
<OrgPageHeader
|
||||
icon={BarChart2}
|
||||
title={t('企业概览')}
|
||||
subtitle={t(
|
||||
'聚合企业额度、组织规模与成员消耗,和个人工作台保持职责分离',
|
||||
)}
|
||||
actions={
|
||||
<>
|
||||
{isAdmin && (
|
||||
<Button
|
||||
icon={<CreditCard size={14} />}
|
||||
theme='solid'
|
||||
type='primary'
|
||||
onClick={() => navigate('/org/recharge')}
|
||||
className='!bg-gradient-to-r !from-emerald-500 !to-emerald-600 !border-0 !shadow-md !shadow-emerald-500/30'
|
||||
>
|
||||
{t('立即充值')}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
icon={<RefreshCw size={14} />}
|
||||
onClick={() => {
|
||||
fetch();
|
||||
fetchQuota(memberQuotaPage, memberQuotaPageSize);
|
||||
}}
|
||||
loading={loading || quotaLoading}
|
||||
>
|
||||
{t('刷新')}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
{/* 统计卡片 - 三列 */}
|
||||
<div className='grid grid-cols-1 sm:grid-cols-3 gap-4'>
|
||||
<StatCard
|
||||
icon={Users}
|
||||
title={t('企业成员')}
|
||||
value={data?.member_count ?? 0}
|
||||
tone='brand'
|
||||
loading={loading}
|
||||
subtitle={
|
||||
data?.max_members > 0
|
||||
? `${t('上限')}: ${data.max_members}`
|
||||
: t('不限')
|
||||
}
|
||||
extra={
|
||||
data?.max_members > 0 && (
|
||||
<Progress
|
||||
percent={memberPercent}
|
||||
size='small'
|
||||
stroke={usageStrokeColor(memberPercent)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/>
|
||||
<StatCard
|
||||
icon={Key}
|
||||
title={t('企业令牌')}
|
||||
value={data?.token_count ?? 0}
|
||||
tone='amber'
|
||||
loading={loading}
|
||||
subtitle={
|
||||
data?.max_tokens > 0
|
||||
? `${t('上限')}: ${data.max_tokens}`
|
||||
: t('不限')
|
||||
}
|
||||
extra={
|
||||
data?.max_tokens > 0 && (
|
||||
<Progress
|
||||
percent={tokenPercent}
|
||||
size='small'
|
||||
stroke={usageStrokeColor(tokenPercent)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/>
|
||||
<StatCard
|
||||
icon={TrendingUp}
|
||||
title={t('企业状态')}
|
||||
value={statusConfig.label}
|
||||
tone='sky'
|
||||
loading={loading}
|
||||
extra={
|
||||
data && (
|
||||
<Tag
|
||||
size='small'
|
||||
color={statusConfig.color}
|
||||
type='light'
|
||||
className='mt-0.5'
|
||||
>
|
||||
{statusConfig.tagLabel}
|
||||
</Tag>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 额度使用详情 - 全宽 */}
|
||||
<OrgPanel icon={Wallet} title={t('额度使用详情')} padding='p-5'>
|
||||
<div className='grid grid-cols-1 sm:grid-cols-3 gap-4'>
|
||||
{/* 总额度 */}
|
||||
<div className='flex items-center gap-3 p-4 rounded-xl bg-gray-50/80 dark:bg-neutral-800/50 border border-gray-100 dark:border-neutral-700/50'>
|
||||
<div className='p-2 rounded-lg bg-brand/10'>
|
||||
<Coins size={16} className='text-brand-deep' />
|
||||
</div>
|
||||
<div>
|
||||
<div className='text-xs font-medium text-gray-400 dark:text-gray-500 mb-0.5'>
|
||||
{t('总额度')}
|
||||
</div>
|
||||
<div className='text-lg font-bold text-gray-900 dark:text-white'>
|
||||
{data ? renderQuota(totalQuota, 2) : '-'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/* 已用额度 */}
|
||||
<div className='flex items-center gap-3 p-4 rounded-xl bg-orange-50/80 dark:bg-orange-900/10 border border-orange-100 dark:border-orange-800/30'>
|
||||
<div className='p-2 rounded-lg bg-orange-100 dark:bg-orange-900/30'>
|
||||
<Flame
|
||||
size={16}
|
||||
className='text-orange-600 dark:text-orange-400'
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<div className='text-xs font-medium text-gray-400 dark:text-gray-500 mb-0.5'>
|
||||
{t('已用额度')}
|
||||
</div>
|
||||
<div className='text-lg font-bold text-orange-600 dark:text-orange-400'>
|
||||
{data ? renderQuota(usedQuota, 2) : '-'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/* 剩余额度 */}
|
||||
<div className='flex items-center gap-3 p-4 rounded-xl bg-emerald-50/80 dark:bg-emerald-900/10 border border-emerald-100 dark:border-emerald-800/30'>
|
||||
<div className='p-2 rounded-lg bg-emerald-100 dark:bg-emerald-900/30'>
|
||||
<PiggyBank
|
||||
size={16}
|
||||
className='text-emerald-600 dark:text-emerald-400'
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<div className='text-xs font-medium text-gray-400 dark:text-gray-500 mb-0.5'>
|
||||
{t('剩余额度')}
|
||||
</div>
|
||||
<div className='text-lg font-bold text-emerald-600 dark:text-emerald-400'>
|
||||
{data ? renderQuota(Number(data.quota || 0), 2) : '-'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 使用率进度条 */}
|
||||
<div className='mt-5 pt-4 border-t border-gray-100 dark:border-neutral-800'>
|
||||
<div className='flex items-center justify-between mb-2'>
|
||||
<span className='text-sm text-gray-500 dark:text-gray-400'>
|
||||
{t('使用率(累计)')}
|
||||
</span>
|
||||
<span
|
||||
className='text-sm font-semibold'
|
||||
style={{ color: usageStrokeColor(usedPercent) }}
|
||||
>
|
||||
{usedPercent}%
|
||||
</span>
|
||||
</div>
|
||||
<div className='w-full h-3 bg-gray-100 dark:bg-neutral-800 rounded-full overflow-hidden'>
|
||||
{/* 渐变由同一语义色的浅调 → 本色构成(单色相),
|
||||
不再引入第二色相(旧 emerald→teal 与用量分析的 teal 系列色撞车) */}
|
||||
<div
|
||||
className='h-full rounded-full transition-all duration-1000 ease-out'
|
||||
style={{
|
||||
width: `${Math.max(usedPercent, 0)}%`,
|
||||
background: `linear-gradient(90deg, color-mix(in srgb, ${usageStrokeColor(usedPercent)} 72%, #fff) 0%, ${usageStrokeColor(usedPercent)} 100%)`,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</OrgPanel>
|
||||
|
||||
{/* 成员用量明细 */}
|
||||
<OrgPanel
|
||||
icon={Users}
|
||||
title={t('成员用量明细')}
|
||||
padding='p-5'
|
||||
bodyClassName='overflow-x-auto'
|
||||
actions={
|
||||
<Link
|
||||
to='/org/members'
|
||||
className='flex items-center gap-1 text-xs text-brand-deep hover:text-brand transition-colors'
|
||||
>
|
||||
{t('查看全部')}
|
||||
<ArrowRight size={12} />
|
||||
</Link>
|
||||
}
|
||||
>
|
||||
<Table
|
||||
columns={memberColumns}
|
||||
dataSource={memberQuotaRows}
|
||||
loading={quotaLoading}
|
||||
rowKey='member_id'
|
||||
pagination={
|
||||
memberQuotaTotal > memberQuotaPageSize
|
||||
? {
|
||||
currentPage: memberQuotaPage,
|
||||
pageSize: memberQuotaPageSize,
|
||||
total: memberQuotaTotal,
|
||||
showSizeChanger: false,
|
||||
onPageChange: (page) => fetchQuota(page, memberQuotaPageSize),
|
||||
}
|
||||
: false
|
||||
}
|
||||
size='small'
|
||||
empty={<OrgStateBlock icon={Users} title={t('暂无成员数据')} />}
|
||||
/>
|
||||
{memberQuotaRows.length > 0 && (
|
||||
<div className='mt-3 pt-3 border-t border-gray-100 dark:border-neutral-800 flex flex-wrap items-center gap-x-6 gap-y-1 text-xs'>
|
||||
<span className='text-gray-500 dark:text-gray-400'>
|
||||
{t('成员合计已用')}:
|
||||
<span className='font-medium text-gray-700 dark:text-gray-300'>
|
||||
{renderQuota(memberUsedSum, 2)}
|
||||
</span>
|
||||
</span>
|
||||
<span className='text-gray-500 dark:text-gray-400'>
|
||||
{t('企业池已用')}:
|
||||
<span className='font-medium text-gray-700 dark:text-gray-300'>
|
||||
{renderQuota(poolUsedQuota, 2)}
|
||||
</span>
|
||||
</span>
|
||||
{unattributedQuota > 0 && (
|
||||
<Tooltip
|
||||
content={t(
|
||||
'企业池已用包含未绑定到具体成员的消费(如管理员直接使用、令牌未分配成员、成员跟踪前的历史数据),因此成员合计通常小于企业池已用。',
|
||||
)}
|
||||
>
|
||||
<span className='text-amber-600 dark:text-amber-400 cursor-help'>
|
||||
{t('未归属差额')}:
|
||||
<span className='font-medium'>
|
||||
{renderQuota(unattributedQuota, 2)}
|
||||
</span>
|
||||
</span>
|
||||
</Tooltip>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</OrgPanel>
|
||||
|
||||
{showAnalytics && (
|
||||
<div className='space-y-3'>
|
||||
<div className='flex items-center gap-2 px-1'>
|
||||
<Activity size={16} className='text-brand-deep' />
|
||||
<div>
|
||||
<div className='font-semibold text-gray-950 dark:text-white'>
|
||||
{t('多维用量分析')}
|
||||
</div>
|
||||
<div className='text-xs text-gray-500 dark:text-gray-400'>
|
||||
{t('管理员专属经营数据,避免与个人工作台重复展示')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<OrgAnalyticsPanel orgId={orgId} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 成员用量明细抽屉 */}
|
||||
<MemberUsageSheet
|
||||
visible={usageSheetVisible}
|
||||
onClose={() => setUsageSheetVisible(false)}
|
||||
member={selectedMember}
|
||||
orgId={orgId}
|
||||
t={t}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default OrgDashboard;
|
||||
Reference in New Issue
Block a user