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

pull/62/head
YunaiV 2025-03-31 18:13:02 +08:00
parent 36838c910b
commit ee0ac825f2
1 changed files with 69 additions and 13 deletions

View File

@ -4,13 +4,13 @@ import type { SystemDeptApi } from '#/api/system/dept';
import { VbenTree } from '@vben/common-ui';
import { useVbenModal } from '@vben/common-ui';
import { Button, message } from 'ant-design-vue';
import { message, Checkbox } from 'ant-design-vue';
import { ref } from 'vue';
import { $t } from '#/locales';
import { useVbenForm } from '#/adapter/form';
import { getRole } from '#/api/system/role';
import { assignRoleDataScope} from '#/api/system/permission';
import { assignRoleDataScope } from '#/api/system/permission';
import { getDeptList } from '#/api/system/dept';
import { handleTree } from '#/utils/tree';
@ -22,6 +22,11 @@ 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 [Form, formApi] = useVbenForm({
layout: 'horizontal',
@ -29,11 +34,6 @@ const [Form, formApi] = useVbenForm({
showDefaultActions: false,
});
function resetForm() {
formApi.resetForm();
formApi.setValues(formData.value || {});
}
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
@ -46,7 +46,10 @@ const [Modal, modalApi] = useVbenModal({
await assignRoleDataScope({
roleId: data.id,
dataScope: data.dataScope,
dataScopeDeptIds: data.dataScope === SystemDataScopeEnum.DEPT_CUSTOM ? data.dataScopeDeptIds : undefined,
dataScopeDeptIds:
data.dataScope === SystemDataScopeEnum.DEPT_CUSTOM
? data.dataScopeDeptIds
: undefined,
});
await modalApi.close();
emit('success');
@ -88,6 +91,51 @@ async function loadDeptTree() {
deptLoading.value = false;
}
}
/** 全选/全不选 */
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', []);
}
}
}
/** 展开/折叠所有节点 */
function toggleExpandAll() {
isExpanded.value = !isExpanded.value;
debugger
if (treeRef.value) {
if (isExpanded.value) {
treeRef.value.expandAll(); //
} else {
treeRef.value.collapseAll(); //
}
}
}
/** 切换父子联动 */
function toggleCheckStrictly() {
isDeptCheckStrictly.value = !isDeptCheckStrictly.value;
}
/** 递归获取所有节点ID */
function getAllNodeIds(nodes, ids = []) {
nodes.forEach((node) => {
ids.push(node.id);
if (node.children && node.children.length > 0) {
getAllNodeIds(node.children, ids);
}
});
return ids;
}
</script>
<template>
@ -96,22 +144,30 @@ async function loadDeptTree() {
<template #dataScopeDeptIds="slotProps">
<Spin :spinning="deptLoading" class="w-full">
<VbenTree
ref="treeRef"
:tree-data="deptTree"
multiple
bordered
:default-expanded-level="2"
:default-expanded-level="isExpanded ? 100 : 0"
v-bind="slotProps"
value-field="id"
label-field="name"
:check-strictly="!isDeptCheckStrictly"
/>
</Spin>
</template>
</Form>
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
<div class="flex flex-auto items-center">
<Checkbox :checked="isAllSelected" @change="toggleSelectAll">
全选
</Checkbox>
<Checkbox :checked="isExpanded" @change="toggleExpandAll">
全部展开
</Checkbox>
<Checkbox :checked="isDeptCheckStrictly" @change="toggleCheckStrictly">
父子联动
</Checkbox>
</div>
</template>
</Modal>