用户生成资源容量管控
This commit is contained in:
@@ -1,14 +1,55 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import {
|
||||
Button, Card, Checkbox, Form, Input, InputNumber, message, Modal, Popconfirm, Select, Space, Switch, Table, Tabs, Tag, Typography,
|
||||
Button, Card, Checkbox, Form, Input, InputNumber, message, Modal, Popconfirm, Progress, Select, Space, Switch, Table, Tabs, Tag, Typography,
|
||||
} from 'antd';
|
||||
import {
|
||||
UserOutlined, WalletOutlined, SearchOutlined, StopOutlined, CheckCircleOutlined, PlusOutlined, MenuOutlined, LockOutlined, SettingOutlined, SaveOutlined,
|
||||
UserOutlined, WalletOutlined, SearchOutlined, StopOutlined, CheckCircleOutlined, PlusOutlined, MenuOutlined, LockOutlined, SettingOutlined, SaveOutlined, DatabaseOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { getAdminUsers, adjustCredits, toggleUserStatus, createUser, updateUserMenus, getMenuConfigs, resetUserPassword, getSystemConfigs, updateSystemConfig, updateFrontendUserKind } from '../api';
|
||||
import type { AdminUser, SystemConfig } from '../types';
|
||||
import {
|
||||
adjustCredits,
|
||||
createUser,
|
||||
deleteUserResourceCapacity,
|
||||
getAdminUsers,
|
||||
getMenuConfigs,
|
||||
getSystemConfigs,
|
||||
getUserResourceCapacity,
|
||||
resetUserPassword,
|
||||
saveUserResourceCapacity,
|
||||
toggleUserStatus,
|
||||
updateFrontendUserKind,
|
||||
updateSystemConfig,
|
||||
updateUserMenus,
|
||||
} from '../api';
|
||||
import type { AdminUser, AdminUserResourceCapacityOut, ResourceCapacityUnit, ResourceCapacityUsage, SystemConfig } from '../types';
|
||||
import { formatDate } from '../utils/formatDate';
|
||||
|
||||
const capacityUnitOptions: { value: ResourceCapacityUnit; label: string }[] = [
|
||||
{ value: 'MB', label: 'MB(1024 × 1024 字节)' },
|
||||
{ value: 'GB', label: 'GB(1024 × 1024 × 1024 字节)' },
|
||||
{ value: 'TB', label: 'TB(1024 × 1024 × 1024 × 1024 字节)' },
|
||||
];
|
||||
|
||||
function formatBytes(bytes?: number | null): string {
|
||||
if (bytes === null || bytes === undefined) return '-';
|
||||
const value = Number(bytes || 0);
|
||||
if (value < 1024) return `${value} B`;
|
||||
const units = ['KB', 'MB', 'GB', 'TB', 'PB'];
|
||||
let size = value;
|
||||
let unitIndex = -1;
|
||||
do {
|
||||
size /= 1024;
|
||||
unitIndex += 1;
|
||||
} while (size >= 1024 && unitIndex < units.length - 1);
|
||||
return `${size.toFixed(size >= 100 ? 0 : size >= 10 ? 1 : 2)} ${units[unitIndex]}`;
|
||||
}
|
||||
|
||||
function capacitySourceLabel(capacity?: ResourceCapacityUsage | null): string {
|
||||
if (!capacity || !capacity.enabled) return '未开启';
|
||||
if (capacity.source === 'user') return '个人';
|
||||
if (capacity.source === 'global') return '全局';
|
||||
return '未开启';
|
||||
}
|
||||
|
||||
const AdminUsers: React.FC = () => {
|
||||
const [users, setUsers] = useState<AdminUser[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
@@ -22,9 +63,13 @@ const AdminUsers: React.FC = () => {
|
||||
const [allMenus, setAllMenus] = useState<any[]>([]);
|
||||
const [checkedMenus, setCheckedMenus] = useState<string[]>([]);
|
||||
const [resetPwdModal, setResetPwdModal] = useState<{ open: boolean; user: AdminUser | null }>({ open: false, user: null });
|
||||
const [capacityModal, setCapacityModal] = useState<{ open: boolean; user: AdminUser | null; detail: AdminUserResourceCapacityOut | null }>({ open: false, user: null, detail: null });
|
||||
const [capacityLoading, setCapacityLoading] = useState(false);
|
||||
const [capacitySaving, setCapacitySaving] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
const [createForm] = Form.useForm();
|
||||
const [resetPwdForm] = Form.useForm();
|
||||
const [capacityForm] = Form.useForm();
|
||||
|
||||
const [page, setPage] = useState(1);
|
||||
const [pageSize, setPageSize] = useState(20);
|
||||
@@ -66,17 +111,17 @@ const AdminUsers: React.FC = () => {
|
||||
setConfigSaving(true);
|
||||
for (const config of creditConfigs) {
|
||||
const newVal = values[config.key];
|
||||
if (newVal !== undefined && newVal !== config.value) {
|
||||
await updateSystemConfig(config.id, newVal ?? '');
|
||||
if (newVal !== undefined && String(newVal) !== config.value) {
|
||||
await updateSystemConfig(config.id, String(newVal ?? ''));
|
||||
}
|
||||
}
|
||||
message.success('积分配置已保存');
|
||||
const configs = await getSystemConfigs();
|
||||
setCreditConfigs(configs.filter(c => c.key.startsWith('user_') && c.key.includes('credits')));
|
||||
setConfigSaving(false);
|
||||
} catch (e: any) {
|
||||
setConfigSaving(false);
|
||||
message.error(e?.message || '保存失败');
|
||||
} finally {
|
||||
setConfigSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -142,7 +187,65 @@ const AdminUsers: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
// Build structured menu display: groups with children, and top-level pages
|
||||
const openCapacityModal = async (user: AdminUser) => {
|
||||
setCapacityLoading(true);
|
||||
setCapacityModal({ open: true, user, detail: null });
|
||||
try {
|
||||
const detail = await getUserResourceCapacity(user.id);
|
||||
const initialConfig = detail.userConfig;
|
||||
capacityForm.setFieldsValue({
|
||||
enabled: initialConfig?.enabled ?? false,
|
||||
limitValue: initialConfig?.limitValue ?? detail.effective.limitValue ?? detail.globalConfig.limitValue ?? '1.000',
|
||||
limitUnit: initialConfig?.limitUnit ?? detail.effective.limitUnit ?? detail.globalConfig.limitUnit ?? 'GB',
|
||||
});
|
||||
setCapacityModal({ open: true, user, detail });
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载容量配置失败');
|
||||
setCapacityModal({ open: false, user: null, detail: null });
|
||||
} finally {
|
||||
setCapacityLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveCapacity = async () => {
|
||||
const { user } = capacityModal;
|
||||
if (!user) return;
|
||||
try {
|
||||
const values = await capacityForm.validateFields();
|
||||
setCapacitySaving(true);
|
||||
await saveUserResourceCapacity(user.id, {
|
||||
enabled: !!values.enabled,
|
||||
limitValue: String(values.limitValue ?? '1.000'),
|
||||
limitUnit: values.limitUnit || 'GB',
|
||||
});
|
||||
message.success('用户容量配置已保存');
|
||||
setCapacityModal({ open: false, user: null, detail: null });
|
||||
capacityForm.resetFields();
|
||||
load();
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '保存失败');
|
||||
} finally {
|
||||
setCapacitySaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRestoreGlobalCapacity = async () => {
|
||||
const { user } = capacityModal;
|
||||
if (!user) return;
|
||||
try {
|
||||
setCapacitySaving(true);
|
||||
await deleteUserResourceCapacity(user.id);
|
||||
message.success('已恢复为全局容量配置');
|
||||
setCapacityModal({ open: false, user: null, detail: null });
|
||||
capacityForm.resetFields();
|
||||
load();
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '恢复失败');
|
||||
} finally {
|
||||
setCapacitySaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const menuGroups = allMenus.filter((m: any) => (m.menu_type ?? m.menuType) === 'group');
|
||||
const menuPages = allMenus.filter((m: any) => (m.menu_type ?? m.menuType) !== 'group');
|
||||
const childMap: Record<string, any[]> = {};
|
||||
@@ -229,6 +332,36 @@ const AdminUsers: React.FC = () => {
|
||||
title: '前台归类', dataIndex: 'frontendUserKind', width: 110,
|
||||
render: (v: string) => <Tag color={v === 'internal' ? 'geekblue' : 'default'}>{v === 'internal' ? '内部用户' : '外部用户'}</Tag>,
|
||||
}] : []),
|
||||
...(!isAdminTab ? [{
|
||||
title: '资源容量', dataIndex: 'resourceCapacity', width: 230,
|
||||
render: (capacity: ResourceCapacityUsage | null | undefined) => {
|
||||
const usedText = formatBytes(capacity?.usedBytes || 0);
|
||||
if (!capacity || !capacity.enabled) {
|
||||
return (
|
||||
<div>
|
||||
<Space size={6} style={{ marginBottom: 4 }}>
|
||||
<Tag>未开启</Tag>
|
||||
<Typography.Text type="secondary" style={{ fontSize: 12 }}>已用 {usedText}</Typography.Text>
|
||||
</Space>
|
||||
<Progress percent={0} size="small" showInfo={false} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
const percent = Math.min(Number(capacity.usagePercent || 0), 100);
|
||||
return (
|
||||
<div>
|
||||
<Space size={6} style={{ marginBottom: 4 }}>
|
||||
<Tag color={capacity.source === 'user' ? 'blue' : 'purple'}>{capacitySourceLabel(capacity)}</Tag>
|
||||
{capacity.exceeded && <Tag color="red">已超额</Tag>}
|
||||
</Space>
|
||||
<Progress percent={percent} size="small" status={capacity.exceeded ? 'exception' : 'active'} />
|
||||
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
|
||||
{usedText} / {formatBytes(capacity.totalBytes)},可用 {formatBytes(capacity.availableBytes)}
|
||||
</Typography.Text>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
}] : []),
|
||||
{
|
||||
title: '状态', dataIndex: 'isActive', width: 80,
|
||||
render: (v: boolean) => (
|
||||
@@ -244,15 +377,21 @@ const AdminUsers: React.FC = () => {
|
||||
render: (v: string) => <Typography.Text type="secondary" style={{ fontSize: 12 }}>{formatDate(v)}</Typography.Text>,
|
||||
},
|
||||
{
|
||||
title: '操作', key: 'action', width: 320, fixed: 'right' as const,
|
||||
title: '操作', key: 'action', width: 390, fixed: 'right' as const,
|
||||
render: (_: any, r: AdminUser) => (
|
||||
<Space size={4}>
|
||||
<Space size={4} wrap>
|
||||
{!isAdminTab && (
|
||||
<Button type="link" size="small" icon={<WalletOutlined />}
|
||||
onClick={() => { setCreditModal({ open: true, user: r }); form.resetFields(); }}>
|
||||
调整积分
|
||||
</Button>
|
||||
)}
|
||||
{!isAdminTab && (
|
||||
<Button type="link" size="small" icon={<DatabaseOutlined />}
|
||||
onClick={() => openCapacityModal(r)}>
|
||||
容量设置
|
||||
</Button>
|
||||
)}
|
||||
{!isAdminTab && r.frontendUserKind !== 'internal' && (
|
||||
<Button type="link" size="small" onClick={() => handleUpdateFrontendKind(r, 'internal')}>设为内部</Button>
|
||||
)}
|
||||
@@ -338,7 +477,6 @@ const AdminUsers: React.FC = () => {
|
||||
</Card>
|
||||
|
||||
<Card variant="outlined" style={{ borderRadius: 12, border: '1px solid #f0f0f5' }}>
|
||||
{/* Search bar */}
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 16 }}>
|
||||
<div style={{ display: 'flex', gap: 12 }}>
|
||||
<Input
|
||||
@@ -393,11 +531,10 @@ const AdminUsers: React.FC = () => {
|
||||
showSizeChanger: true,
|
||||
showTotal: (t) => `共 ${t} 个用户`,
|
||||
}}
|
||||
scroll={{ x: 1000 }}
|
||||
scroll={{ x: 1280 }}
|
||||
/>
|
||||
</Card>
|
||||
|
||||
{/* Adjust Credits Modal */}
|
||||
<Modal
|
||||
title={<Space><WalletOutlined />调整积分 - {creditModal.user?.username}</Space>}
|
||||
open={creditModal.open}
|
||||
@@ -428,7 +565,77 @@ const AdminUsers: React.FC = () => {
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
{/* Create User Modal */}
|
||||
<Modal
|
||||
title={<Space><DatabaseOutlined />容量设置 - {capacityModal.user?.username}</Space>}
|
||||
open={capacityModal.open}
|
||||
confirmLoading={capacitySaving}
|
||||
onOk={handleSaveCapacity}
|
||||
onCancel={() => { setCapacityModal({ open: false, user: null, detail: null }); capacityForm.resetFields(); }}
|
||||
okText="保存个人配置" cancelText="取消" width={560}
|
||||
>
|
||||
<Card loading={capacityLoading} variant="outlined" style={{ marginBottom: 16 }}>
|
||||
<Space direction="vertical" size={6} style={{ width: '100%' }}>
|
||||
<Typography.Text type="secondary">
|
||||
当前生效来源:{capacitySourceLabel(capacityModal.detail?.effective)}
|
||||
{capacityModal.detail?.effective.hasUserConfig ? '(已单独设置)' : '(未单独设置)'}
|
||||
</Typography.Text>
|
||||
<Typography.Text>
|
||||
已用:{formatBytes(capacityModal.detail?.effective.usedBytes)};
|
||||
总量:{formatBytes(capacityModal.detail?.effective.totalBytes)};
|
||||
可用:{formatBytes(capacityModal.detail?.effective.availableBytes)}
|
||||
</Typography.Text>
|
||||
{capacityModal.detail?.effective.enabled && (
|
||||
<Progress
|
||||
percent={Math.min(Number(capacityModal.detail.effective.usagePercent || 0), 100)}
|
||||
status={capacityModal.detail.effective.exceeded ? 'exception' : 'active'}
|
||||
/>
|
||||
)}
|
||||
</Space>
|
||||
</Card>
|
||||
<Form form={capacityForm} layout="vertical">
|
||||
<Form.Item
|
||||
name="enabled"
|
||||
label="启用个人容量限制"
|
||||
valuePropName="checked"
|
||||
extra="保存后会生成用户个人配置,优先级高于全局;关闭并保存表示该用户个人明确不限制,不再走全局。"
|
||||
>
|
||||
<Switch checkedChildren="开启" unCheckedChildren="关闭" />
|
||||
</Form.Item>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 180px', gap: 16 }}>
|
||||
<Form.Item
|
||||
name="limitValue"
|
||||
label="容量数值"
|
||||
rules={[{ required: true, message: '请输入容量数值' }]}
|
||||
extra="最小为1,不能为负数,最多支持3位小数。"
|
||||
>
|
||||
<InputNumber min={1} precision={3} style={{ width: '100%' }} size="large" placeholder="例如 10.500" />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="limitUnit"
|
||||
label="容量单位"
|
||||
rules={[{ required: true, message: '请选择容量单位' }]}
|
||||
extra="MB / GB / TB 固定枚举"
|
||||
>
|
||||
<Select size="large" options={capacityUnitOptions} />
|
||||
</Form.Item>
|
||||
</div>
|
||||
</Form>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 8 }}>
|
||||
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
|
||||
恢复全局设置会删除该用户个人配置,让用户重新按全局规则判断。
|
||||
</Typography.Text>
|
||||
<Popconfirm
|
||||
title="确定恢复为全局容量配置?"
|
||||
onConfirm={handleRestoreGlobalCapacity}
|
||||
disabled={!capacityModal.detail?.hasUserConfig}
|
||||
>
|
||||
<Button disabled={!capacityModal.detail?.hasUserConfig} loading={capacitySaving}>
|
||||
恢复全局设置
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
title={<Space><UserOutlined />创建用户</Space>}
|
||||
open={createModal}
|
||||
@@ -476,7 +683,6 @@ const AdminUsers: React.FC = () => {
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
{/* Menu Permission Modal */}
|
||||
<Modal
|
||||
title={<Space><MenuOutlined />菜单权限 - {menuModal.user?.username} ({menuModal.user?.userType === 'admin' ? '后台菜单' : '前台菜单'})</Space>}
|
||||
open={menuModal.open}
|
||||
@@ -492,14 +698,12 @@ const AdminUsers: React.FC = () => {
|
||||
<div style={{ padding: '12px 16px', background: '#f8fafc', borderRadius: 8, maxHeight: 400, overflow: 'auto' }}>
|
||||
<Checkbox.Group value={checkedMenus} onChange={(vals) => setCheckedMenus(vals as string[])}>
|
||||
<Space direction="vertical" size={8} style={{ width: '100%' }}>
|
||||
{/* Top-level pages */}
|
||||
{topLevelPages.map((m: any) => (
|
||||
<Checkbox key={m.path} value={m.path} style={{ width: '100%' }}>
|
||||
{m.label}
|
||||
<Typography.Text type="secondary" style={{ fontSize: 12, marginLeft: 8 }}>{m.path}</Typography.Text>
|
||||
</Checkbox>
|
||||
))}
|
||||
{/* Groups with their children */}
|
||||
{menuGroups.map((g: any) => {
|
||||
const children = childMap[g.id] || [];
|
||||
if (children.length === 0) return null;
|
||||
@@ -524,7 +728,6 @@ const AdminUsers: React.FC = () => {
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* Reset Password Modal */}
|
||||
<Modal
|
||||
title={<Space><LockOutlined />重置密码 - {resetPwdModal.user?.username}</Space>}
|
||||
open={resetPwdModal.open}
|
||||
|
||||
Reference in New Issue
Block a user