302 lines
10 KiB
React
302 lines
10 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 {
|
||
KeyRound,
|
||
LayoutGrid,
|
||
ScrollText,
|
||
Wallet,
|
||
UserRound,
|
||
Gift,
|
||
Receipt,
|
||
} from 'lucide-react';
|
||
import { StatusContext } from '../../context/Status';
|
||
import { UserContext } from '../../context/User';
|
||
import { isSidebarModuleVisible } from '../../helpers/navigationVisibility';
|
||
import { useMarquee } from '../../hooks/common/useMarquee';
|
||
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 WeyAiDownload from '../WeyAiDownload';
|
||
import KeyManagement from '../KeyManagement';
|
||
import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules';
|
||
import {
|
||
isMiniProgramPaymentPath,
|
||
isMiniProgramWebView,
|
||
} from '../../helpers/miniProgram';
|
||
|
||
// 移动端底部导航组件
|
||
const MobileBottomNav = ({ sidebarConfig }) => {
|
||
const { t } = useTranslation();
|
||
const location = useLocation();
|
||
const navigate = useNavigate();
|
||
const [userState] = useContext(UserContext);
|
||
const userGroup = userState?.user?.group;
|
||
|
||
const navItems = [
|
||
{
|
||
key: 'dashboard',
|
||
icon: LayoutGrid,
|
||
label: t('概览'),
|
||
path: '/user-center/dashboard',
|
||
},
|
||
{
|
||
key: 'usageRecord',
|
||
icon: ScrollText,
|
||
label: t('记录'),
|
||
path: '/user-center/usage-record',
|
||
},
|
||
{
|
||
key: 'keyManagement',
|
||
icon: KeyRound,
|
||
label: t('密钥管理'),
|
||
path: '/user-center/key-management',
|
||
},
|
||
{
|
||
key: 'invite',
|
||
icon: Gift,
|
||
label: t('邀请'),
|
||
path: '/user-center/invite',
|
||
},
|
||
{
|
||
key: 'topup',
|
||
icon: Wallet,
|
||
label: t('充值'),
|
||
path: '/user-center/topup',
|
||
},
|
||
{
|
||
key: 'invoice',
|
||
icon: Receipt,
|
||
label: t('发票'),
|
||
path: '/user-center/invoice',
|
||
},
|
||
{
|
||
key: 'settings',
|
||
icon: UserRound,
|
||
label: t('设置'),
|
||
path: '/user-center/settings',
|
||
},
|
||
];
|
||
|
||
const inMiniProgram = isMiniProgramWebView();
|
||
const visibleNavItems = navItems.filter(
|
||
(item) =>
|
||
isSidebarModuleVisible(sidebarConfig, item.key, userGroup) &&
|
||
!(inMiniProgram && isMiniProgramPaymentPath(item.path)),
|
||
);
|
||
|
||
if (visibleNavItems.length === 0) {
|
||
return null;
|
||
}
|
||
|
||
return (
|
||
<nav className='uc-mobile-nav md:hidden fixed bottom-0 left-0 right-0 z-50 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}
|
||
type='button'
|
||
data-active={isActive}
|
||
aria-current={isActive ? 'page' : undefined}
|
||
onClick={() => navigate(item.path)}
|
||
className='uc-mobile-nav-item flex flex-col items-center justify-center flex-1 h-full'
|
||
>
|
||
<IconComponent size={20} />
|
||
<span>{item.label}</span>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
</nav>
|
||
);
|
||
};
|
||
|
||
const UserCenter = ({ keyManagementMode = false }) => {
|
||
const [statusState] = useContext(StatusContext);
|
||
const [userState] = useContext(UserContext);
|
||
const userGroup = userState?.user?.group;
|
||
// 仅用于页面外壳的样式标识(看板与子页面底色不同),不参与路由判断
|
||
const ucRoute = useLocation().pathname.split('/')[2] || 'dashboard';
|
||
|
||
// 跑马灯公告是否存在(用于动态计算顶部间距)
|
||
const { visible: hasMarquee } = useMarquee();
|
||
const userCenterConfig = useMemo(
|
||
() =>
|
||
normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin)
|
||
.userCenter,
|
||
[statusState?.status?.SidebarModulesAdmin],
|
||
);
|
||
const userCenterRoutes = [
|
||
'dashboard',
|
||
'usageRecord',
|
||
'topup',
|
||
'invoice',
|
||
'invite',
|
||
'settings',
|
||
];
|
||
const routePathMap = {
|
||
dashboard: 'dashboard',
|
||
usageRecord: 'usage-record',
|
||
topup: 'topup',
|
||
invoice: 'invoice',
|
||
invite: 'invite',
|
||
settings: 'settings',
|
||
};
|
||
// 小程序里付费页面会被 App.jsx 送回 /user-center,所以默认页不能落在它们上面,否则来回跳转。
|
||
const inMiniProgram = isMiniProgramWebView();
|
||
const firstVisiblePath =
|
||
userCenterConfig.enabled === false
|
||
? null
|
||
: routePathMap[
|
||
userCenterRoutes.find(
|
||
(key) =>
|
||
isSidebarModuleVisible(userCenterConfig, key, userGroup) &&
|
||
!(
|
||
inMiniProgram &&
|
||
isMiniProgramPaymentPath(`/user-center/${routePathMap[key]}`)
|
||
),
|
||
)
|
||
] || null;
|
||
const firstVisibleRoute = firstVisiblePath
|
||
? `/user-center/${firstVisiblePath}`
|
||
: '/';
|
||
const guardUserCenterRoute = (key, element) => {
|
||
if (!isSidebarModuleVisible(userCenterConfig, key, userGroup)) {
|
||
return <Navigate to={firstVisibleRoute} replace />;
|
||
}
|
||
return element;
|
||
};
|
||
|
||
if (!firstVisiblePath && !keyManagementMode) {
|
||
return <Navigate to='/' replace />;
|
||
}
|
||
|
||
return (
|
||
<div
|
||
data-uc-route={ucRoute}
|
||
className={`user-center flex flex-col md:flex-row h-screen overflow-hidden bg-[var(--surface-bg-base)] ${hasMarquee ? 'pt-[91px] md:pt-[107px]' : 'pt-14 md:pt-[72px]'}`}
|
||
>
|
||
{/* Sidebar - 仅桌面端 */}
|
||
<div className='hidden md:block flex-shrink-0 h-full'>
|
||
<UserCenterSidebar />
|
||
</div>
|
||
|
||
{/* Main Content */}
|
||
<div className='min-w-0 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'>
|
||
{keyManagementMode ? (
|
||
<KeyManagement embedded />
|
||
) : (
|
||
<Routes>
|
||
<Route
|
||
path='/'
|
||
element={<Navigate to={firstVisibleRoute} replace />}
|
||
/>
|
||
<Route
|
||
path='dashboard'
|
||
element={guardUserCenterRoute('dashboard', <UserDashboard />)}
|
||
/>
|
||
<Route
|
||
path='usage-record'
|
||
element={guardUserCenterRoute('usageRecord', <UsageRecord />)}
|
||
/>
|
||
{/* 服务监控暂不开放,历史地址回到首个已开放页面。 */}
|
||
<Route
|
||
path='service-dashboard'
|
||
element={<Navigate to={firstVisibleRoute} replace />}
|
||
/>
|
||
{/* 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;
|