feat: 参考原型图 ui优化提交 企业端-企业中心 相关的页面
This commit is contained in:
@@ -16,8 +16,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import React, {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Switch } from 'antd';
|
||||
import {
|
||||
Table,
|
||||
Button,
|
||||
@@ -25,10 +32,9 @@ import {
|
||||
Toast,
|
||||
Tag,
|
||||
Progress,
|
||||
Banner,
|
||||
Popconfirm,
|
||||
} from '@/components/ui';
|
||||
import { RefreshCw, Plus, Pencil, Trash2 } from 'lucide-react';
|
||||
import { RefreshCw, Plus } from 'lucide-react';
|
||||
import { API } from '../../helpers/api';
|
||||
import { showError } from '../../helpers';
|
||||
import {
|
||||
@@ -38,7 +44,7 @@ import {
|
||||
getQuotaInputUnit,
|
||||
} from '../../helpers/render';
|
||||
import OrgPageHeader from './components/OrgPageHeader';
|
||||
import OrgPanel from './components/OrgPanel';
|
||||
import { OrgListCard, OrgRowActions } from './components/OrgListCard';
|
||||
import OrgModal from './components/OrgModal';
|
||||
|
||||
const getOrgHeaders = (orgId) => ({ 'X-Org-Id': String(orgId) });
|
||||
@@ -48,7 +54,7 @@ const subjectTypeLabel = (t, st) =>
|
||||
department: t('部门'),
|
||||
project: t('项目'),
|
||||
member: t('成员'),
|
||||
token: t('令牌'),
|
||||
token: t('密钥'),
|
||||
org: t('企业'),
|
||||
})[st] || st;
|
||||
|
||||
@@ -65,11 +71,9 @@ const cycleLabel = (t, c) =>
|
||||
const OrgBudgets = ({ orgId }) => {
|
||||
const { t } = useTranslation();
|
||||
const [list, setList] = useState([]);
|
||||
// null=未知(接口未返回该字段的旧后端),true/false=预算 cap 是否真正在扣费链路生效
|
||||
const [enforcementEnabled, setEnforcementEnabled] = useState(null);
|
||||
// 未生效的细分原因:平台级引擎急停(engine=false) vs 企业未开「预算实时拦截」
|
||||
const [engineEnabled, setEngineEnabled] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [updatingBudgetId, setUpdatingBudgetId] = useState(null);
|
||||
const updatingBudgetRef = useRef(false);
|
||||
const [depts, setDepts] = useState([]);
|
||||
const [projects, setProjects] = useState([]);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
@@ -96,29 +100,17 @@ const OrgBudgets = ({ orgId }) => {
|
||||
API.get('/api/org/project', { headers: getOrgHeaders(orgId) }),
|
||||
]);
|
||||
if (b.data.success) {
|
||||
// 新后端返回 {items, enforcement_enabled, engine_enabled};兼容旧后端直接返回数组
|
||||
// 新后端返回 {items, enforcement_enabled, engine_enabled};兼容旧后端直接返回数组。
|
||||
// enforcement/engine 两个字段原只驱动表格上方旧提示栏,提示栏按原型移除后不再读取。
|
||||
const payload = b.data.data;
|
||||
if (Array.isArray(payload)) {
|
||||
setList(payload || []);
|
||||
setEnforcementEnabled(null);
|
||||
setEngineEnabled(null);
|
||||
} else {
|
||||
setList(payload?.items || []);
|
||||
setEnforcementEnabled(payload?.enforcement_enabled ?? null);
|
||||
setEngineEnabled(payload?.engine_enabled ?? null);
|
||||
}
|
||||
setList(Array.isArray(payload) ? payload || [] : payload?.items || []);
|
||||
} else {
|
||||
showError(b.data.message);
|
||||
// 拉取失败回到「未知」,避免 Banner 停留在陈旧状态
|
||||
setEnforcementEnabled(null);
|
||||
setEngineEnabled(null);
|
||||
}
|
||||
if (d.data.success) setDepts(d.data.data || []);
|
||||
if (p.data.success) setProjects(p.data.data || []);
|
||||
} catch (err) {
|
||||
showError(err.message);
|
||||
setEnforcementEnabled(null);
|
||||
setEngineEnabled(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -128,7 +120,7 @@ const OrgBudgets = ({ orgId }) => {
|
||||
fetchAll();
|
||||
}, [fetchAll]);
|
||||
|
||||
// 主体名称与存在性优先取后端 subject_* 字段(覆盖成员/令牌,且能区分「已删除」与「已停用」);
|
||||
// 主体名称与存在性优先取后端 subject_* 字段(覆盖成员/密钥,且能区分「已删除」与「已停用」);
|
||||
// 旧后端未返回时回落到本地部门/项目列表匹配。
|
||||
const subjectOf = useCallback(
|
||||
(r) => {
|
||||
@@ -156,7 +148,7 @@ const OrgBudgets = ({ orgId }) => {
|
||||
disabled: p?.status === 2,
|
||||
};
|
||||
}
|
||||
// 旧后端下成员/令牌无从解析,按存在处理,避免误标「已删除」
|
||||
// 旧后端下成员/密钥无从解析,按存在处理,避免误标「已删除」
|
||||
return { name: `#${r.subject_id}`, exists: true, disabled: false };
|
||||
},
|
||||
[depts, projects],
|
||||
@@ -252,6 +244,52 @@ const OrgBudgets = ({ orgId }) => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleStatusChange = async (record, enabled) => {
|
||||
if (
|
||||
updatingBudgetRef.current ||
|
||||
(record.status === 1) === enabled ||
|
||||
!subjectOf(record).exists
|
||||
)
|
||||
return;
|
||||
updatingBudgetRef.current = true;
|
||||
setUpdatingBudgetId(record.id);
|
||||
try {
|
||||
// Reuse the edit upsert contract with raw quotas, without currency conversion.
|
||||
const res = await API.put(
|
||||
'/api/org/budget',
|
||||
{
|
||||
subject_type: record.subject_type,
|
||||
subject_id: record.subject_id,
|
||||
cycle_type: record.cycle_type,
|
||||
cycle_quota: record.cycle_quota,
|
||||
total_quota: record.total_quota,
|
||||
custom_cycle_days: record.custom_cycle_days || 0,
|
||||
over_limit_action: record.over_limit_action,
|
||||
status: enabled ? 1 : 2,
|
||||
},
|
||||
{ headers: getOrgHeaders(orgId), skipErrorHandler: true },
|
||||
);
|
||||
if (res.data.success) {
|
||||
Toast.success({
|
||||
content: t('状态已更新'),
|
||||
key: `org-budget-status-${orgId}-${record.id}`,
|
||||
});
|
||||
setList((items) =>
|
||||
items.map((item) =>
|
||||
item.id === record.id ? { ...item, status: enabled ? 1 : 2 } : item,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
showError(res.data.message);
|
||||
}
|
||||
} catch (err) {
|
||||
showError(err.message);
|
||||
} finally {
|
||||
updatingBudgetRef.current = false;
|
||||
setUpdatingBudgetId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (record) => {
|
||||
try {
|
||||
const res = await API.delete(`/api/org/budget/${record.id}`, {
|
||||
@@ -268,30 +306,32 @@ const OrgBudgets = ({ orgId }) => {
|
||||
}
|
||||
};
|
||||
|
||||
// 列宽与渲染对齐原型 EnterpriseBudget(300/110/260/120/120/130,scroll.x 1040);取值与判定沿用原逻辑
|
||||
const columns = [
|
||||
{
|
||||
title: t('主体'),
|
||||
width: 300,
|
||||
render: (_, r) => {
|
||||
const subj = subjectOf(r);
|
||||
return (
|
||||
<span>
|
||||
<Tag color='blue' size='small'>
|
||||
<div className='oc-budget-subject'>
|
||||
<Tag color='gold' bordered={false}>
|
||||
{subjectTypeLabel(t, r.subject_type)}
|
||||
</Tag>{' '}
|
||||
<span className={subj.exists ? '' : 'text-gray-400 line-through'}>
|
||||
</Tag>
|
||||
<span className={subj.exists ? '' : 'oc-muted line-through'}>
|
||||
{subj.name}
|
||||
</span>{' '}
|
||||
</span>
|
||||
{!subj.exists && (
|
||||
<Tag color='red' size='small'>
|
||||
<Tag color='red' bordered={false}>
|
||||
{t('主体已删除')}
|
||||
</Tag>
|
||||
)}
|
||||
{subj.exists && subj.disabled && (
|
||||
<Tag color='grey' size='small'>
|
||||
<Tag color='grey' bordered={false}>
|
||||
{t('已停用')}
|
||||
</Tag>
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
@@ -299,31 +339,32 @@ const OrgBudgets = ({ orgId }) => {
|
||||
title: t('周期'),
|
||||
dataIndex: 'cycle_type',
|
||||
render: (v) => cycleLabel(t, v),
|
||||
width: 90,
|
||||
width: 110,
|
||||
},
|
||||
{
|
||||
title: t('周期用量 / cap'),
|
||||
width: 220,
|
||||
title: t('周期用量 / 上限'),
|
||||
width: 260,
|
||||
render: (_, r) => {
|
||||
if (!r.cycle_quota)
|
||||
return <span className='text-xs text-gray-400'>{t('不限')}</span>;
|
||||
return <span className='oc-budget-unlimited'>{t('不限')}</span>;
|
||||
const pct = Math.min(
|
||||
100,
|
||||
Math.round((r.used_in_cycle / r.cycle_quota) * 100),
|
||||
);
|
||||
return (
|
||||
<div>
|
||||
<div className='oc-budget-usage'>
|
||||
<Progress
|
||||
percent={pct}
|
||||
size='small'
|
||||
showInfo={false}
|
||||
stroke={pct >= 90 ? 'var(--ui-color-danger)' : undefined}
|
||||
/>
|
||||
<span className='text-xs text-gray-500'>
|
||||
<span>
|
||||
{renderQuota(r.used_in_cycle, 2)}
|
||||
{r.reserved_in_cycle > 0
|
||||
? ` (+${renderQuota(r.reserved_in_cycle, 2)} ${t('预留')})`
|
||||
: ''}
|
||||
{' / '}
|
||||
<i>/</i>
|
||||
{renderQuota(r.cycle_quota, 2)}
|
||||
</span>
|
||||
</div>
|
||||
@@ -333,36 +374,37 @@ const OrgBudgets = ({ orgId }) => {
|
||||
{
|
||||
title: t('超限动作'),
|
||||
dataIndex: 'over_limit_action',
|
||||
// 90px 是按中文两字标签定的,泰语最短的地道说法 "เมื่อเกินขีดจำกัด" 也要 ~150px;
|
||||
// 这一列没有更短的译法,只能让列让位(其余列头已在 kt_overrides_th.json 收敛用词)
|
||||
width: 150,
|
||||
// 原型 120px;表头 nowrap,较长译文(如泰语)由表格自动撑开列宽,不截断
|
||||
width: 120,
|
||||
render: () => t('拦截'),
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
dataIndex: 'status',
|
||||
width: 80,
|
||||
render: (v) => (
|
||||
<Tag color={v === 1 ? 'green' : 'grey'}>
|
||||
{v === 1 ? t('启用') : t('停用')}
|
||||
</Tag>
|
||||
width: 120,
|
||||
render: (v, record) => (
|
||||
<Switch
|
||||
size='small'
|
||||
checked={v === 1}
|
||||
checkedChildren={t('启用')}
|
||||
unCheckedChildren={t('禁用')}
|
||||
aria-label={`${subjectOf(record).name} ${t('状态')}`}
|
||||
loading={updatingBudgetId === record.id}
|
||||
disabled={updatingBudgetId !== null || !subjectOf(record).exists}
|
||||
onChange={(checked) => handleStatusChange(record, checked)}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
width: 160,
|
||||
width: 130,
|
||||
render: (_, r) => {
|
||||
const subj = subjectOf(r);
|
||||
return (
|
||||
<div className='flex gap-1'>
|
||||
<OrgRowActions>
|
||||
{/* 主体已删除的孤儿行改不了(后端 upsert 会因主体不存在报错),只留删除 */}
|
||||
{subj.exists && (
|
||||
<Button
|
||||
size='small'
|
||||
theme='light'
|
||||
icon={<Pencil size={14} />}
|
||||
onClick={() => openEdit(r)}
|
||||
>
|
||||
<Button type='link' onClick={() => openEdit(r)}>
|
||||
{t('编辑')}
|
||||
</Button>
|
||||
)}
|
||||
@@ -373,31 +415,36 @@ const OrgBudgets = ({ orgId }) => {
|
||||
)}
|
||||
onConfirm={() => handleDelete(r)}
|
||||
>
|
||||
<Button
|
||||
size='small'
|
||||
theme='light'
|
||||
type='danger'
|
||||
icon={<Trash2 size={14} />}
|
||||
>
|
||||
<Button type='link' danger>
|
||||
{t('删除')}
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</div>
|
||||
</OrgRowActions>
|
||||
);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div className='uc-page'>
|
||||
<div className='uc-page oc-page'>
|
||||
<OrgPageHeader
|
||||
title={t('预算中心')}
|
||||
subtitle={t('部门 / 项目 / 成员的额度上限')}
|
||||
actions={
|
||||
subtitle={t('管理部门、项目与成员的额度上限')}
|
||||
/>
|
||||
|
||||
{/* 原型 .eb-policy 说明条:替换原“预算实时拦截未生效”警告与 cap 说明两条 Banner */}
|
||||
<div className='oc-policy' role='note'>
|
||||
{t(
|
||||
'预算只设置「最多能花多少」的上限,不增加企业真实额度池;提额仅提高上限。',
|
||||
)}
|
||||
</div>
|
||||
|
||||
<OrgListCard
|
||||
title={t('预算列表')}
|
||||
tools={
|
||||
<>
|
||||
<Button
|
||||
theme='light'
|
||||
icon={<RefreshCw size={14} />}
|
||||
icon={<RefreshCw size={16} />}
|
||||
loading={loading}
|
||||
onClick={fetchAll}
|
||||
>
|
||||
@@ -405,51 +452,27 @@ const OrgBudgets = ({ orgId }) => {
|
||||
</Button>
|
||||
<Button
|
||||
theme='solid'
|
||||
icon={<Plus size={14} />}
|
||||
icon={<Plus size={16} />}
|
||||
onClick={openCreate}
|
||||
>
|
||||
{t('配置预算')}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
{enforcementEnabled === false && (
|
||||
<Banner
|
||||
type='warning'
|
||||
closeIcon={null}
|
||||
description={
|
||||
engineEnabled === false
|
||||
? t(
|
||||
'预算引擎已被平台全局急停:所有企业的预算拦截与用量统计均暂停,恢复需平台管理员开启系统开关,与本企业配置无关。',
|
||||
)
|
||||
: t(
|
||||
'预算实时拦截当前未生效:这里的配置只会被保存,不会拦截超额调用,也不会累计用量统计。请联系平台管理员开启本企业的「预算实时拦截」功能。',
|
||||
)
|
||||
}
|
||||
className='mb-4'
|
||||
/>
|
||||
)}
|
||||
<Banner
|
||||
type='info'
|
||||
closeIcon={null}
|
||||
description={t(
|
||||
'预算只设置「最多能花多少」的上限(cap),不增加企业真实额度池;提额仅提高上限。',
|
||||
)}
|
||||
className='mb-4'
|
||||
/>
|
||||
|
||||
<OrgPanel padding='p-2'>
|
||||
>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={list}
|
||||
loading={loading}
|
||||
pagination={false}
|
||||
rowKey='id'
|
||||
scroll={{ x: 1040 }}
|
||||
/>
|
||||
</OrgPanel>
|
||||
</OrgListCard>
|
||||
|
||||
<OrgModal
|
||||
className='oc-dialog oc-budget-dialog'
|
||||
width={560}
|
||||
title={editing ? t('编辑预算') : t('配置预算')}
|
||||
visible={modalVisible}
|
||||
onOk={submit}
|
||||
@@ -459,11 +482,11 @@ const OrgBudgets = ({ orgId }) => {
|
||||
>
|
||||
<Form getFormApi={setFormApi}>
|
||||
{editing ? (
|
||||
<div className='mb-3'>
|
||||
<Tag color='blue' size='small'>
|
||||
<div className='oc-budget-edit-subject'>
|
||||
<Tag color='gold' bordered={false}>
|
||||
{subjectTypeLabel(t, editing.subject_type)}
|
||||
</Tag>{' '}
|
||||
{subjectOf(editing).name}
|
||||
</Tag>
|
||||
<strong>{subjectOf(editing).name}</strong>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
|
||||
Reference in New Issue
Block a user