admin-vben5/apps/web-antd/src/views/system/role/role-auth-modal.vue

119 lines
2.9 KiB
Vue
Raw Normal View History

2024-09-25 11:47:55 +08:00
<script setup lang="ts">
import { ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
2024-09-25 14:46:02 +08:00
import { cloneDeep } from '@vben/utils';
2024-09-25 11:47:55 +08:00
2024-10-17 15:16:22 +08:00
import { useVbenForm } from '#/adapter/form';
2024-09-25 11:47:55 +08:00
import { roleDataScope, roleDeptTree, roleInfo } from '#/api/system/role';
import { TreeSelectPanel } from '#/components/tree';
import { authModalSchemas } from './data';
const emit = defineEmits<{ reload: [] }>();
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
},
2024-09-26 08:10:30 +08:00
layout: 'vertical',
2024-09-25 11:47:55 +08:00
schema: authModalSchemas(),
showDefaultActions: false,
});
const deptTree = ref<any[]>([]);
async function setupDeptTree(id: number | string) {
const resp = await roleDeptTree(id);
formApi.setFieldValue('deptIds', resp.checkedKeys);
// 设置菜单信息
deptTree.value = resp.depts;
}
const [BasicModal, modalApi] = useVbenModal({
fullscreenButton: false,
onCancel: handleCancel,
onConfirm: handleConfirm,
onOpenChange: async (isOpen) => {
if (!isOpen) {
return null;
}
modalApi.modalLoading(true);
const { id } = modalApi.getData() as { id: number | string };
setupDeptTree(id);
const record = await roleInfo(id);
2024-09-25 14:46:02 +08:00
await formApi.setValues(record);
2024-09-25 11:47:55 +08:00
modalApi.modalLoading(false);
},
});
/**
* 这里拿到的是一个数组ref
*/
const deptSelectRef = ref();
async function handleConfirm() {
try {
modalApi.modalLoading(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
// formApi.getValues拿到的是一个readonly对象不能直接修改需要cloneDeep
const data = cloneDeep(await formApi.getValues());
// 不为自定义权限的话 删除部门id
if (data.dataScope === '2') {
const deptIds = deptSelectRef.value?.[0]?.getCheckedKeys() ?? [];
data.deptIds = deptIds;
} else {
data.deptIds = [];
}
await roleDataScope(data);
emit('reload');
await handleCancel();
} catch (error) {
console.error(error);
} finally {
modalApi.modalLoading(false);
}
}
async function handleCancel() {
modalApi.close();
await formApi.resetForm();
}
/**
* 通过回调更新 无法通过v-model
* @param value 菜单选择是否严格模式
*/
function handleCheckStrictlyChange(value: boolean) {
formApi.setFieldValue('deptCheckStrictly', value);
}
</script>
<template>
<BasicModal
:close-on-click-modal="false"
2024-09-26 08:10:30 +08:00
class="min-h-[600px] w-[550px]"
2024-09-25 11:47:55 +08:00
title="分配权限"
>
<BasicForm>
<template #deptIds="slotProps">
<TreeSelectPanel
ref="deptSelectRef"
v-bind="slotProps"
:check-strictly="formApi.form.values.deptCheckStrictly"
:expand-all-on-init="true"
:tree-data="deptTree"
@check-strictly-change="handleCheckStrictlyChange"
/>
</template>
</BasicForm>
</BasicModal>
</template>