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

119 lines
3.1 KiB
Vue
Raw Normal View History

2025-03-31 15:47:48 +08:00
<script lang="ts" setup>
import type { SystemRoleApi } from '#/api/system/role';
2025-03-31 16:53:51 +08:00
import type { SystemDeptApi } from '#/api/system/dept';
2025-03-31 15:47:48 +08:00
2025-03-31 16:53:51 +08:00
import { VbenTree } from '@vben/common-ui';
2025-03-31 15:47:48 +08:00
import { useVbenModal } from '@vben/common-ui';
import { Button, message } from 'ant-design-vue';
2025-03-31 16:53:51 +08:00
import { ref } from 'vue';
2025-03-31 15:47:48 +08:00
import { $t } from '#/locales';
2025-03-31 16:53:51 +08:00
import { useVbenForm } from '#/adapter/form';
import { getRole } from '#/api/system/role';
import { assignRoleDataScope} from '#/api/system/permission';
import { getDeptList } from '#/api/system/dept';
import { handleTree } from '#/utils/tree';
2025-03-31 15:47:48 +08:00
import { useAssignDataPermissionFormSchema } from '../data';
2025-03-31 16:53:51 +08:00
import { SystemDataScopeEnum } from '#/utils/constants';
2025-03-31 15:47:48 +08:00
const emit = defineEmits(['success']);
const formData = ref<SystemRoleApi.SystemRole>();
2025-03-31 16:53:51 +08:00
const deptTree = ref<SystemDeptApi.SystemDept[]>([]); // 部门树
const deptLoading = ref(false); // 加载部门列表
2025-03-31 15:47:48 +08:00
const [Form, formApi] = useVbenForm({
layout: 'horizontal',
schema: useAssignDataPermissionFormSchema(),
showDefaultActions: false,
});
function resetForm() {
formApi.resetForm();
formApi.setValues(formData.value || {});
}
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 15:47:48 +08:00
await modalApi.close();
emit('success');
message.success({
content: $t('ui.actionMessage.operationSuccess'),
key: 'action_process_msg',
});
} finally {
modalApi.lock(false);
}
},
async onOpenChange(isOpen) {
if (!isOpen) {
return;
}
const data = modalApi.getData<SystemRoleApi.SystemRole>();
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
formData.value = await getRole(data.id as number);
await formApi.setValues(formData.value);
2025-03-31 16:53:51 +08:00
// 加载部门列表
await loadDeptTree();
2025-03-31 15:47:48 +08:00
} finally {
modalApi.lock(false);
}
},
});
2025-03-31 16:53:51 +08:00
/** 加载部门树 */
async function loadDeptTree() {
deptLoading.value = true;
2025-03-31 15:47:48 +08:00
try {
const res = await getDeptList();
2025-03-31 16:53:51 +08:00
deptTree.value = handleTree(res);
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
}
}
</script>
<template>
<Modal title="数据权限">
<Form class="mx-4">
<template #dataScopeDeptIds="slotProps">
2025-03-31 16:53:51 +08:00
<Spin :spinning="deptLoading" class="w-full">
2025-03-31 15:47:48 +08:00
<VbenTree
2025-03-31 16:53:51 +08:00
:tree-data="deptTree"
2025-03-31 15:47:48 +08:00
multiple
bordered
:default-expanded-level="2"
v-bind="slotProps"
value-field="id"
label-field="name"
/>
</Spin>
</template>
</Form>
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
</div>
</template>
</Modal>
</template>