/* 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, useEffect, useState, useCallback, useRef, useMemo, } from 'react'; import { Routes, Route, Navigate, useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Button } from '@douyinfe/semi-ui'; import OrgSideSheet from './components/OrgSideSheet'; import { Menu } from 'lucide-react'; import { StatusContext } from '../../context/Status'; import { UserContext } from '../../context/User'; import { API } from '../../helpers/api'; import { showError } from '../../helpers'; import Loading from '../../components/common/ui/Loading'; import PageBackground from '../../components/common/PageBackground'; import OrgSidebar from './OrgSidebar'; import OrgWorkbench from './OrgWorkbench'; import OrgDashboard from './OrgDashboard'; import OrgMembers from './OrgMembers'; import OrgDepartments from './OrgDepartments'; import OrgProjects from './OrgProjects'; import OrgBudgets from './OrgBudgets'; import OrgRequests from './OrgRequests'; import OrgTokens from './OrgTokens'; import OrgDeptManage from './OrgDeptManage'; import OrgProjectManage from './OrgProjectManage'; import OrgAudit from './OrgAudit'; import OrgLogs from './OrgLogs'; import OrgSettings from './OrgSettings'; import OrgRecharge from './OrgRecharge'; import OrgPersonalSettings from './OrgPersonalSettings'; import CloudBrain from '../CloudBrain'; import OrgKnowledge from './OrgKnowledge'; import OrgSkillCenter from './OrgSkillCenter'; import OrgSkillAssignments from './OrgSkillAssignments'; import OrgMyCapabilities from './OrgMyCapabilities'; import OrgExperts from './OrgExperts'; import OrgAgentAudit from './OrgAgentAudit'; import OrgConnectors from './OrgConnectors'; import OrgEffectiveness from './OrgEffectiveness'; import OrgQuality from './OrgQuality'; import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules'; import { canAccessOrgSettings } from './orgFeatureFlags'; import { CLOUD_BRAIN_UI_ENABLED, isCloudBrainOrgModule, } from '../../config/cloudBrainUi'; import '../../styles/org.css'; const routeFeatureRequirements = { cloudBrain: 'cloud_brain', knowledge: 'knowledge_base', skillLibrary: 'skill_market', skillAssignments: 'skill_market', mySkills: 'skill_market', experts: 'expert', agentAudit: 'cloud_brain', connectors: 'connector', myConnectors: 'connector', effectiveness: 'agent_effectiveness', quality: 'quality_monitor', privateSkills: 'skill_market', departments: 'dept', projects: 'project', projectManage: 'project', budgets: 'budget', requests: 'approval', }; const parseFeatureFlags = (raw) => { if (!raw) return {}; if (typeof raw === 'object') return raw; try { return JSON.parse(raw) || {}; } catch { return {}; } }; const isFeatureEnabled = (flags, key) => { if (!key) return true; const value = flags?.[key]; return value === true || value === 'true' || value === '1' || value === 1; }; const Organization = () => { const { t } = useTranslation(); const navigate = useNavigate(); const [mobileNavOpen, setMobileNavOpen] = useState(false); const navigateRef = useRef(navigate); navigateRef.current = navigate; const [statusState] = useContext(StatusContext); const [userState] = useContext(UserContext); // org_only 员工被 App 守卫限制在 /org;这里的回退不能再把他们踢回 C 端,否则死循环 const isOrgUser = userState?.user?.org_only_mode === true; const [orgInfo, setOrgInfo] = useState(null); const [orgRole, setOrgRole] = useState(null); const [canManageCloudBrain, setCanManageCloudBrain] = useState(false); const [canManageKnowledge, setCanManageKnowledge] = useState(false); const [canAuditKnowledge, setCanAuditKnowledge] = useState(false); const [canManageSkills, setCanManageSkills] = useState(false); const [canAssignSkills, setCanAssignSkills] = useState(false); const [canManageExperts, setCanManageExperts] = useState(false); const [canManageConnectors, setCanManageConnectors] = useState(false); const [canAuditAgentMetadata, setCanAuditAgentMetadata] = useState(false); const [canAuditAgentContent, setCanAuditAgentContent] = useState(false); const [canViewAgentEffect, setCanViewAgentEffect] = useState(false); const [canViewQuality, setCanViewQuality] = useState(false); const [canManageAgentPolicy, setCanManageAgentPolicy] = useState(false); // 平台超管查看企业时没有成员身份,成员型接口一律 member_actor_required; // 用该标志收敛“我的技能/我的业务系统授权”这类必失败入口。 const [isMemberActor, setIsMemberActor] = useState(true); const [loading, setLoading] = useState(true); const [pendingActivation, setPendingActivation] = useState(false); const [isDeptManager, setIsDeptManager] = useState(false); const [deptScopeLoaded, setDeptScopeLoaded] = useState(false); const [isProjectManager, setIsProjectManager] = useState(false); const [projectScopeLoaded, setProjectScopeLoaded] = useState(false); const hasMarquee = !!statusState?.status?.marquee_content?.trim(); const isAdmin = orgRole === 'owner' || orgRole === 'admin' || orgRole === 'super_admin'; const orgConfig = useMemo( () => normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin) .orgCenter, [statusState?.status?.SidebarModulesAdmin], ); const orgFeatureFlags = useMemo( () => parseFeatureFlags(orgInfo?.feature_flags), [orgInfo?.feature_flags], ); const canManageSettings = canAccessOrgSettings(orgRole, orgFeatureFlags); const isOrgModuleVisible = useCallback( (key) => { if (!CLOUD_BRAIN_UI_ENABLED && isCloudBrainOrgModule(key)) return false; return ( orgConfig?.enabled !== false && orgConfig?.[key] !== false && isFeatureEnabled(orgFeatureFlags, routeFeatureRequirements[key]) ); }, [orgConfig, orgFeatureFlags], ); const visibleOrgRoutes = useMemo(() => { const routes = []; // 管理员默认落 dashboard;普通成员默认落 workbench(§36 角色化首屏) if (isAdmin && isOrgModuleVisible('dashboard')) routes.push('dashboard'); if (isOrgModuleVisible('workbench')) routes.push('workbench'); if (isOrgModuleVisible('cloudBrain')) routes.push('cloud-brain'); // 成员型入口对无成员身份的超管隐藏;否则模块配置特定组合下 // hiddenOrgTarget 可能算出指向自身的 Navigate 造成重定向自环。 // 我的技能 + 业务系统授权已合并为「我的 AI 能力」,任一页签可见即入路由。 if ( isMemberActor && (isOrgModuleVisible('mySkills') || isOrgModuleVisible('myConnectors')) ) routes.push('my-capabilities'); if (isAdmin && canManageConnectors && isOrgModuleVisible('connectors')) routes.push('connectors'); if (isAdmin && canManageExperts && isOrgModuleVisible('experts')) routes.push('experts'); if (isAdmin && canViewAgentEffect && isOrgModuleVisible('effectiveness')) routes.push('effectiveness'); if (isAdmin && canViewQuality && isOrgModuleVisible('quality')) routes.push('quality'); // 审计路由组后端是 OrgAdminAuth(仅 owner/admin),不放行部门负责人; // 这里必须同样只认 isAdmin。此前的 isDeptManager 分支当前不可达 // (canAuditAgentMetadata 对非 owner/admin 恒 false),但一旦把 // agent_audit_metadata 下放给 dept_admin,就会变成"入口可见但接口必 403"。 if (isAdmin && canAuditAgentMetadata && isOrgModuleVisible('agentAudit')) routes.push('agent-audit'); // 技能库 + 自研 Skill 已合并为「技能中心」;平台技能的启用动作 // 有两个入口:平台技能超市(/skill-market)就地启用,或技能中心内的 // 「引入技能」抽屉(供超管与被挡在超市外的管理员使用)。企业技能市场 // 路由已不存在,旧路径重定向。 if ( isAdmin && canManageSkills && (isOrgModuleVisible('skillLibrary') || isOrgModuleVisible('privateSkills')) ) routes.push('skill-center'); if ( (isAdmin || isDeptManager) && canAssignSkills && isOrgModuleVisible('skillAssignments') ) routes.push('skill-assignments'); if (isAdmin && canManageKnowledge && isOrgModuleVisible('knowledge')) routes.push('knowledge'); if ( isAdmin && (isOrgModuleVisible('members') || isOrgModuleVisible('memberAccounts')) ) { routes.push('members'); } if (isDeptManager && isOrgModuleVisible('deptManage')) routes.push('dept-manage'); if (isProjectManager && isOrgModuleVisible('projectManage')) routes.push('project-manage'); if (isAdmin && isOrgModuleVisible('departments')) routes.push('departments'); if (isAdmin && isOrgModuleVisible('projects')) routes.push('projects'); if (isAdmin && isOrgModuleVisible('budgets')) routes.push('budgets'); if (isOrgModuleVisible('requests')) routes.push('requests'); if (isOrgModuleVisible('tokens')) routes.push('tokens'); if (isOrgModuleVisible('logs')) routes.push('logs'); if (isAdmin && isOrgModuleVisible('audit')) routes.push('audit'); if (isAdmin && isOrgModuleVisible('recharge')) routes.push('recharge'); if (canManageSettings && isOrgModuleVisible('settings')) routes.push('settings'); return routes; }, [ isAdmin, canManageKnowledge, canManageSkills, canAssignSkills, canManageExperts, canManageConnectors, canAuditAgentMetadata, canViewAgentEffect, canViewQuality, isDeptManager, isProjectManager, isMemberActor, canManageSettings, isOrgModuleVisible, ]); const firstOrgRoute = visibleOrgRoutes[0] || null; const firstOrgTarget = firstOrgRoute ? `/org/${firstOrgRoute}` : '/user-center/dashboard'; const hiddenOrgTarget = firstOrgTarget; const guardOrgRoute = (key, element) => { if (!isOrgModuleVisible(key)) { return ; } return element; }; // 从 localStorage 获取当前企业ID const orgId = parseInt(localStorage.getItem('current_org_id') || '0', 10); const fetchOrgInfo = useCallback(async () => { if (!orgId) { setLoading(false); return; } setLoading(true); setPendingActivation(false); try { const res = await API.get('/api/org/info', { headers: { 'X-Org-Id': String(orgId) }, }); if (res.data.success) { const member = res.data.data.member || res.data.data; setOrgInfo(res.data.data.organization); setOrgRole(member.role); setCanManageCloudBrain(member.can_manage_cloud_brain === true); setCanManageKnowledge(member.can_manage_knowledge === true); setCanAuditKnowledge(member.can_audit_knowledge === true); setCanManageSkills(member.can_manage_skills === true); setCanAssignSkills(member.can_assign_skills === true); setCanManageExperts(member.can_manage_experts === true); setCanManageConnectors(member.can_manage_connectors === true); setCanViewAgentEffect(member.can_view_agent_effect === true); setCanViewQuality(member.can_view_quality === true); setCanManageAgentPolicy(member.can_manage_agent_policy === true); // 后端未返回该键时按“有成员身份”处理,避免旧后端下入口消失。 setIsMemberActor(member.is_member_actor !== false); setCanAuditAgentMetadata(member.can_audit_agent_metadata === true); setCanAuditAgentContent(member.can_audit_agent_content === true); } else if (res.data.code === 'org_pending_activation') { // 待激活:不跳转创建页,展示占位页,保留 current_org_id setPendingActivation(true); } else { showError(res.data.message); localStorage.removeItem('current_org_id'); navigateRef.current('/org/create', { replace: true }); } } catch { localStorage.removeItem('current_org_id'); navigateRef.current('/org/create', { replace: true }); } finally { setLoading(false); } }, [orgId]); useEffect(() => { fetchOrgInfo(); }, [fetchOrgInfo]); // 是否为部门负责人(控制「部门管理」入口/路由),失败按非负责人处理。 useEffect(() => { let cancelled = false; if (!orgId) { setIsDeptManager(false); setDeptScopeLoaded(true); return () => { cancelled = true; }; } setIsDeptManager(false); setDeptScopeLoaded(false); API.get('/api/org/me/dept-scope', { headers: { 'X-Org-Id': String(orgId) }, }) .then((res) => { if (!cancelled) { setIsDeptManager( !!(res.data.success && res.data.data?.is_dept_manager), ); } }) .catch(() => { if (!cancelled) setIsDeptManager(false); }) .finally(() => { if (!cancelled) setDeptScopeLoaded(true); }); return () => { cancelled = true; }; }, [orgId]); // 是否为项目经理(控制「我的项目」入口/路由) useEffect(() => { if (!orgId) { setIsProjectManager(false); setProjectScopeLoaded(true); return; } setProjectScopeLoaded(false); API.get('/api/org/me/project-scope', { headers: { 'X-Org-Id': String(orgId) }, }) .then((res) => { setIsProjectManager( !!(res.data.success && res.data.data?.is_project_manager), ); }) .catch(() => setIsProjectManager(false)) .finally(() => setProjectScopeLoaded(true)); }, [orgId]); // org_only 员工无可用企业功能时的非循环提示(不跳 C 端) const orgUnavailableFallback = (
{t('暂无可用的企业功能')}

{t('您的账号当前没有可访问的企业功能,请联系企业管理员开通。')}

); if (orgConfig.enabled === false) { if (isOrgUser) return orgUnavailableFallback; return ; } if (!orgId) { return ; } if (loading) { return ; } // 待激活占位页:不展示任何企业功能,仅引导联系客服/管理员 if (pendingActivation) { return (
{t('企业待激活')}

{t( '您的企业已创建,正在等待平台管理员审核激活。激活后即可使用企业工作台与治理功能,请联系客服或销售协助开通。', )}

{!isOrgUser && ( )}
); } if (!firstOrgRoute) { if (isOrgUser) return orgUnavailableFallback; return ; } return (
{/* Sidebar - desktop only */}
{/* Main Content */}
{/* 移动端顶栏:汉堡菜单 + 当前企业名(窄屏唯一的菜单入口) */}
{/* 统一页面背景层 —— 唯一背景来源;实底卡片方案下降为 subtle 档, 不再叠加硬编码渐变装饰层(原 teal/amber 层不跟主题预设,已删除) */}
{/* 默认路由:管理员进数据看板,普通成员进工作台 */} } /> {/* 数据看板、员工管理、审计日志、企业设置仅 admin/owner 可访问;消耗明细普通成员可看本人 */} , ) ) : ( ) } /> ) : ( ) } /> ) : ( ) } /> , ) ) : ( ) } /> , ) ) : ( ) } /> ) : ( ), )} /> ) ) : ( ) } /> , ) ) : ( ) } /> ) ) : ( ) } /> {/* 部门管理 - 部门负责人可访问(阶段一);与菜单/project-manage 同口径消费 deptManage 管理开关,避免"菜单隐藏但直达 URL 可进" */} ) : !deptScopeLoaded ? ( ) : ( ) } /> {/* 我的项目 - 项目经理可访问(阶段一) */} ) : !projectScopeLoaded ? ( ) : ( ) } /> {/* 企业工作台 - 所有成员可访问(§36.1) */} , )} /> , )} /> , ) ) : ( ) } /> {/* 我的 AI 能力:我的技能 + 业务系统授权 合并页(成员自助视图) */} ) : ( ) } /> {/* 旧路径兼容:合并前的两个成员页重定向到对应页签 */} } /> ) ) : ( ) } /> ) ) : ( ) } /> , ) ) : ( ) } /> ) ) : ( ) } /> , ) ) : ( ) } /> } /> {/* 技能中心:企业技能库 + 企业自研 Skill 合并页 */} ) : ( ) } /> {/* 企业技能市场页已并入平台技能超市:管理员在 /skill-market 就地 为企业启用技能(普通成员落到超市浏览页也无害) */} } /> {/* 旧路径兼容:合并前的两个管理页重定向到对应页签 */} } /> } /> , ) ) : ( ) } /> {/* 申请/审批 - 所有成员可访问(成员看自己,管理员审批) */} , )} /> {/* 预算中心 - 仅管理员 */} ) ) : ( ) } /> {/* 用量分析统一归入数据看板,旧路径重定向 */} } /> {/* 令牌管理 - 所有成员可访问 */} , )} /> {/* 个人设置 - 所有成员可访问(头像/昵称/密码/配额预警通知) */} } />
{/* 移动端侧栏抽屉(窄屏点击顶栏汉堡菜单打开) */} setMobileNavOpen(false)} width={260} closable={false} bodyStyle={{ padding: 0 }} > setMobileNavOpen(false)} />
); }; export default Organization;