/* 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, { useContext } from 'react'; import { Navigate, useLocation } from 'react-router-dom'; import { history } from './history'; import { UserContext } from '../context/User'; import Loading from '../components/common/ui/Loading'; import { DEFAULT_LOGIN_REDIRECT, getUserData, isSafePostLoginRedirect, savePostLoginRedirect, } from './data'; /** * 判断是否处于认证类页面(登录/注册/重置密码/企业登录/企业注册)。 * 认证页顶栏遵循行业惯例只保留最小化工具(logo + 主题切换), * HeaderBar / ActionButtons / UserArea 共用本判断,避免各处正则漂移。 */ export const isAuthPagePath = (pathname) => ['/login', '/register', '/reset', '/user/reset'].includes(pathname) || pathname === '/org/login' || pathname === '/org/register' || // WeyAi 客户端授权页:与登录页同属认证流程,使用最小化顶栏。 // 大小写不敏感——React Router 默认如此,/Client/Authorize 渲染的是同一个 // 页面,后端 ClientFrameGuard 也按 EqualFold 判定,这里必须跟上,否则该 // 写法会带出完整顶栏与跑马灯 pathname.toLowerCase() === '/client/authorize' || /^\/org\/[^/]+\/login$/.test(pathname); // 未登录统一引导到企业登录(本产品主推企业用户;个人用户可经页内切换回 /login)。 // 回跳地址已存本地(savePostLoginRedirect),切换到个人登录后仍可正常回跳。 const getLoginTarget = (location) => { const target = `${location.pathname}${location.search}${location.hash}`; if (isSafePostLoginRedirect(target)) { savePostLoginRedirect(target); return `/org/login?redirect=${encodeURIComponent(target)}`; } return '/org/login'; }; export const AuthRedirect = ({ children }) => { const [userState] = useContext(UserContext); const cachedUser = getUserData(); if (userState?.user) { // org_only(仅企业资源)员工登录后直接进入企业空间 if (userState.user.org_only_mode === true) { return ; } // 已登录用户统一跳转到默认登录落地页,而不是控制台(控制台需要管理员权限) return ; } if (!userState?.authChecked && cachedUser) { return ; } return children; }; function PrivateRoute({ children }) { const [userState] = useContext(UserContext); const location = useLocation(); const cachedUser = getUserData(); if (userState?.user) { return children; } if (!userState?.authChecked && cachedUser) { return ; } return ( ); } export function AdminRoute({ children }) { const [userState] = useContext(UserContext); const location = useLocation(); const cachedUser = getUserData(); if (!userState?.authChecked && cachedUser) { return ; } if (!userState?.user) { return ( ); } if (typeof userState.user.role === 'number' && userState.user.role >= 10) { return children; } return ; } export function RootRoute({ children }) { const [userState] = useContext(UserContext); const location = useLocation(); const cachedUser = getUserData(); if (!userState?.authChecked && cachedUser) { return ; } if (!userState?.user) { return ( ); } if (typeof userState.user.role === 'number' && userState.user.role >= 100) { return children; } return ; } export { PrivateRoute };