feat:角色 role 的数据权限 100%(基于 VbenTree)

pull/62/head
YunaiV 2025-03-31 21:53:08 +08:00
parent ee0ac825f2
commit 42c9e19f80
1 changed files with 26 additions and 32 deletions

View File

@ -23,10 +23,9 @@ const formData = ref<SystemRoleApi.SystemRole>();
const deptTree = ref<SystemDeptApi.SystemDept[]>([]); //
const deptLoading = ref(false); //
const isAllSelected = ref(false); //
const isExpanded = ref(true); //
const isDeptCheckStrictly = ref(true); //
const treeRef = ref(); //
const isExpanded = ref(false); //
const isCheckStrictly = ref(true); //
const expandedKeys = ref<number[]>([]); //
const [Form, formApi] = useVbenForm({
layout: 'horizontal',
@ -75,6 +74,7 @@ const [Modal, modalApi] = useVbenModal({
await formApi.setValues(formData.value);
//
await loadDeptTree();
toggleExpandAll();
} finally {
modalApi.lock(false);
}
@ -85,8 +85,8 @@ const [Modal, modalApi] = useVbenModal({
async function loadDeptTree() {
deptLoading.value = true;
try {
const res = await getDeptList();
deptTree.value = handleTree(res);
const data = await getDeptList();
deptTree.value = handleTree(data);
} finally {
deptLoading.value = false;
}
@ -95,40 +95,33 @@ async function loadDeptTree() {
/** 全选/全不选 */
function toggleSelectAll() {
isAllSelected.value = !isAllSelected.value;
if (treeRef.value) {
// VbenTreeAPI
if (isAllSelected.value) {
//
const allIds = getAllNodeIds(deptTree.value);
formApi.setFieldValue('dataScopeDeptIds', allIds);
} else {
//
formApi.setFieldValue('dataScopeDeptIds', []);
}
if (isAllSelected.value) {
const allIds = getAllNodeIds(deptTree.value);
formApi.setFieldValue('dataScopeDeptIds', allIds);
} else {
formApi.setFieldValue('dataScopeDeptIds', []);
}
}
/** 展开/折叠所有节点 */
function toggleExpandAll() {
async function toggleExpandAll() {
isExpanded.value = !isExpanded.value;
debugger
if (treeRef.value) {
if (isExpanded.value) {
treeRef.value.expandAll(); //
} else {
treeRef.value.collapseAll(); //
}
if (isExpanded.value) {
// ID
expandedKeys.value = getAllNodeIds(deptTree.value);
} else {
expandedKeys.value = [];
}
}
/** 切换父子联动 */
function toggleCheckStrictly() {
isDeptCheckStrictly.value = !isDeptCheckStrictly.value;
isCheckStrictly.value = !isCheckStrictly.value;
}
/** 递归获取所有节点ID */
function getAllNodeIds(nodes, ids = []) {
nodes.forEach((node) => {
/** 递归获取所有节点 ID */
function getAllNodeIds(nodes: any[], ids: number[] = []): number[] {
nodes.forEach((node: any) => {
ids.push(node.id);
if (node.children && node.children.length > 0) {
getAllNodeIds(node.children, ids);
@ -143,16 +136,17 @@ function getAllNodeIds(nodes, ids = []) {
<Form class="mx-4">
<template #dataScopeDeptIds="slotProps">
<Spin :spinning="deptLoading" class="w-full">
<!-- TODO @芋艿可优化使用 antd tree原因是更原生 -->
<VbenTree
ref="treeRef"
:tree-data="deptTree"
multiple
bordered
:default-expanded-level="isExpanded ? 100 : 0"
:expanded="expandedKeys"
v-bind="slotProps"
value-field="id"
label-field="name"
:check-strictly="!isDeptCheckStrictly"
:auto-check-parent="false"
:check-strictly="!isCheckStrictly"
/>
</Spin>
</template>
@ -165,7 +159,7 @@ function getAllNodeIds(nodes, ids = []) {
<Checkbox :checked="isExpanded" @change="toggleExpandAll">
全部展开
</Checkbox>
<Checkbox :checked="isDeptCheckStrictly" @change="toggleCheckStrictly">
<Checkbox :checked="isCheckStrictly" @change="toggleCheckStrictly">
父子联动
</Checkbox>
</div>