Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,373 @@
|
||||
/*
|
||||
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, { useState, useEffect, useContext } from 'react';
|
||||
import { Button, Switch, Row, Col } from '@douyinfe/semi-ui';
|
||||
import {
|
||||
LayoutGrid,
|
||||
MessageSquare,
|
||||
BarChart2,
|
||||
User,
|
||||
Shield,
|
||||
} from 'lucide-react';
|
||||
import { API, showSuccess, showError } from '../../../../helpers';
|
||||
import { StatusContext } from '../../../../context/Status';
|
||||
import { useUserPermissions } from '../../../../hooks/common/useUserPermissions';
|
||||
import { useSidebar } from '../../../../hooks/common/useSidebar';
|
||||
|
||||
const SidebarSection = ({ t }) => {
|
||||
const [statusState] = useContext(StatusContext);
|
||||
const [sidebarLoading, setSidebarLoading] = useState(false);
|
||||
const [sidebarModulesUser, setSidebarModulesUser] = useState({
|
||||
chat: { enabled: true, playground: true, chat: true },
|
||||
console: {
|
||||
enabled: true,
|
||||
detail: true,
|
||||
token: true,
|
||||
log: true,
|
||||
midjourney: true,
|
||||
task: true,
|
||||
monitor: true,
|
||||
},
|
||||
personal: { enabled: true, topup: true, personal: true },
|
||||
admin: {
|
||||
enabled: true,
|
||||
analytics: true,
|
||||
channel: true,
|
||||
models: true,
|
||||
redemption: true,
|
||||
user: true,
|
||||
subscription: true,
|
||||
'admin-token': true,
|
||||
organizations: true,
|
||||
order: true,
|
||||
invoice: true,
|
||||
setting: true,
|
||||
},
|
||||
});
|
||||
const [adminConfig, setAdminConfig] = useState(null);
|
||||
|
||||
const {
|
||||
hasSidebarSettingsPermission,
|
||||
isSidebarSectionAllowed,
|
||||
isSidebarModuleAllowed,
|
||||
} = useUserPermissions();
|
||||
|
||||
const { refreshUserConfig } = useSidebar();
|
||||
|
||||
const handleSectionChange = (sectionKey) => (checked) => {
|
||||
setSidebarModulesUser((prev) => ({
|
||||
...prev,
|
||||
[sectionKey]: { ...prev[sectionKey], enabled: checked },
|
||||
}));
|
||||
};
|
||||
|
||||
const handleModuleChange = (sectionKey, moduleKey) => (checked) => {
|
||||
setSidebarModulesUser((prev) => ({
|
||||
...prev,
|
||||
[sectionKey]: { ...prev[sectionKey], [moduleKey]: checked },
|
||||
}));
|
||||
};
|
||||
|
||||
const saveSidebarSettings = async () => {
|
||||
setSidebarLoading(true);
|
||||
try {
|
||||
const res = await API.put('/api/user/self', {
|
||||
sidebar_modules: JSON.stringify(sidebarModulesUser),
|
||||
});
|
||||
if (res.data.success) {
|
||||
showSuccess(t('侧边栏设置保存成功'));
|
||||
await refreshUserConfig();
|
||||
} else {
|
||||
showError(res.data.message);
|
||||
}
|
||||
} catch (error) {
|
||||
showError(t('保存失败'));
|
||||
}
|
||||
setSidebarLoading(false);
|
||||
};
|
||||
|
||||
const resetSidebarModules = () => {
|
||||
setSidebarModulesUser({
|
||||
chat: { enabled: true, playground: true, chat: true },
|
||||
console: {
|
||||
enabled: true,
|
||||
detail: true,
|
||||
token: true,
|
||||
log: true,
|
||||
midjourney: true,
|
||||
task: true,
|
||||
},
|
||||
personal: { enabled: true, topup: true, personal: true },
|
||||
admin: {
|
||||
enabled: true,
|
||||
channel: true,
|
||||
models: true,
|
||||
redemption: true,
|
||||
user: true,
|
||||
setting: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const loadSidebarConfigs = async () => {
|
||||
try {
|
||||
if (statusState?.status?.SidebarModulesAdmin) {
|
||||
setAdminConfig(JSON.parse(statusState.status.SidebarModulesAdmin));
|
||||
}
|
||||
const userRes = await API.get('/api/user/self');
|
||||
if (userRes.data.success && userRes.data.data.sidebar_modules) {
|
||||
setSidebarModulesUser(JSON.parse(userRes.data.data.sidebar_modules));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载边栏配置失败:', error);
|
||||
}
|
||||
};
|
||||
loadSidebarConfigs();
|
||||
}, [statusState]);
|
||||
|
||||
const isAllowedByAdmin = (sectionKey, moduleKey = null) => {
|
||||
if (!adminConfig) return true;
|
||||
if (moduleKey) {
|
||||
return (
|
||||
adminConfig[sectionKey]?.enabled && adminConfig[sectionKey]?.[moduleKey]
|
||||
);
|
||||
}
|
||||
return adminConfig[sectionKey]?.enabled;
|
||||
};
|
||||
|
||||
// 区域配置
|
||||
const sectionConfigs = [
|
||||
{
|
||||
key: 'chat',
|
||||
title: t('聊天区域'),
|
||||
description: t('操练场和聊天功能'),
|
||||
icon: MessageSquare,
|
||||
gradient: 'from-blue-500 to-cyan-500',
|
||||
modules: [
|
||||
{
|
||||
key: 'playground',
|
||||
title: t('操练场'),
|
||||
description: t('AI模型测试环境'),
|
||||
},
|
||||
{ key: 'chat', title: t('聊天'), description: t('聊天会话管理') },
|
||||
],
|
||||
},
|
||||
{
|
||||
key: 'console',
|
||||
title: t('控制台区域'),
|
||||
description: t('数据管理和日志查看'),
|
||||
icon: BarChart2,
|
||||
gradient: 'from-emerald-500 to-teal-500',
|
||||
modules: [
|
||||
{ key: 'detail', title: t('数据看板'), description: t('系统数据统计') },
|
||||
{ key: 'token', title: t('令牌管理'), description: t('API令牌管理') },
|
||||
{ key: 'log', title: t('使用日志'), description: t('API使用记录') },
|
||||
{
|
||||
key: 'midjourney',
|
||||
title: t('绘图日志'),
|
||||
description: t('绘图任务记录'),
|
||||
},
|
||||
{ key: 'task', title: t('任务日志'), description: t('系统任务记录') },
|
||||
],
|
||||
},
|
||||
{
|
||||
key: 'personal',
|
||||
title: t('个人中心区域'),
|
||||
description: t('用户个人功能'),
|
||||
icon: User,
|
||||
gradient: 'from-brand-from to-pink-500',
|
||||
modules: [
|
||||
{ key: 'topup', title: t('钱包管理'), description: t('余额充值管理') },
|
||||
{
|
||||
key: 'personal',
|
||||
title: t('个人设置'),
|
||||
description: t('个人信息设置'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
key: 'admin',
|
||||
title: t('管理员区域'),
|
||||
description: t('系统管理功能'),
|
||||
icon: Shield,
|
||||
gradient: 'from-orange-500 to-red-500',
|
||||
modules: [
|
||||
{ key: 'channel', title: t('渠道管理'), description: t('API渠道配置') },
|
||||
{ key: 'models', title: t('模型管理'), description: t('AI模型配置') },
|
||||
{
|
||||
key: 'redemption',
|
||||
title: t('兑换码管理'),
|
||||
description: t('兑换码生成管理'),
|
||||
},
|
||||
{ key: 'user', title: t('用户管理'), description: t('用户账户管理') },
|
||||
{
|
||||
key: 'setting',
|
||||
title: t('系统设置'),
|
||||
description: t('系统参数配置'),
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
.filter((section) => isSidebarSectionAllowed(section.key))
|
||||
.map((section) => ({
|
||||
...section,
|
||||
modules: section.modules.filter((module) =>
|
||||
isSidebarModuleAllowed(section.key, module.key),
|
||||
),
|
||||
}))
|
||||
.filter(
|
||||
(section) => section.modules.length > 0 && isAllowedByAdmin(section.key),
|
||||
);
|
||||
|
||||
if (!hasSidebarSettingsPermission()) {
|
||||
return (
|
||||
<div className='flex items-center justify-center h-64 text-gray-500'>
|
||||
{t('您没有权限访问此设置')}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* 页面标题 */}
|
||||
<div className='flex items-center justify-between mb-6'>
|
||||
<div className='flex items-center gap-3'>
|
||||
<div className='w-10 h-10 rounded-xl bg-gradient-to-br from-cyan-500 to-blue-600 flex items-center justify-center shadow-lg shadow-cyan-500/25'>
|
||||
<LayoutGrid className='w-5 h-5 text-white' />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className='text-xl font-bold text-gray-900 dark:text-white'>
|
||||
{t('边栏设置')}
|
||||
</h2>
|
||||
<p className='text-sm text-gray-500 dark:text-gray-400'>
|
||||
{t('个性化设置侧边栏的显示功能')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className='flex gap-2'>
|
||||
<Button
|
||||
type='tertiary'
|
||||
theme='light'
|
||||
onClick={resetSidebarModules}
|
||||
className='!rounded-lg !border !border-gray-200 dark:!border-neutral-700'
|
||||
style={{ color: '#4b5563' }}
|
||||
>
|
||||
{t('重置为默认')}
|
||||
</Button>
|
||||
<Button
|
||||
theme='solid'
|
||||
onClick={saveSidebarSettings}
|
||||
loading={sidebarLoading}
|
||||
className='!bg-gradient-to-r !from-cyan-500 !to-blue-500 !border-0 !rounded-lg'
|
||||
>
|
||||
{t('保存设置')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 区域设置列表 */}
|
||||
<div className='space-y-6'>
|
||||
{sectionConfigs.map((section) => {
|
||||
const Icon = section.icon;
|
||||
return (
|
||||
<div
|
||||
key={section.key}
|
||||
className='bg-white/50 dark:bg-gray-800/50 rounded-xl border border-gray-100 dark:border-gray-700/50 overflow-hidden'
|
||||
>
|
||||
{/* 区域标题和总开关 */}
|
||||
<div
|
||||
className={`flex justify-between items-center p-5 bg-gradient-to-r ${section.gradient} bg-opacity-5`}
|
||||
>
|
||||
<div className='flex items-center gap-3'>
|
||||
<div
|
||||
className={`w-10 h-10 rounded-lg bg-gradient-to-br ${section.gradient} flex items-center justify-center shadow-lg`}
|
||||
>
|
||||
<Icon className='w-5 h-5 text-white' />
|
||||
</div>
|
||||
<div>
|
||||
<h3 className='font-semibold text-gray-900 dark:text-white'>
|
||||
{section.title}
|
||||
</h3>
|
||||
<p className='text-sm text-gray-500 dark:text-gray-400'>
|
||||
{section.description}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Switch
|
||||
checked={sidebarModulesUser[section.key]?.enabled}
|
||||
onChange={handleSectionChange(section.key)}
|
||||
size='large'
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 功能模块网格 */}
|
||||
<div className='p-5'>
|
||||
<Row gutter={[12, 12]}>
|
||||
{section.modules
|
||||
.filter((module) =>
|
||||
isAllowedByAdmin(section.key, module.key),
|
||||
)
|
||||
.map((module) => (
|
||||
<Col key={module.key} xs={24} sm={12} md={8} lg={6}>
|
||||
<div
|
||||
className={`p-4 rounded-xl border transition-all ${
|
||||
sidebarModulesUser[section.key]?.enabled
|
||||
? 'border-gray-200 dark:border-gray-700 hover:border-blue-300 dark:hover:border-blue-700'
|
||||
: 'border-gray-100 dark:border-gray-800 opacity-50'
|
||||
}`}
|
||||
>
|
||||
<div className='flex justify-between items-center'>
|
||||
<div>
|
||||
<div className='font-medium text-sm text-gray-900 dark:text-white'>
|
||||
{module.title}
|
||||
</div>
|
||||
<div className='text-xs text-gray-500 dark:text-gray-400 mt-0.5'>
|
||||
{module.description}
|
||||
</div>
|
||||
</div>
|
||||
<Switch
|
||||
checked={
|
||||
sidebarModulesUser[section.key]?.[module.key]
|
||||
}
|
||||
onChange={handleModuleChange(
|
||||
section.key,
|
||||
module.key,
|
||||
)}
|
||||
size='small'
|
||||
disabled={
|
||||
!sidebarModulesUser[section.key]?.enabled
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Col>
|
||||
))}
|
||||
</Row>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default SidebarSection;
|
||||
Reference in New Issue
Block a user