Files
iot-device-management-frontend/apps/web-antd/src/views/system/role/modules/assign-data-permission-form.vue

169 lines
4.5 KiB
Vue
Raw Normal View History

2025-03-31 15:47:48 +08:00
<script lang="ts" setup>
2025-03-31 16:53:51 +08:00
import type { SystemDeptApi } from '#/api/system/dept';
2025-04-22 11:25:11 +08:00
import type { SystemRoleApi } from '#/api/system/role';
2025-03-31 15:47:48 +08:00
2025-03-31 16:53:51 +08:00
import { ref } from 'vue';
2025-04-22 11:25:11 +08:00
import { useVbenModal, VbenTree } from '@vben/common-ui';
import { handleTree } from '@vben/utils';
2025-04-22 11:25:11 +08:00
2025-04-26 13:34:10 +08:00
import { Checkbox, message } from 'ant-design-vue';
2025-04-22 11:25:11 +08:00
2025-03-31 16:53:51 +08:00
import { useVbenForm } from '#/adapter/form';
import { getDeptList } from '#/api/system/dept';
2025-04-22 11:25:11 +08:00
import { assignRoleDataScope } from '#/api/system/permission';
import { getRole } from '#/api/system/role';
import { $t } from '#/locales';
2025-05-06 23:23:42 +08:00
import { SystemDataScopeEnum } from '#/utils';
2025-03-31 15:47:48 +08:00
import { useAssignDataPermissionFormSchema } from '../data';
const emit = defineEmits(['success']);
2025-04-22 22:10:33 +08:00
const deptTree = ref<SystemDeptApi.Dept[]>([]); // 部门树
2025-03-31 16:53:51 +08:00
const deptLoading = ref(false); // 加载部门列表
const isAllSelected = ref(false); // 全选状态
const isExpanded = ref(false); // 展开状态
const isCheckStrictly = ref(true); // 父子联动状态
const expandedKeys = ref<number[]>([]); // 展开的节点
2025-03-31 15:47:48 +08:00
const [Form, formApi] = useVbenForm({
2025-04-26 13:34:10 +08:00
commonConfig: {
componentProps: {
class: 'w-full',
},
formItemClass: 'col-span-2',
labelWidth: 80,
},
2025-03-31 15:47:48 +08:00
layout: 'horizontal',
schema: useAssignDataPermissionFormSchema(),
showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
2025-03-31 16:53:51 +08:00
const data = await formApi.getValues();
2025-03-31 15:47:48 +08:00
try {
2025-03-31 16:53:51 +08:00
await assignRoleDataScope({
roleId: data.id,
dataScope: data.dataScope,
dataScopeDeptIds:
data.dataScope === SystemDataScopeEnum.DEPT_CUSTOM
? data.dataScopeDeptIds
: undefined,
2025-03-31 16:53:51 +08:00
});
2025-03-31 15:47:48 +08:00
await modalApi.close();
emit('success');
message.success($t('ui.actionMessage.operationSuccess'));
2025-03-31 15:47:48 +08:00
} finally {
modalApi.unlock();
2025-03-31 15:47:48 +08:00
}
},
2025-04-03 22:56:03 +08:00
async onOpenChange(isOpen: boolean) {
2025-03-31 15:47:48 +08:00
if (!isOpen) {
return;
}
2025-04-22 22:10:33 +08:00
const data = modalApi.getData<SystemRoleApi.Role>();
2025-03-31 15:47:48 +08:00
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
await formApi.setValues(await getRole(data.id as number));
2025-03-31 16:53:51 +08:00
// 加载部门列表
await loadDeptTree();
toggleExpandAll();
2025-03-31 15:47:48 +08:00
} finally {
modalApi.unlock();
2025-03-31 15:47:48 +08:00
}
},
});
2025-03-31 16:53:51 +08:00
/** 加载部门树 */
async function loadDeptTree() {
deptLoading.value = true;
2025-03-31 15:47:48 +08:00
try {
const data = await getDeptList();
2025-04-22 22:10:33 +08:00
deptTree.value = handleTree(data) as SystemDeptApi.Dept[];
2025-03-31 15:47:48 +08:00
} finally {
2025-03-31 16:53:51 +08:00
deptLoading.value = false;
2025-03-31 15:47:48 +08:00
}
}
/** 全选/全不选 */
function toggleSelectAll() {
isAllSelected.value = !isAllSelected.value;
if (isAllSelected.value) {
const allIds = getAllNodeIds(deptTree.value);
formApi.setFieldValue('dataScopeDeptIds', allIds);
} else {
formApi.setFieldValue('dataScopeDeptIds', []);
}
}
/** 展开/折叠所有节点 */
function toggleExpandAll() {
isExpanded.value = !isExpanded.value;
2025-04-22 11:25:11 +08:00
// 获取所有节点的 ID
expandedKeys.value = isExpanded.value ? getAllNodeIds(deptTree.value) : [];
}
/** 切换父子联动 */
function toggleCheckStrictly() {
isCheckStrictly.value = !isCheckStrictly.value;
}
/** 递归获取所有节点 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);
}
});
return ids;
}
2025-03-31 15:47:48 +08:00
</script>
<template>
2025-04-26 13:34:10 +08:00
<Modal title="数据权限" class="w-[40%]">
2025-03-31 15:47:48 +08:00
<Form class="mx-4">
<template #dataScopeDeptIds="slotProps">
2025-04-26 13:34:10 +08:00
<!-- <Spin :spinning="deptLoading"> -->
<!-- TODO @芋艿可优化使用 antd tree原因是更原生 -->
<VbenTree
:tree-data="deptTree"
multiple
bordered
:expanded="expandedKeys"
v-bind="slotProps"
value-field="id"
label-field="name"
:auto-check-parent="false"
:check-strictly="!isCheckStrictly"
/>
<!-- </Spin> -->
2025-03-31 15:47:48 +08:00
</template>
</Form>
<template #prepend-footer>
<div class="flex flex-auto items-center">
<Checkbox :checked="isAllSelected" @change="toggleSelectAll">
全选
</Checkbox>
<Checkbox :checked="isExpanded" @change="toggleExpandAll">
全部展开
</Checkbox>
<Checkbox :checked="isCheckStrictly" @change="toggleCheckStrictly">
父子联动
</Checkbox>
2025-03-31 15:47:48 +08:00
</div>
</template>
</Modal>
</template>