293 lines
9.6 KiB
React
293 lines
9.6 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, { useContext, useMemo } from 'react';
|
||
import {
|
||
Routes,
|
||
Route,
|
||
Navigate,
|
||
useLocation,
|
||
useNavigate,
|
||
} from 'react-router-dom';
|
||
import { useTranslation } from 'react-i18next';
|
||
import {
|
||
Activity,
|
||
BarChart2,
|
||
CreditCard,
|
||
FileText,
|
||
Gift,
|
||
Receipt,
|
||
Settings,
|
||
} from 'lucide-react';
|
||
import { StatusContext } from '../../context/Status';
|
||
import PageBackground from '../../components/common/PageBackground';
|
||
import UserCenterSidebar from './UserCenterSidebar';
|
||
import UserDashboard from './UserDashboard';
|
||
|
||
import UsageRecord from './UsageRecord';
|
||
import Invite from './Invite';
|
||
import Invoice from './Invoice';
|
||
import TopUp from '../TopUp';
|
||
import PersonalSetting from '../../components/settings/PersonalSetting';
|
||
import ServiceDashboard from '../ServiceDashboard';
|
||
import WeyAiDownload from '../WeyAiDownload';
|
||
import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules';
|
||
|
||
// 移动端底部导航组件
|
||
const MobileBottomNav = ({ sidebarConfig }) => {
|
||
const { t } = useTranslation();
|
||
const location = useLocation();
|
||
const navigate = useNavigate();
|
||
|
||
const navItems = [
|
||
{
|
||
key: 'dashboard',
|
||
icon: BarChart2,
|
||
label: t('概览'),
|
||
path: '/user-center/dashboard',
|
||
},
|
||
{
|
||
key: 'usageRecord',
|
||
icon: FileText,
|
||
label: t('记录'),
|
||
path: '/user-center/usage-record',
|
||
},
|
||
{
|
||
key: 'serviceDashboard',
|
||
icon: Activity,
|
||
label: t('监控'),
|
||
path: '/user-center/service-dashboard',
|
||
},
|
||
{
|
||
key: 'invite',
|
||
icon: Gift,
|
||
label: t('邀请'),
|
||
path: '/user-center/invite',
|
||
},
|
||
{
|
||
key: 'topup',
|
||
icon: CreditCard,
|
||
label: t('充值'),
|
||
path: '/user-center/topup',
|
||
},
|
||
{
|
||
key: 'invoice',
|
||
icon: Receipt,
|
||
label: t('发票'),
|
||
path: '/user-center/invoice',
|
||
},
|
||
{
|
||
key: 'settings',
|
||
icon: Settings,
|
||
label: t('设置'),
|
||
path: '/user-center/settings',
|
||
},
|
||
];
|
||
|
||
const visibleNavItems = navItems.filter(
|
||
(item) =>
|
||
sidebarConfig?.enabled !== false && sidebarConfig?.[item.key] !== false,
|
||
);
|
||
|
||
if (visibleNavItems.length === 0) {
|
||
return null;
|
||
}
|
||
|
||
return (
|
||
<div className='md:hidden fixed bottom-0 left-0 right-0 z-50 bg-white/90 dark:bg-neutral-900/90 backdrop-blur-xl border-t border-gray-200/80 dark:border-neutral-800/80 safe-area-inset-bottom'>
|
||
<div className='flex items-center justify-around h-14'>
|
||
{visibleNavItems.map((item) => {
|
||
const isActive =
|
||
location.pathname === item.path ||
|
||
location.pathname.startsWith(`${item.path}/`);
|
||
const IconComponent = item.icon;
|
||
|
||
return (
|
||
<button
|
||
key={item.key}
|
||
onClick={() => navigate(item.path)}
|
||
className={`flex flex-col items-center justify-center flex-1 h-full transition-all ${
|
||
isActive ? 'scale-105' : 'opacity-60'
|
||
}`}
|
||
>
|
||
<div
|
||
className={`p-1.5 rounded-lg ${isActive ? 'bg-gradient-to-br from-blue-500 to-brand-to' : ''}`}
|
||
>
|
||
<IconComponent
|
||
size={20}
|
||
className={
|
||
isActive ? 'text-white' : 'text-gray-500 dark:text-gray-400'
|
||
}
|
||
/>
|
||
</div>
|
||
<span
|
||
className={`text-[10px] mt-0.5 font-medium ${
|
||
isActive
|
||
? 'text-gray-900 dark:text-white'
|
||
: 'text-gray-500 dark:text-gray-400'
|
||
}`}
|
||
>
|
||
{item.label}
|
||
</span>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
const UserCenter = () => {
|
||
const [statusState] = useContext(StatusContext);
|
||
|
||
// 跑马灯公告是否存在(用于动态计算顶部间距)
|
||
const hasMarquee = !!statusState?.status?.marquee_content?.trim();
|
||
const userCenterConfig = useMemo(
|
||
() =>
|
||
normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin)
|
||
.userCenter,
|
||
[statusState?.status?.SidebarModulesAdmin],
|
||
);
|
||
const userCenterRoutes = [
|
||
'dashboard',
|
||
'usageRecord',
|
||
'serviceDashboard',
|
||
'topup',
|
||
'invoice',
|
||
'invite',
|
||
'settings',
|
||
];
|
||
const routePathMap = {
|
||
dashboard: 'dashboard',
|
||
usageRecord: 'usage-record',
|
||
serviceDashboard: 'service-dashboard',
|
||
topup: 'topup',
|
||
invoice: 'invoice',
|
||
invite: 'invite',
|
||
settings: 'settings',
|
||
};
|
||
const firstVisiblePath =
|
||
userCenterConfig.enabled === false
|
||
? null
|
||
: routePathMap[
|
||
userCenterRoutes.find((key) => userCenterConfig[key] !== false)
|
||
] || null;
|
||
const firstVisibleRoute = firstVisiblePath
|
||
? `/user-center/${firstVisiblePath}`
|
||
: '/';
|
||
const guardUserCenterRoute = (key, element) => {
|
||
if (userCenterConfig.enabled === false || userCenterConfig[key] === false) {
|
||
return <Navigate to={firstVisibleRoute} replace />;
|
||
}
|
||
return element;
|
||
};
|
||
|
||
if (!firstVisiblePath) {
|
||
return <Navigate to='/' replace />;
|
||
}
|
||
|
||
return (
|
||
<div
|
||
className={`flex flex-col md:flex-row h-screen overflow-hidden bg-[var(--surface-bg-base)] transition-colors duration-300 ${hasMarquee ? 'pt-[96px] sm:pt-[104px]' : 'pt-14 sm:pt-16'}`}
|
||
>
|
||
{/* Sidebar - 仅桌面端 */}
|
||
<div className='hidden md:block flex-shrink-0 h-full'>
|
||
<UserCenterSidebar />
|
||
</div>
|
||
|
||
{/* Main Content */}
|
||
<div className='flex-1 h-full overflow-hidden relative pb-16 md:pb-0'>
|
||
{/* 统一页面背景层 —— 唯一背景来源,走 bg-glow-* 主题变量 */}
|
||
<PageBackground />
|
||
|
||
{/* 内容区域 */}
|
||
<div className='relative z-10 h-full overflow-y-auto uc-page-scrollbar'>
|
||
<Routes>
|
||
<Route
|
||
path='/'
|
||
element={<Navigate to={firstVisibleRoute} replace />}
|
||
/>
|
||
<Route
|
||
path='dashboard'
|
||
element={guardUserCenterRoute('dashboard', <UserDashboard />)}
|
||
/>
|
||
<Route
|
||
path='usage-record'
|
||
element={guardUserCenterRoute('usageRecord', <UsageRecord />)}
|
||
/>
|
||
{/* ServiceDashboard 组件内部有 min-h-screen pt-[88px],嵌入 UserCenter 后用 -mt-[64px] 部分抵消 pt,保留 24px 顶部间距(与其他页面 p-6 一致),[&>div]:min-h-0 避免撑高 */}
|
||
<Route
|
||
path='service-dashboard'
|
||
element={guardUserCenterRoute(
|
||
'serviceDashboard',
|
||
<div className='-mt-[64px] [&>div]:min-h-0'>
|
||
<ServiceDashboard />
|
||
</div>,
|
||
)}
|
||
/>
|
||
{/* TopUp 组件内部有 pt-[64px] 用于独立路由清除固定头部,UserCenter 已处理顶部偏移,需用 -mt 抵消。
|
||
* embedded:UserCenter shell 已提供 PageBackground,子页面不再渲染自身背景,避免双层叠加 */}
|
||
<Route
|
||
path='topup'
|
||
element={guardUserCenterRoute(
|
||
'topup',
|
||
<div className='-mt-[64px]'>
|
||
<TopUp embedded />
|
||
</div>,
|
||
)}
|
||
/>
|
||
{/* PersonalSetting 内部有 pt-16 sm:pt-20 用于独立路由清除固定头部,UserCenter 已处理顶部偏移,需用 -mt 抵消 */}
|
||
{/* 邀请返利 / 发票抬头:侧栏项、routePathMap、权限守卫此前都已配好,
|
||
唯独漏了这两个 <Route>,导致点进来是空白内容区。
|
||
手机端侧栏是 hidden md:block,底部导航是唯一入口,等于两个死路口。
|
||
两个组件根节点都是 p-4 sm:p-6、无自带顶部偏移,可直接嵌入。 */}
|
||
<Route
|
||
path='invite'
|
||
element={guardUserCenterRoute('invite', <Invite />)}
|
||
/>
|
||
<Route
|
||
path='invoice'
|
||
element={guardUserCenterRoute('invoice', <Invoice />)}
|
||
/>
|
||
<Route
|
||
path='settings'
|
||
element={guardUserCenterRoute(
|
||
'settings',
|
||
<div className='-mt-16 sm:-mt-20 [&_.sticky]:!top-4'>
|
||
<PersonalSetting />
|
||
</div>,
|
||
)}
|
||
/>
|
||
{/* WeyAi 桌面端下载:侧栏入口指到这里而不是独立的 /weyai/download,
|
||
否则点一下侧栏就整页跳走、导航栏消失。embedded:不加顶部让位、
|
||
不自绘背景(shell 已有 PageBackground)。未开放时页面自身显示空态,
|
||
侧栏项也随开关隐藏,无需再加守卫 */}
|
||
<Route path='weyai-download' element={<WeyAiDownload embedded />} />
|
||
</Routes>
|
||
</div>
|
||
</div>
|
||
|
||
{/* 移动端底部导航栏 */}
|
||
<MobileBottomNav sidebarConfig={userCenterConfig} />
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default UserCenter;
|