feat: ✨ 更换权限分配弹窗类型为 drawer 并添加父子联动开关
将权限分配弹窗从 dialog 更换为 drawer,提升用户体验。 添加父子联动开关以解决选择父节点时无需选择子节点的问题。 https://gitee.com/youlaiorg/vue3-element-admin/issues/I9T2BA #I9T2BA
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
<el-card shadow="never" class="table-container">
|
<el-card shadow="never" class="table-container">
|
||||||
<template #header>
|
<template #header>
|
||||||
<el-button type="success" @click="openDialog()"
|
<el-button type="success" @click="handleOpenDialog()"
|
||||||
><i-ep-plus />新增</el-button
|
><i-ep-plus />新增</el-button
|
||||||
>
|
>
|
||||||
<el-button
|
<el-button
|
||||||
@@ -60,7 +60,7 @@
|
|||||||
type="primary"
|
type="primary"
|
||||||
size="small"
|
size="small"
|
||||||
link
|
link
|
||||||
@click="openMenuDialog(scope.row)"
|
@click="handleOpenAssignPermDialog(scope.row)"
|
||||||
>
|
>
|
||||||
<i-ep-position />分配权限
|
<i-ep-position />分配权限
|
||||||
</el-button>
|
</el-button>
|
||||||
@@ -68,7 +68,7 @@
|
|||||||
type="primary"
|
type="primary"
|
||||||
size="small"
|
size="small"
|
||||||
link
|
link
|
||||||
@click="openDialog(scope.row.id)"
|
@click="handleOpenDialog(scope.row.id)"
|
||||||
>
|
>
|
||||||
<i-ep-edit />编辑
|
<i-ep-edit />编辑
|
||||||
</el-button>
|
</el-button>
|
||||||
@@ -98,7 +98,7 @@
|
|||||||
v-model="dialog.visible"
|
v-model="dialog.visible"
|
||||||
:title="dialog.title"
|
:title="dialog.title"
|
||||||
width="500px"
|
width="500px"
|
||||||
@close="closeDialog"
|
@close="handleCloseDialog"
|
||||||
>
|
>
|
||||||
<el-form
|
<el-form
|
||||||
ref="roleFormRef"
|
ref="roleFormRef"
|
||||||
@@ -143,57 +143,93 @@
|
|||||||
<template #footer>
|
<template #footer>
|
||||||
<div class="dialog-footer">
|
<div class="dialog-footer">
|
||||||
<el-button type="primary" @click="handleSubmit">确 定</el-button>
|
<el-button type="primary" @click="handleSubmit">确 定</el-button>
|
||||||
<el-button @click="closeDialog">取 消</el-button>
|
<el-button @click="handleCloseDialog">取 消</el-button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
|
|
||||||
<!-- 分配菜单弹窗 -->
|
<!-- 分配权限弹窗 -->
|
||||||
<el-dialog
|
<el-drawer
|
||||||
v-model="menuDialogVisible"
|
v-model="assignPermDialogVisible"
|
||||||
:title="'【' + checkedRole.name + '】权限分配'"
|
:title="'【' + checkedRole.name + '】权限分配'"
|
||||||
width="800px"
|
size="500"
|
||||||
>
|
>
|
||||||
<el-scrollbar v-loading="loading" max-height="600px">
|
<div class="flex-x-between">
|
||||||
|
<el-input
|
||||||
|
v-model="permKeywords"
|
||||||
|
clearable
|
||||||
|
class="w-[200px]"
|
||||||
|
placeholder="菜单权限名称"
|
||||||
|
>
|
||||||
|
<template #prefix>
|
||||||
|
<i-ep-search />
|
||||||
|
</template>
|
||||||
|
</el-input>
|
||||||
|
|
||||||
|
<div class="flex-center">
|
||||||
|
<el-button type="primary" size="small" plain @click="togglePermTree"
|
||||||
|
><i-ep-switch />{{ isExpanded ? "收缩" : "展开" }}</el-button
|
||||||
|
>
|
||||||
|
<el-checkbox
|
||||||
|
v-model="parentChildLinked"
|
||||||
|
@change="handleparentChildLinkedChange"
|
||||||
|
class="ml-5"
|
||||||
|
>父子联动
|
||||||
|
</el-checkbox>
|
||||||
|
|
||||||
|
<el-tooltip placement="bottom">
|
||||||
|
<template #content>
|
||||||
|
如果只需勾选菜单权限,不需要勾选子菜单或者按钮权限,请关闭父子联动
|
||||||
|
</template>
|
||||||
|
<i-ep-QuestionFilled
|
||||||
|
class="ml-1 color-[--el-color-primary] inline-block cursor-pointer"
|
||||||
|
/>
|
||||||
|
</el-tooltip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<el-tree
|
<el-tree
|
||||||
ref="menuRef"
|
ref="permTreeRef"
|
||||||
node-key="value"
|
node-key="value"
|
||||||
show-checkbox
|
show-checkbox
|
||||||
:data="menuList"
|
:data="menuPermOptions"
|
||||||
|
:filter-node-method="handlePermFilter"
|
||||||
:default-expand-all="true"
|
:default-expand-all="true"
|
||||||
|
:check-on-click-node="true"
|
||||||
|
:check-strictly="!parentChildLinked"
|
||||||
|
class="mt-5"
|
||||||
>
|
>
|
||||||
<template #default="{ data }">
|
<template #default="{ data }">
|
||||||
{{ data.label }}
|
{{ data.label }}
|
||||||
</template>
|
</template>
|
||||||
</el-tree>
|
</el-tree>
|
||||||
</el-scrollbar>
|
|
||||||
|
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<div class="dialog-footer">
|
<div class="dialog-footer">
|
||||||
<el-button type="primary" @click="handleRoleMenuSubmit"
|
<el-button type="primary" @click="handleAssignPermSubmit"
|
||||||
>确 定</el-button
|
>确 定</el-button
|
||||||
>
|
>
|
||||||
<el-button @click="menuDialogVisible = false">取 消</el-button>
|
<el-button @click="assignPermDialogVisible = false">取 消</el-button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-drawer>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import RoleAPI from "@/api/role";
|
|
||||||
import MenuAPI from "@/api/menu";
|
|
||||||
|
|
||||||
import { RolePageVO, RoleForm, RoleQuery } from "@/api/role/model";
|
|
||||||
|
|
||||||
defineOptions({
|
defineOptions({
|
||||||
name: "Role",
|
name: "Role",
|
||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
import RoleAPI from "@/api/role";
|
||||||
|
import MenuAPI from "@/api/menu";
|
||||||
|
|
||||||
|
import { RolePageVO, RoleForm, RoleQuery } from "@/api/role/model";
|
||||||
|
|
||||||
const queryFormRef = ref(ElForm);
|
const queryFormRef = ref(ElForm);
|
||||||
const roleFormRef = ref(ElForm);
|
const roleFormRef = ref(ElForm);
|
||||||
const menuRef = ref(ElTree);
|
const permTreeRef = ref<InstanceType<typeof ElTree>>();
|
||||||
|
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const ids = ref<number[]>([]);
|
const ids = ref<number[]>([]);
|
||||||
@@ -204,13 +240,17 @@ const queryParams = reactive<RoleQuery>({
|
|||||||
pageSize: 10,
|
pageSize: 10,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 角色表格数据
|
||||||
const roleList = ref<RolePageVO[]>();
|
const roleList = ref<RolePageVO[]>();
|
||||||
|
// 菜单权限下拉
|
||||||
|
const menuPermOptions = ref<OptionType[]>([]);
|
||||||
|
|
||||||
|
// 弹窗
|
||||||
const dialog = reactive({
|
const dialog = reactive({
|
||||||
title: "",
|
title: "",
|
||||||
visible: false,
|
visible: false,
|
||||||
});
|
});
|
||||||
|
// 角色表单
|
||||||
const formData = reactive<RoleForm>({
|
const formData = reactive<RoleForm>({
|
||||||
sort: 1,
|
sort: 1,
|
||||||
status: 1,
|
status: 1,
|
||||||
@@ -225,17 +265,20 @@ const rules = reactive({
|
|||||||
status: [{ required: true, message: "请选择状态", trigger: "blur" }],
|
status: [{ required: true, message: "请选择状态", trigger: "blur" }],
|
||||||
});
|
});
|
||||||
|
|
||||||
const menuDialogVisible = ref(false);
|
// 选中的角色
|
||||||
|
|
||||||
const menuList = ref<OptionType[]>([]);
|
|
||||||
|
|
||||||
interface CheckedRole {
|
interface CheckedRole {
|
||||||
id?: number;
|
id?: number;
|
||||||
name?: string;
|
name?: string;
|
||||||
}
|
}
|
||||||
let checkedRole: CheckedRole = reactive({});
|
const checkedRole = ref<CheckedRole>({});
|
||||||
|
const assignPermDialogVisible = ref(false);
|
||||||
|
|
||||||
/** 查询 */
|
const permKeywords = ref("");
|
||||||
|
const isExpanded = ref(true);
|
||||||
|
|
||||||
|
const parentChildLinked = ref(true);
|
||||||
|
|
||||||
|
// 查询
|
||||||
function handleQuery() {
|
function handleQuery() {
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
RoleAPI.getPage(queryParams)
|
RoleAPI.getPage(queryParams)
|
||||||
@@ -247,20 +290,20 @@ function handleQuery() {
|
|||||||
loading.value = false;
|
loading.value = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/** 重置查询 */
|
// 重置查询
|
||||||
function handleResetQuery() {
|
function handleResetQuery() {
|
||||||
queryFormRef.value.resetFields();
|
queryFormRef.value.resetFields();
|
||||||
queryParams.pageNum = 1;
|
queryParams.pageNum = 1;
|
||||||
handleQuery();
|
handleQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 行checkbox 选中事件 */
|
// 行选中事件
|
||||||
function handleSelectionChange(selection: any) {
|
function handleSelectionChange(selection: any) {
|
||||||
ids.value = selection.map((item: any) => item.id);
|
ids.value = selection.map((item: any) => item.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 打开角色表单弹窗 */
|
// 打开弹窗
|
||||||
function openDialog(roleId?: number) {
|
function handleOpenDialog(roleId?: number) {
|
||||||
dialog.visible = true;
|
dialog.visible = true;
|
||||||
if (roleId) {
|
if (roleId) {
|
||||||
dialog.title = "修改角色";
|
dialog.title = "修改角色";
|
||||||
@@ -272,7 +315,7 @@ function openDialog(roleId?: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 角色保存提交 */
|
// 角色保存提交
|
||||||
function handleSubmit() {
|
function handleSubmit() {
|
||||||
roleFormRef.value.validate((valid: any) => {
|
roleFormRef.value.validate((valid: any) => {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
@@ -282,7 +325,7 @@ function handleSubmit() {
|
|||||||
RoleAPI.update(roleId, formData)
|
RoleAPI.update(roleId, formData)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
ElMessage.success("修改成功");
|
ElMessage.success("修改成功");
|
||||||
closeDialog();
|
handleCloseDialog();
|
||||||
handleResetQuery();
|
handleResetQuery();
|
||||||
})
|
})
|
||||||
.finally(() => (loading.value = false));
|
.finally(() => (loading.value = false));
|
||||||
@@ -290,7 +333,7 @@ function handleSubmit() {
|
|||||||
RoleAPI.add(formData)
|
RoleAPI.add(formData)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
ElMessage.success("新增成功");
|
ElMessage.success("新增成功");
|
||||||
closeDialog();
|
handleCloseDialog();
|
||||||
handleResetQuery();
|
handleResetQuery();
|
||||||
})
|
})
|
||||||
.finally(() => (loading.value = false));
|
.finally(() => (loading.value = false));
|
||||||
@@ -299,14 +342,10 @@ function handleSubmit() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 关闭表单弹窗 */
|
// 关闭表单弹窗
|
||||||
function closeDialog() {
|
function handleCloseDialog() {
|
||||||
dialog.visible = false;
|
dialog.visible = false;
|
||||||
resetForm();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 重置表单 */
|
|
||||||
function resetForm() {
|
|
||||||
roleFormRef.value.resetFields();
|
roleFormRef.value.resetFields();
|
||||||
roleFormRef.value.clearValidate();
|
roleFormRef.value.clearValidate();
|
||||||
|
|
||||||
@@ -315,7 +354,7 @@ function resetForm() {
|
|||||||
formData.status = 1;
|
formData.status = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 删除角色 */
|
// 删除角色
|
||||||
function handleDelete(roleId?: number) {
|
function handleDelete(roleId?: number) {
|
||||||
const roleIds = [roleId || ids.value].join(",");
|
const roleIds = [roleId || ids.value].join(",");
|
||||||
if (!roleIds) {
|
if (!roleIds) {
|
||||||
@@ -338,26 +377,25 @@ function handleDelete(roleId?: number) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 打开分配菜单弹窗 */
|
// 打开分配权限
|
||||||
async function openMenuDialog(row: RolePageVO) {
|
async function handleOpenAssignPermDialog(row: RolePageVO) {
|
||||||
const roleId = row.id;
|
const roleId = row.id;
|
||||||
if (roleId) {
|
if (roleId) {
|
||||||
checkedRole = {
|
assignPermDialogVisible.value = true;
|
||||||
id: roleId,
|
|
||||||
name: row.name,
|
|
||||||
};
|
|
||||||
menuDialogVisible.value = true;
|
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
|
|
||||||
|
checkedRole.value.id = roleId;
|
||||||
|
checkedRole.value.name = row.name;
|
||||||
|
|
||||||
// 获取所有的菜单
|
// 获取所有的菜单
|
||||||
menuList.value = await MenuAPI.getOptions();
|
menuPermOptions.value = await MenuAPI.getOptions();
|
||||||
|
|
||||||
// 回显角色已拥有的菜单
|
// 回显角色已拥有的菜单
|
||||||
RoleAPI.getRoleMenuIds(roleId)
|
RoleAPI.getRoleMenuIds(roleId)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
const checkedMenuIds = data;
|
const checkedMenuIds = data;
|
||||||
checkedMenuIds.forEach((menuId) =>
|
checkedMenuIds.forEach((menuId) =>
|
||||||
menuRef.value.setChecked(menuId, true, false)
|
permTreeRef.value!.setChecked(menuId, true, false)
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
@@ -366,19 +404,19 @@ async function openMenuDialog(row: RolePageVO) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 角色分配菜单保存提交 */
|
// 分配权限提交
|
||||||
function handleRoleMenuSubmit() {
|
function handleAssignPermSubmit() {
|
||||||
const roleId = checkedRole.id;
|
const roleId = checkedRole.value.id;
|
||||||
if (roleId) {
|
if (roleId) {
|
||||||
const checkedMenuIds: number[] = menuRef.value
|
const checkedMenuIds: number[] = permTreeRef
|
||||||
.getCheckedNodes(false, true)
|
.value!.getCheckedNodes(false, true)
|
||||||
.map((node: any) => node.value);
|
.map((node: any) => node.value);
|
||||||
|
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
RoleAPI.updateRoleMenus(roleId, checkedMenuIds)
|
RoleAPI.updateRoleMenus(roleId, checkedMenuIds)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
ElMessage.success("分配权限成功");
|
ElMessage.success("分配权限成功");
|
||||||
menuDialogVisible.value = false;
|
assignPermDialogVisible.value = false;
|
||||||
handleResetQuery();
|
handleResetQuery();
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
@@ -387,6 +425,38 @@ function handleRoleMenuSubmit() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 切换菜单树展开状态
|
||||||
|
function togglePermTree() {
|
||||||
|
isExpanded.value = !isExpanded.value;
|
||||||
|
if (permTreeRef.value) {
|
||||||
|
Object.values(permTreeRef.value.store.nodesMap).forEach((node: any) => {
|
||||||
|
if (isExpanded.value) {
|
||||||
|
node.expand();
|
||||||
|
} else {
|
||||||
|
node.collapse();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
watch(permKeywords, (val) => {
|
||||||
|
permTreeRef.value!.filter(val);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 权限筛选
|
||||||
|
function handlePermFilter(
|
||||||
|
value: string,
|
||||||
|
data: {
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
if (!value) return true;
|
||||||
|
return data.label.includes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleparentChildLinkedChange(val: any) {
|
||||||
|
parentChildLinked.value = val;
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
handleQuery();
|
handleQuery();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user