Files
monkey-ai-web/src/authz/usePerm.js
T
qiubaishen 870b51d054 feat: 同步权限管理、企业成员排行与消费计费明细
- 接入平台与企业角色权限,按权限展示菜单、页面及操作。
- 企业成员排行按需加载,翻页沿用同一榜单快照,保留连续名次与前三名样式。
- 更新消费日志的费用明细、超管指标和官方价格汇率设置,补齐翻译与回归测试。
2026-11-25 21:08:24 +08:00

60 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 { useContext, useEffect } from 'react';
import { AuthzContext, currentOrgViewKey } from './AuthzProvider';
import { checkPerm, checkAnyPerm, viewForCode } from './permLogic';
// usePerm(code, legacy):模块已切到新体系时按权限点判断,否则按 legacy(旧判断)判断。
export function usePerm(code, legacy) {
const ctx = useContext(AuthzContext);
return checkPerm(viewForCode(ctx, code), code, legacy);
}
// useAnyPerm(codes, legacy):持有其中任一权限点。
export function useAnyPerm(codes, legacy) {
const ctx = useContext(AuthzContext);
if (!codes.length) return false;
return checkAnyPerm(viewForCode(ctx, codes[0]), codes, legacy);
}
// useAuthzView(domain):'platform' | 'org' 的原始视图(菜单树渲染等)。
export function useAuthzView(domain) {
const ctx = useContext(AuthzContext);
return domain === 'org' ? ctx.org : ctx.platform;
}
export function useAuthzReload() {
return useContext(AuthzContext).reload;
}
// useOrgViewReady():企业视图是否已按当前企业(含代理会话)加载过(成败均算)。
// 企业中心在它就绪前不做路由守卫,避免新体系下持有菜单的人在视图到达前被旧判断重定向走。
// 当前企业刚由解析器写入、Provider 尚未感知时主动触发一次加载。
export function useOrgViewReady() {
const ctx = useContext(AuthzContext);
const key = currentOrgViewKey();
const ready = ctx.orgLoadedKey === key;
const { reload } = ctx;
useEffect(() => {
if (!ready) reload();
}, [ready, key, reload]);
return ready;
}