870b51d054
- 接入平台与企业角色权限,按权限展示菜单、页面及操作。 - 企业成员排行按需加载,翻页沿用同一榜单快照,保留连续名次与前三名样式。 - 更新消费日志的费用明细、超管指标和官方价格汇率设置,补齐翻译与回归测试。
301 lines
11 KiB
React
301 lines
11 KiB
React
import React, { useMemo, useState } from 'react';
|
||
import { Button, Empty, Input, Tag, Tree, theme } from 'antd';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { Search } from '@/components/common/icons/ActionIcons';
|
||
|
||
// 权限授权树(平台角色、套餐、模板、企业角色共用)。nodes 为后端 GrantNode 列表:
|
||
// { code, parent, type: 'dir'|'menu'|'button', name, sort, sensitive, grantable }。
|
||
// value 为已勾选的权限编码数组,只会包含 grantable 的节点;grantable=false 的节点只作为祖先展示,不可勾选。
|
||
//
|
||
// 联动规则(PRD AC50 / AC62 / AC80):
|
||
// - 勾选一个节点 = 勾选它和它全部可选后代,并带上可选祖先(后端本来也会补齐祖先);
|
||
// - 取消一个节点 = 只取消它和它的后代,兄弟不受影响;祖先下已没有任何选中的后代时祖先一并取消;
|
||
// - 祖先的显示状态由"子树内可选节点是否全部选中"推导:全部选中为勾选,部分选中为半选。
|
||
// 半选的祖先只提交它自己的编码,不会把未选中的后代带上。
|
||
|
||
const TYPE_META = {
|
||
dir: { color: 'default', label: '目录' },
|
||
menu: { color: 'blue', label: '菜单' },
|
||
button: { color: 'green', label: '按钮' },
|
||
};
|
||
|
||
const bySort = (a, b) =>
|
||
(a.sort || 0) - (b.sort || 0) || a.code.localeCompare(b.code);
|
||
|
||
// 由扁平节点建索引:parent(父编码,根为 '')、children(已排序的子编码)、
|
||
// subtree(子树内可选的编码,含自身)、order(先序遍历的编码)。
|
||
export const buildPermIndex = (nodes) => {
|
||
const byCode = new Map();
|
||
for (const n of nodes || []) byCode.set(n.code, n);
|
||
const parent = new Map();
|
||
const childNodes = new Map();
|
||
for (const n of byCode.values()) {
|
||
// parent 不在列表里的节点当作根
|
||
const p =
|
||
n.parent && n.parent !== n.code && byCode.has(n.parent) ? n.parent : '';
|
||
parent.set(n.code, p);
|
||
const list = childNodes.get(p) || [];
|
||
list.push(n);
|
||
childNodes.set(p, list);
|
||
}
|
||
const children = new Map();
|
||
for (const [p, list] of childNodes) {
|
||
children.set(
|
||
p,
|
||
list.sort(bySort).map((n) => n.code),
|
||
);
|
||
}
|
||
const subtree = new Map();
|
||
const order = [];
|
||
const walk = (code) => {
|
||
order.push(code);
|
||
const own = byCode.get(code).grantable ? [code] : [];
|
||
for (const c of children.get(code) || []) own.push(...walk(c));
|
||
subtree.set(code, own);
|
||
return own;
|
||
};
|
||
for (const c of children.get('') || []) walk(c);
|
||
return { byCode, parent, children, subtree, order };
|
||
};
|
||
|
||
// 去掉树里不存在或不可授予的编码(提交它们会被后端拒绝)。
|
||
// 只认 subtree 里登记过的节点:父链成环而从根走不到的节点不参与勾选。
|
||
const selectableSet = (index, value) =>
|
||
new Set(
|
||
(value || []).filter(
|
||
(c) => index.subtree.has(c) && index.byCode.get(c).grantable === true,
|
||
),
|
||
);
|
||
|
||
// 可以提交给后端的勾选结果(已排序):保存前用它过一遍,避免带上树里没有的历史编码
|
||
export const selectablePerms = (index, value) =>
|
||
[...selectableSet(index, value)].sort();
|
||
|
||
// 各节点的显示状态:checked = 子树内可选节点全部选中;halfChecked = 部分选中
|
||
export const permCheckState = (index, value) => {
|
||
const set = selectableSet(index, value);
|
||
const checked = [];
|
||
const halfChecked = [];
|
||
for (const [code, list] of index.subtree) {
|
||
if (!list.length) continue;
|
||
let hit = 0;
|
||
for (const c of list) if (set.has(c)) hit += 1;
|
||
if (hit === list.length) checked.push(code);
|
||
else if (hit > 0) halfChecked.push(code);
|
||
}
|
||
return { checked, halfChecked };
|
||
};
|
||
|
||
// 点击一个节点后的新勾选结果(已排序)
|
||
export const togglePerm = (index, value, code) => {
|
||
const set = selectableSet(index, value);
|
||
const sub = index.subtree.get(code) || [];
|
||
if (!sub.length) return [...set].sort();
|
||
if (sub.every((c) => set.has(c))) {
|
||
for (const c of sub) set.delete(c);
|
||
for (let p = index.parent.get(code); p; p = index.parent.get(p)) {
|
||
if (index.subtree.get(p).some((c) => c !== p && set.has(c))) break;
|
||
set.delete(p);
|
||
}
|
||
} else {
|
||
for (const c of sub) set.add(c);
|
||
for (let p = index.parent.get(code); p; p = index.parent.get(p)) {
|
||
if (index.byCode.get(p).grantable) set.add(p);
|
||
}
|
||
}
|
||
return [...set].sort();
|
||
};
|
||
|
||
// 权限摘要:已选的菜单(附其下已选按钮数)与菜单、按钮总数。用于列表里的"权限摘要"列。
|
||
export const summarizePerms = (index, perms) => {
|
||
const set = selectableSet(index, perms);
|
||
const menus = [];
|
||
let buttonCount = 0;
|
||
for (const code of index.order) {
|
||
if (!set.has(code)) continue;
|
||
const n = index.byCode.get(code);
|
||
if (n.type === 'button') {
|
||
buttonCount += 1;
|
||
} else if (n.type === 'menu') {
|
||
const buttons = index.subtree
|
||
.get(code)
|
||
.filter(
|
||
(c) => set.has(c) && index.byCode.get(c).type === 'button',
|
||
).length;
|
||
menus.push({ code, name: n.name, buttons });
|
||
}
|
||
}
|
||
return { menus, menuCount: menus.length, buttonCount, total: set.size };
|
||
};
|
||
|
||
const expandableKeys = (index, keep) =>
|
||
index.order.filter(
|
||
(c) =>
|
||
(!keep || keep.has(c)) &&
|
||
(index.children.get(c) || []).some((k) => !keep || keep.has(k)),
|
||
);
|
||
|
||
// 搜索:保留命中的节点、它的祖先(定位用)和它的整棵子树(方便直接勾选下面的按钮)
|
||
const matchKeys = (index, keyword) => {
|
||
const kw = keyword.trim().toLowerCase();
|
||
if (!kw) return null;
|
||
const keep = new Set();
|
||
const addSubtree = (code) => {
|
||
keep.add(code);
|
||
for (const c of index.children.get(code) || []) addSubtree(c);
|
||
};
|
||
for (const code of index.order) {
|
||
const n = index.byCode.get(code);
|
||
if (
|
||
!(n.name || '').toLowerCase().includes(kw) &&
|
||
!code.toLowerCase().includes(kw)
|
||
)
|
||
continue;
|
||
addSubtree(code);
|
||
for (let p = index.parent.get(code); p; p = index.parent.get(p))
|
||
keep.add(p);
|
||
}
|
||
return keep;
|
||
};
|
||
|
||
/**
|
||
* @param {object} props
|
||
* @param {Array} props.nodes 后端 GrantNode 列表
|
||
* @param {string[]} [props.value] 已勾选的权限编码
|
||
* @param {(codes: string[]) => void} [props.onChange] 勾选变化,参数为排序后的编码数组
|
||
* @param {boolean} [props.disabled] 只读:不可勾选,仍可展开、收起与搜索
|
||
* @param {number|string} [props.height=480] 树区域的最大高度,超出滚动
|
||
* @param {boolean} [props.showToolbar=true] 是否显示搜索框与"全部展开 / 全部收起"
|
||
* @param {boolean} [props.defaultExpandAll=true] 初始是否全部展开(false 时只显示根级节点)
|
||
* @param {string} [props.className]
|
||
*/
|
||
const PermTree = ({
|
||
nodes,
|
||
value,
|
||
onChange,
|
||
disabled,
|
||
height = 480,
|
||
showToolbar = true,
|
||
defaultExpandAll = true,
|
||
className,
|
||
}) => {
|
||
const { t } = useTranslation();
|
||
const { token } = theme.useToken();
|
||
const index = useMemo(() => buildPermIndex(nodes), [nodes]);
|
||
const [keyword, setKeyword] = useState('');
|
||
// null = 尚未手动操作,按 defaultExpandAll 取初始值(节点异步到达后仍然生效)
|
||
const [expandedKeys, setExpandedKeys] = useState(null);
|
||
// 搜索期间单独记一份展开状态,清空搜索后回到原来的展开状态
|
||
const [searchExpandedKeys, setSearchExpandedKeys] = useState(null);
|
||
|
||
const keep = useMemo(() => matchKeys(index, keyword), [index, keyword]);
|
||
const allExpandable = useMemo(
|
||
() => expandableKeys(index, keep),
|
||
[index, keep],
|
||
);
|
||
const checkState = useMemo(
|
||
() => permCheckState(index, value),
|
||
[index, value],
|
||
);
|
||
|
||
const treeData = useMemo(() => {
|
||
const build = (parentCode) =>
|
||
(index.children.get(parentCode) || [])
|
||
.filter((code) => !keep || keep.has(code))
|
||
.map((code) => {
|
||
const n = index.byCode.get(code);
|
||
const meta = TYPE_META[n.type];
|
||
return {
|
||
key: code,
|
||
disableCheckbox: disabled || !n.grantable,
|
||
title: (
|
||
<span className='inline-flex flex-wrap items-center gap-1'>
|
||
<span>{n.name}</span>
|
||
{meta ? (
|
||
<Tag color={meta.color} style={{ marginInlineEnd: 0 }}>
|
||
{t(meta.label)}
|
||
</Tag>
|
||
) : null}
|
||
{n.sensitive ? (
|
||
<Tag color='error' style={{ marginInlineEnd: 0 }}>
|
||
{t('敏感')}
|
||
</Tag>
|
||
) : null}
|
||
</span>
|
||
),
|
||
children: build(code),
|
||
};
|
||
});
|
||
return build('');
|
||
}, [index, keep, disabled, t]);
|
||
|
||
const searching = keep !== null;
|
||
const currentExpanded = searching
|
||
? (searchExpandedKeys ?? allExpandable)
|
||
: (expandedKeys ?? (defaultExpandAll ? allExpandable : []));
|
||
const setCurrentExpanded = searching
|
||
? setSearchExpandedKeys
|
||
: setExpandedKeys;
|
||
|
||
return (
|
||
<div className={className}>
|
||
{showToolbar ? (
|
||
<div className='mb-2 flex flex-wrap items-center gap-2'>
|
||
<Input
|
||
allowClear
|
||
value={keyword}
|
||
prefix={<Search size={14} />}
|
||
placeholder={t('搜索权限')}
|
||
aria-label={t('搜索权限')}
|
||
style={{ flex: '1 1 180px', minWidth: 0 }}
|
||
onChange={(e) => {
|
||
setKeyword(e.target.value);
|
||
// 每次改关键字都重新展开全部命中路径
|
||
setSearchExpandedKeys(null);
|
||
}}
|
||
/>
|
||
<Button onClick={() => setCurrentExpanded(allExpandable)}>
|
||
{t('全部展开')}
|
||
</Button>
|
||
<Button onClick={() => setCurrentExpanded([])}>
|
||
{t('全部收起')}
|
||
</Button>
|
||
</div>
|
||
) : null}
|
||
<div
|
||
style={{
|
||
maxHeight: height,
|
||
overflow: 'auto',
|
||
padding: 8,
|
||
border: `1px solid ${token.colorBorderSecondary}`,
|
||
borderRadius: token.borderRadius,
|
||
}}
|
||
>
|
||
{treeData.length ? (
|
||
<Tree
|
||
checkable
|
||
// 联动与半选由本组件按"可选节点"自行计算;antd 自带的联动会把已保存的祖先当成全选
|
||
checkStrictly
|
||
selectable={false}
|
||
treeData={treeData}
|
||
checkedKeys={checkState}
|
||
expandedKeys={currentExpanded}
|
||
autoExpandParent={false}
|
||
onExpand={(keys) => setCurrentExpanded(keys)}
|
||
onCheck={(_, info) =>
|
||
onChange?.(togglePerm(index, value, info.node.key))
|
||
}
|
||
/>
|
||
) : (
|
||
<Empty
|
||
image={Empty.PRESENTED_IMAGE_SIMPLE}
|
||
description={searching ? t('没有匹配的权限') : t('暂无数据')}
|
||
/>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default PermTree;
|