/* 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 . For commercial licensing, please contact support@quantumnous.com */ import React, { lazy, Suspense, useContext, useMemo } from 'react'; import { Route, Routes, Navigate, useLocation } from 'react-router-dom'; import Loading from './components/common/ui/Loading'; import { AuthRedirect, PrivateRoute, AdminRoute } from './helpers'; import NotFound from './pages/NotFound'; import Forbidden from './pages/Forbidden'; import { StatusContext } from './context/Status'; import { UserContext } from './context/User'; import SetupCheck from './components/layout/SetupCheck'; import { CLOUD_BRAIN_UI_ENABLED } from './config/cloudBrainUi'; import { getHeaderModuleRequireAuth, normalizeHeaderNavModules, normalizeOrgUserNavModules, isHeaderModuleEnabled, } from './constants/headerNavModules'; import { hasEnabledOrgCenterModule, normalizeSidebarModulesAdmin, } from './constants/sidebarModules'; import { useOrgMembership } from './hooks/common/useOrgMembership'; // 懒加载所有页面组件以优化首屏加载速度 const Home = lazy(() => import('./pages/Home')); const About = lazy(() => import('./pages/About')); const UserAgreement = lazy(() => import('./pages/UserAgreement')); const PrivacyPolicy = lazy(() => import('./pages/PrivacyPolicy')); const ExperienceCenter = lazy(() => import('./pages/ExperienceCenter')); const UserCenter = lazy(() => import('./pages/UserCenter')); const Recharge = lazy(() => import('./pages/Recharge')); const Tutorial = lazy(() => import('./pages/Tutorial')); const Docs = lazy(() => import('./pages/Docs')); // 认证相关页面懒加载 const LoginForm = lazy(() => import('./components/auth/LoginForm')); const OrgLogin = lazy(() => import('./pages/OrgLogin')); const PartnerPortal = lazy(() => import('./pages/PartnerPortal')); const PartnerConsole = lazy(() => import('./pages/PartnerPortal/Console')); const MyCommission = lazy(() => import('./pages/MyCommission')); const OrgRegister = lazy(() => import('./pages/OrgRegister')); const RegisterForm = lazy(() => import('./components/auth/RegisterForm')); const PasswordResetForm = lazy( () => import('./components/auth/PasswordResetForm'), ); const PasswordResetConfirm = lazy( () => import('./components/auth/PasswordResetConfirm'), ); const OAuth2Callback = lazy(() => import('./components/auth/OAuth2Callback')); // 控制台页面懒加载 const User = lazy(() => import('./pages/User')); const Setting = lazy(() => import('./pages/Setting')); const WebsiteSettings = lazy(() => import('./pages/WebsiteSettings')); const DemoReservations = lazy(() => import('./pages/DemoReservations')); const Channel = lazy(() => import('./pages/Channel')); const Token = lazy(() => import('./pages/Token')); const Redemption = lazy(() => import('./pages/Redemption')); const TopUp = lazy(() => import('./pages/TopUp')); const Log = lazy(() => import('./pages/Log')); const Chat = lazy(() => import('./pages/Chat')); const Chat2Link = lazy(() => import('./pages/Chat2Link')); const Midjourney = lazy(() => import('./pages/Midjourney')); const Pricing = lazy(() => import('./pages/Pricing')); const SkillMarket = lazy(() => import('./pages/SkillMarket')); const Task = lazy(() => import('./pages/Task')); const ModelPage = lazy(() => import('./pages/Model')); const Playground = lazy(() => import('./pages/Playground')); const MonitorCenter = lazy(() => import('./pages/MonitorCenter')); const PersonalSetting = lazy( () => import('./components/settings/PersonalSetting'), ); const Setup = lazy(() => import('./pages/Setup')); const Analytics = lazy(() => import('./pages/Analytics')); const Organization = lazy(() => import('./pages/Organization')); const OrgCreate = lazy(() => import('./pages/Organization/OrgCreate')); const AdminOrganizations = lazy(() => import('./pages/AdminOrganizations')); const AdminOrder = lazy(() => import('./pages/AdminOrder')); const AdminInvoice = lazy(() => import('./pages/AdminInvoice')); const Subscription = lazy(() => import('./pages/Subscription')); const SkillPlatform = lazy(() => import('./pages/SkillPlatform')); const ExpertPlatform = lazy(() => import('./pages/ExpertPlatform')); // WeyAi:客户端授权页(自带登录判断,不套 PrivateRoute)与桌面端下载页 const ClientAuthorize = lazy(() => import('./pages/ClientAuthorize')); const WeyAiDownload = lazy(() => import('./pages/WeyAiDownload')); // org_only(仅企业资源)员工被限制在企业空间:仅放行以下前缀,其余 C 端路由统一重定向 /org const ORG_ALLOWED_PREFIXES = [ '/org', '/login', '/logout', '/oauth', '/forbidden', '/setup', '/register', // WeyAi 授权页与下载页:企业员工也要能登录 WeyAi 桌面端/网页端 '/client/authorize', '/weyai/download', ]; // 企业员工顶栏可配置的 C 端入口 → 对应路由前缀。 // 管理员在「企业员工顶栏配置」里放开某项时,必须同步放行其目标路由, // 否则会出现「按钮显示了、点了却被踢回 /org」的死按钮。 const ORG_USER_NAV_PATHS = { // /pricing 为模型超市旧路径,保留放行以便旧链接经重定向进入 /market 时不被企业封锁踢回 pricing: ['/market', '/pricing'], skillMarket: ['/skill-market'], docs: ['/docs'], experienceCenter: ['/experience-center'], keyManagement: ['/key-management', '/user-center/key-management'], userCenter: ['/user-center'], }; const isOrgAllowedPath = (pathname, extraPrefixes = []) => [...ORG_ALLOWED_PREFIXES, ...extraPrefixes].some( (p) => pathname === p || pathname.startsWith(p + '/'), ); // 模型超市旧路径 /pricing → /market。用组件而非静态 , // 以保留 query/hash(如资源过期重载的 __wl_reload 参数、外部带参分享链接) const PricingLegacyRedirect = () => { const location = useLocation(); return ( ); }; function App() { const location = useLocation(); const [statusState] = useContext(StatusContext); const [userState] = useContext(UserContext); // 获取模块权限配置 const moduleAuthConfig = useMemo(() => { const headerNavModulesConfig = statusState?.status?.HeaderNavModules; const modules = normalizeHeaderNavModules(headerNavModulesConfig); return { experienceCenter: getHeaderModuleRequireAuth(modules, 'experienceCenter'), keyManagement: getHeaderModuleRequireAuth(modules, 'keyManagement'), userCenter: getHeaderModuleRequireAuth(modules, 'userCenter'), pricing: getHeaderModuleRequireAuth(modules, 'pricing'), skillMarket: getHeaderModuleRequireAuth(modules, 'skillMarket'), docs: getHeaderModuleRequireAuth(modules, 'docs'), }; }, [statusState?.status?.HeaderNavModules]); const sidebarModulesAdmin = useMemo( () => normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin), [statusState?.status?.SidebarModulesAdmin], ); const orgCenterConfig = sidebarModulesAdmin.orgCenter || {}; const isOrgCenterAvailable = hasEnabledOrgCenterModule(orgCenterConfig); // 仅 org_only 用户需要这个判断(其余用户不受 orgConfined 约束);且 // useHeaderBar 已在每个页面调用同一 hook,API 层的 in-flight GET 去重 // 会合并这两次 /api/org/list,不产生额外请求。 const { isOrgAdmin, loaded: orgMembershipLoaded } = useOrgMembership( userState?.user?.id, userState?.user?.org_only_mode === true, ); // 企业员工顶栏放开的 C 端入口(模型服务/文档等)需同步放行其目标路由, // 使「显示即可进入」,避免点击后被企业封锁重定向。配置源与顶栏一致。 const orgUserAllowedPrefixes = useMemo(() => { const orgUserNav = normalizeOrgUserNavModules( statusState?.status?.HeaderNavModulesOrgUser, ); const prefixes = Object.entries(ORG_USER_NAV_PATHS) .filter(([key]) => isHeaderModuleEnabled(orgUserNav, key)) .flatMap(([, paths]) => paths); // 顶栏右侧按钮(新手教程/充值)由 rightActions 配置控制,与中部导航同理: // 管理员放开按钮必须同步放行其目标路由,否则按钮显示了、点了却被踢回 // /org(推广按钮打开的是 Modal,不涉及导航,无需放行)。 if (orgUserNav.rightActions?.tutorial === true) prefixes.push('/tutorial'); if (orgUserNav.rightActions?.recharge === true) prefixes.push('/recharge'); // 技能超市对企业管理员不只是"浏览入口",它同时是「为企业启用平台技能」的 // 唯一落点(企业侧 /org/skill-market 管理页已并入其中)。而企业 owner 与 // 托管成员账号默认就是 org_only(model/organization.go 的 // CreateOrganizationWithManagedOwnerTx、controller/organization_account.go), // 若运营关掉「企业员工顶栏 → 技能超市」,这些管理员会被 orgConfined 弹回 // /org,从此无任何路径把平台技能加入企业技能库——技能库与分发页永远为空。 // 顶栏开关的本意是收敛"员工浏览目录",不应连带阉割企业治理能力,故对 // 管理员无条件放行该路由(页面内部仍按 skill_manage 决定是否出现启用按钮)。 if (isOrgAdmin && !prefixes.includes('/skill-market')) { prefixes.push('/skill-market'); } return prefixes; }, [statusState?.status?.HeaderNavModulesOrgUser, isOrgAdmin]); // 路由级封锁:org_only 员工访问非企业路由一律回到企业空间。 // 仅在企业中心可用时启用,避免企业中心被全局关闭时与其内部回退跳转形成重定向死循环。 // 企业列表未到位前不做封锁判定:isOrgAdmin 首帧恒 false,此时若直接重定向, // 硬刷新/收藏夹直达 /skill-market 的 org_only 管理员会在放行判据还没算出来时 // 就被弹回 /org,而重定向不可逆——之后 isOrgAdmin 变 true 也回不去。 const orgConfined = userState?.user?.org_only_mode === true && orgMembershipLoaded && isOrgCenterAvailable && !isOrgAllowedPath(location.pathname, orgUserAllowedPrefixes); if (orgConfined) { return ( ); } return ( } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } /> }> } /> }> } /> }> } /> }> } /> }> } /> }> } /> }> } /> }> } /> } /> }> } /> }> ) : ( ) } /> }> ) : ( ) } /> }> } /> }> } /> {/* 旧路由重定向到用户中心内的服务监控 */} } /> } key={location.pathname}> ) : ( ) } /> } key={location.pathname} > ) : ( } key={location.pathname}> ) } /> }> } /> }> } /> }> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> }> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> }> } /> }> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> }> } /> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname} > ) : ( } key={location.pathname}> ) } /> {/* 模型超市旧路径兼容:外部收藏/文档中的 /pricing 链接重定向到 /market */} } /> {/* 技能超市:与模型超市同构的平台级目录页,同时是"为企业启用技能"的 唯一入口——企业侧原 /org/skill-market 管理页已并入这里,按身份渐进 增强(游客浏览 / 个人收藏 / 员工「我想要」/ 管理员就地为企业启用)。 */} } key={location.pathname} > ) : ( } key={location.pathname}> ) } /> {/* 密钥管理归入用户中心路径;旧地址保留为重定向(旧链接、登录回跳、书签) */} } /> } key={location.pathname} > ) : ( } key={location.pathname}> ) } /> }> ) : ( }> ) } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname} > ) : ( } key={location.pathname}> ) } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } key={location.pathname}> } /> {/* 方便使用chat2link直接跳转聊天... */} } key={location.pathname}> } /> {isOrgCenterAvailable ? ( }> ) : ( )} } /> {isOrgCenterAvailable ? ( }> ) : ( )} } /> } key={location.pathname}> } /> } key={location.pathname}> } /> } /> ); } export default App;