import { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js'; import React, { useContext, useEffect, useMemo } from 'react'; import { useLocation } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { BarChart3, Key, FileSearch, Receipt, Settings, CreditCard, Network, Folder, LayoutGrid, Wallet, UserRound, BrainCircuit, LibraryBig, Blocks, ListChecks, Bot, ShieldCheck, Cable, Gauge, Zap, Check, Activity, Route as RouteIcon, UserCog, } from 'lucide-react'; import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed'; import { useSidebarNavigation } from '../../hooks/common/useTopNavigation'; import { useSidebarGroups } from '../../hooks/common/useSidebarGroups'; import { CenterSidebar, CenterSidebarGroup, CenterSidebarItem, } from '../../components/layout/components/CenterSidebar'; import { StatusContext } from '../../context/Status'; import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules'; import { canAccessOrgSettings } from './orgFeatureFlags'; import OrgCommissionNavigation from './OrgCommissionNavigation'; import { usePerm, useAuthzView } from '../../authz'; import { gateOrgRoute, hasEnforcedModules, isOrgMenuEnforced, orgGroupLayout, orgMenuLayout, orgRouteKey, } from './orgMenuAccess'; import { isMiniProgramPaymentPath, isMiniProgramWebView, } from '../../helpers/miniProgram'; import { CLOUD_BRAIN_UI_ENABLED, isCloudBrainOrgPath, } from '../../config/cloudBrainUi'; const pathFeatureRequirements = { '/org/cloud-brain': 'cloud_brain', '/org/knowledge': 'knowledge_base', '/org/skill-center': 'skill_market', '/org/skill-assignments': 'skill_market', '/org/experts': 'expert', '/org/agent-audit': 'cloud_brain', '/org/connectors': 'connector', '/org/effectiveness': 'agent_effectiveness', '/org/quality': 'quality_monitor', '/org/smart-router': 'smart_router', '/org/departments': 'dept', '/org/projects': 'project', '/org/budgets': 'budget', '/org/requests': 'approval', '/org/project-manage': 'project', }; const isFeatureEnabled = (flags, key) => { if (!key) return true; const value = flags?.[key]; return value === true || value === 'true' || value === '1' || value === 1; }; const OrgSidebar = ({ orgName, organizations = [], currentOrgId, onOrganizationChange, orgRole, featureFlags, isDeptManager = false, isProjectManager = false, canManageKnowledge = false, canManageSkills = false, canAssignSkills = false, canManageExperts = false, canManageConnectors = false, canAuditAgentMetadata = false, canViewAgentEffect = false, canViewQuality = false, canViewCommission = false, canViewSmartRouter = false, smartRouterConsentOnly = false, isMemberActor = true, // 路由守卫算好的可进入表(Organization/index.jsx 的 routeAccess):已切到新体系的菜单以它为准,保证与路由一致 routeAccess = null, onNavigate, mobile = false, }) => { const { t } = useTranslation(); const location = useLocation(); const [collapsedState, toggleCollapsed] = useSidebarCollapsed(); // 移动端抽屉里始终展开,不允许折叠 const collapsed = mobile ? false : collapsedState; const followSidebarLink = useSidebarNavigation(onNavigate); const [statusState] = useContext(StatusContext); const orgConfig = useMemo( () => normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin) .orgCenter, [statusState?.status?.SidebarModulesAdmin], ); // 企业视图(权限重构 P2-7):有模块切到新体系后,菜单显示、名称、分组与排序按后端菜单树 // (/api/authz/me 的 menus);未切换的菜单沿用下方的旧判断 const orgView = useAuthzView('org'); const menuTreeMode = hasEnforcedModules(orgView); const isAdmin = orgRole === 'owner' || orgRole === 'admin' || orgRole === 'super_admin'; // owner/超管可管理完整设置;普通管理员仅在平台授权数据安全自助配置时进入该页。 const canManageSettings = canAccessOrgSettings(orgRole, featureFlags); // 角色管理入口:只在角色模块已切新体系时按 org.role.query 显示;未切换时企业角色不生效,不提供入口 const canQueryRoles = usePerm('org.role.query', false); const roleLabel = useMemo(() => { if (orgRole === 'super_admin') return t('超级管理员'); if (orgRole === 'owner') return t('所有者'); if (orgRole === 'admin') return t('企业管理员'); if (orgRole === 'dept_admin' && isProjectManager) return t('部门/项目管理员'); if (orgRole === 'dept_admin' || isDeptManager) return t('部门管理员'); if (isProjectManager) return t('项目管理员'); return t('成员'); }, [t, orgRole, isDeptManager, isProjectManager]); // 菜单分组(云平台风格的分组导航,给扁平的十几个入口建立层次) const groups = useMemo(() => { const base = [ { key: 'overview', label: t('概览') }, { key: 'ai', label: t('AI 能力管理') }, { key: 'org', label: t('组织管理') }, { key: 'governance', label: t('额度与治理') }, { key: 'records', label: t('财务与日志') }, { key: 'settings', label: t('系统设置') }, ]; if (!menuTreeMode) return base; // 菜单树模式:分组即后端目录,名称与排序取后端(未持有该目录时用前端文案与默认排序) return base .map((g) => { const layout = orgGroupLayout(orgView, g.key); return { ...g, label: layout.name ? t(layout.name) : g.label, sort: layout.sort, }; }) .sort((a, b) => a.sort - b.sort); }, [t, menuTreeMode, orgView]); // 统一单一品牌色高亮(云平台风格),不再每项一种颜色 const items = useMemo(() => { if (orgConfig?.enabled === false) { return []; } // adminOnly 仅 owner/admin/super_admin 可见;其余所有成员可见 const all = [ { itemKey: '/org/workbench', text: t('工作台'), icon: LayoutGrid, group: 'overview', }, { itemKey: '/org/cloud-brain', text: t('云端大脑'), icon: BrainCircuit, group: 'overview', beta: true, }, { itemKey: '/org/knowledge', text: t('企业知识'), icon: LibraryBig, group: 'overview', adminOnly: true, knowledgePermission: true, beta: true, }, { // 我的技能 + 业务系统授权合并页:成员自助视图,归入概览组, // 让「AI 能力管理」成为纯管理员分组。 itemKey: '/org/my-capabilities', text: t('我的 AI 能力'), icon: Zap, group: 'overview', }, { // 企业技能库 + 企业自研 Skill 合并页;平台技能的启用在技能超市完成 itemKey: '/org/skill-center', text: t('技能中心'), icon: Blocks, group: 'ai', adminOnly: true, skillManage: true, }, { itemKey: '/org/skill-assignments', text: t('分发与锁定'), icon: ListChecks, group: 'ai', assignmentPermission: true, }, { itemKey: '/org/experts', text: t('企业专家工坊'), icon: Bot, group: 'ai', adminOnly: true, expertManage: true, }, { itemKey: '/org/connectors', text: t('Connector 管理'), icon: Cable, group: 'ai', adminOnly: true, connectorManage: true, }, { itemKey: '/org/effectiveness', text: t('效能分析'), icon: Gauge, group: 'overview', adminOnly: true, effectView: true, }, { itemKey: '/org/quality', text: t('调用质量'), icon: Activity, group: 'overview', adminOnly: true, qualityView: true, }, { itemKey: '/org/smart-router', text: t('智能路由'), icon: RouteIcon, group: 'overview', adminOnly: true, smartRouterView: true, }, { itemKey: '/org/agent-audit', text: t('Agent 任务审计'), icon: ShieldCheck, group: 'records', agentAudit: true, }, { itemKey: '/org/dashboard', text: t('数据看板'), icon: BarChart3, group: 'overview', adminOnly: true, }, { itemKey: '/org/members', text: t('员工管理'), icon: UserRound, group: 'org', adminOnly: true, }, { itemKey: '/org/roles', text: t('角色管理'), icon: UserCog, group: 'org', roleManage: true, }, { itemKey: '/org/dept-manage', text: t('我管辖的部门'), icon: Network, group: 'org', deptScope: true, }, { itemKey: '/org/project-manage', text: t('我的项目'), icon: Folder, group: 'org', projectScope: true, }, { itemKey: '/org/departments', text: t('部门管理'), icon: Network, group: 'org', adminOnly: true, }, { itemKey: '/org/projects', text: t('项目管理'), icon: Folder, group: 'org', adminOnly: true, }, { itemKey: '/org/budgets', text: t('预算中心'), icon: Wallet, group: 'governance', adminOnly: true, }, { itemKey: '/org/requests', text: isAdmin ? t('审批中心') : t('我的申请'), icon: Check, group: 'governance', }, { itemKey: '/org/tokens', text: t('令牌管理'), icon: Key, group: 'governance', }, { itemKey: '/org/recharge', text: t('企业充值'), icon: CreditCard, group: 'records', adminOnly: true, }, { itemKey: '/org/commission', text: t('企业分佣'), icon: Wallet, group: 'records', commissionAccess: true, }, { itemKey: '/org/logs', text: isAdmin || isDeptManager ? t('消耗明细') : t('我的消耗明细'), icon: Receipt, group: 'records', }, { itemKey: '/org/audit', text: t('审计日志'), icon: FileSearch, group: 'records', adminOnly: true, }, { itemKey: '/org/personal-settings', text: t('个人设置'), icon: UserRound, group: 'settings', }, { itemKey: '/org/settings', text: t('企业设置'), icon: Settings, group: 'settings', ownerOnly: true, }, ]; const visibilityByPath = { '/org/workbench': 'workbench', '/org/cloud-brain': 'cloudBrain', '/org/knowledge': 'knowledge', '/org/skill-assignments': 'skillAssignments', '/org/experts': 'experts', '/org/agent-audit': 'agentAudit', '/org/connectors': 'connectors', '/org/effectiveness': 'effectiveness', '/org/quality': 'quality', '/org/smart-router': 'smartRouter', '/org/dashboard': 'dashboard', '/org/members': 'members', '/org/departments': 'departments', '/org/projects': 'projects', '/org/dept-manage': 'deptManage', '/org/project-manage': 'projectManage', '/org/budgets': 'budgets', '/org/requests': 'requests', '/org/tokens': 'tokens', '/org/logs': 'logs', '/org/audit': 'audit', '/org/recharge': 'recharge', '/org/commission': 'commission', '/org/settings': 'settings', }; const visible = all.filter((item) => { if (!CLOUD_BRAIN_UI_ENABLED && isCloudBrainOrgPath(item.itemKey)) { return false; } // WeyAi 小程序里不出现付费入口(helpers/miniProgram.js)。 if (isMiniProgramWebView() && isMiniProgramPaymentPath(item.itemKey)) return false; // 仅授权模式:功能已关闭但所有者仍有生效中的出境同意,入口保留以便撤回(与路由守卫同口径) if (item.itemKey === '/org/smart-router' && smartRouterConsentOnly) return true; // 菜单所属模块已切到新体系:以菜单树为准(与路由守卫共用 routeAccess),不再看角色与 can_*。 // 后端目录里没有对应菜单的入口(如企业分佣)不会进入该分支,继续走下面的旧判断 const routeKey = orgRouteKey(item.itemKey); if (isOrgMenuEnforced(orgView, routeKey)) { return routeAccess ? !!routeAccess[routeKey] : gateOrgRoute(orgView, routeKey, false); } if (item.adminOnly && !isAdmin) return false; if (item.roleManage && !canQueryRoles) return false; // 平台超管(无成员身份)访问成员型入口必被 member_actor_required 拒绝。 if (!isMemberActor && item.itemKey === '/org/my-capabilities') return false; if (item.knowledgePermission && !canManageKnowledge) return false; if (item.skillManage && !canManageSkills) return false; // 与路由守卫一致:分发页要求 (isAdmin || isDeptManager) && canAssignSkills, // 只查权限位会给无管辖部门的 dept_admin 一个点击即被重定向的死入口。 if ( item.assignmentPermission && !(canAssignSkills && (isAdmin || isDeptManager)) ) return false; if (item.expertManage && !canManageExperts) return false; // 不变量:除 can_assign_skills 外,全部 can_* 都由 // MemberHasAdminPermission 计算,对非 owner/admin 恒 false——即权限位 // 本身已蕴含 isAdmin,菜单无需重复判角色。路由守卫(Organization/index.jsx) // 写成 isAdmin && can_xxx 是防御性冗余,两者当前等价。 // ⚠️ 若将来把某个权限下放给 dept_admin(做法参照 can_assign_skills 的 // `Role == DeptAdmin || MemberHasAdminPermission(...)`),该不变量即被打破, // 必须同步在这里补角色条件,否则会变成"菜单可见但路由守卫拒绝"的死入口。 if (item.agentAudit && !canAuditAgentMetadata) return false; if (item.effectView && !canViewAgentEffect) return false; if (item.qualityView && !canViewQuality) return false; if (item.commissionAccess && !canViewCommission) return false; if (item.smartRouterView && !canViewSmartRouter) return false; if (item.connectorManage && !canManageConnectors) return false; // 与路由守卫一致:管理员只有 data_security 授权后才显示设置入口。 if (item.ownerOnly && !canManageSettings) return false; // 部门管理:仅部门负责人可见(与路由门控一致;纯管理员用员工管理页,避免点了被重定向) if (item.deptScope && !isDeptManager) return false; // 我的项目:仅项目经理可见(同上,纯管理员用项目管理页) if (item.projectScope && !isProjectManager) return false; const key = visibilityByPath[item.itemKey]; const requiredFeature = pathFeatureRequirements[item.itemKey]; // 合并页跨两个模块开关/功能位,任一页签可见即显示入口(与路由守卫同口径) if (item.itemKey === '/org/my-capabilities') { return ( (orgConfig?.mySkills !== false && isFeatureEnabled(featureFlags, 'skill_market')) || (orgConfig?.myConnectors !== false && isFeatureEnabled(featureFlags, 'connector')) ); } if (item.itemKey === '/org/skill-center') { return ( isFeatureEnabled(featureFlags, 'skill_market') && (orgConfig?.skillLibrary !== false || orgConfig?.privateSkills !== false) ); } if (item.itemKey === '/org/members') { return ( (orgConfig?.members !== false || orgConfig?.memberAccounts !== false) && isFeatureEnabled(featureFlags, requiredFeature) ); } return ( (!key || orgConfig?.[key] !== false) && isFeatureEnabled(featureFlags, requiredFeature) ); }); if (!menuTreeMode) return visible; // 菜单树模式:分组按后端目录、名称与排序取后端;未切换的菜单按目录默认位置排列 return visible .map((item) => { const layout = orgMenuLayout(orgView, orgRouteKey(item.itemKey)); if (!layout) return item; return { ...item, group: layout.group || item.group, sort: layout.sort, text: layout.name ? t(layout.name) : item.text, }; }) .sort((a, b) => (a.sort ?? 0) - (b.sort ?? 0)); }, [ t, isAdmin, canQueryRoles, canManageSettings, isDeptManager, isProjectManager, canManageKnowledge, canManageSkills, canAssignSkills, canManageExperts, canAuditAgentMetadata, canViewAgentEffect, canViewQuality, canViewCommission, canViewSmartRouter, smartRouterConsentOnly, isMemberActor, canManageConnectors, orgConfig, featureFlags, orgView, menuTreeMode, routeAccess, ]); // 按分组聚合,保持 groups 顺序,自动跳过空组 const groupedItems = useMemo( () => groups .map((g) => ({ ...g, items: items.filter((it) => it.group === g.key) })) .filter((g) => g.items.length > 0), [groups, items], ); const { keys: foldedGroups, toggle: toggleGroup, setGroup, } = useSidebarGroups('org_sidebar_folded_groups'); const getSelectedKey = () => { if (location.pathname === '/org/member-accounts') { return '/org/members'; } if (items.some((item) => item.itemKey === location.pathname)) { return location.pathname; } if (location.pathname === '/org' || location.pathname === '/org/') { return items[0]?.itemKey || ''; } return ''; }; const selectedKey = getSelectedKey(); // 当前路由所在分组自动展开(仅本次会话,不覆盖用户的持久化选择) useEffect(() => { const activeGroup = groupedItems.find((g) => g.items.some((it) => it.itemKey === selectedKey), )?.key; setGroup(activeGroup, false); }, [selectedKey, groupedItems, location.key, setGroup]); return ( ); }; export default OrgSidebar;