2021-06-07 17:22:07 +08:00
|
|
|
<template>
|
|
|
|
|
<div>
|
2022-10-29 20:08:15 +08:00
|
|
|
<el-dialog :title="'分配“' + roleInfo?.name + '”菜单&权限'" v-model="dialogVisible" :before-close="cancel"
|
|
|
|
|
:show-close="false" width="400px">
|
|
|
|
|
<el-tree style="height: 50vh; overflow: auto" ref="menuTree" :data="resources" show-checkbox node-key="id"
|
|
|
|
|
:default-checked-keys="defaultCheckedKeys" :props="defaultProps">
|
2021-06-07 17:22:07 +08:00
|
|
|
<template #default="{ node, data }">
|
|
|
|
|
<span class="custom-tree-node">
|
2022-10-29 20:08:15 +08:00
|
|
|
<span v-if="data.type == enums.ResourceTypeEnum['MENU'].value">{{ node.label }}</span>
|
|
|
|
|
<span v-if="data.type == enums.ResourceTypeEnum['PERMISSION'].value" style="color: #67c23a">{{
|
|
|
|
|
node.label
|
|
|
|
|
}}</span>
|
2021-06-07 17:22:07 +08:00
|
|
|
</span>
|
|
|
|
|
</template>
|
|
|
|
|
</el-tree>
|
|
|
|
|
<template #footer>
|
|
|
|
|
<div class="dialog-footer">
|
2022-01-12 16:00:31 +08:00
|
|
|
<el-button @click="cancel">取 消</el-button>
|
2022-05-08 14:10:57 +08:00
|
|
|
<el-button type="primary" @click="btnOk">确 定</el-button>
|
2021-06-07 17:22:07 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
</el-dialog>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
<script lang="ts" setup>
|
|
|
|
|
import { toRefs, reactive, watch, ref } from 'vue';
|
2021-06-07 17:22:07 +08:00
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
import { roleApi } from '../api';
|
|
|
|
|
import enums from '../enums';
|
|
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
const props = defineProps({
|
|
|
|
|
visible: {
|
|
|
|
|
type: Boolean,
|
2021-06-07 17:22:07 +08:00
|
|
|
},
|
2022-10-29 20:08:15 +08:00
|
|
|
title: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
role: {
|
|
|
|
|
type: Object,
|
|
|
|
|
},
|
|
|
|
|
// 默认勾选的节点
|
|
|
|
|
defaultCheckedKeys: {
|
|
|
|
|
type: Array,
|
|
|
|
|
},
|
|
|
|
|
// 所有资源树
|
|
|
|
|
resources: {
|
|
|
|
|
type: Array,
|
|
|
|
|
},
|
|
|
|
|
})
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
//定义事件
|
|
|
|
|
const emit = defineEmits(['update:visible', 'cancel', 'val-change'])
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
const defaultProps = {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'name',
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
const menuTree: any = ref(null);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
const state = reactive({
|
|
|
|
|
dialogVisible: false,
|
|
|
|
|
roleInfo: null as any,
|
|
|
|
|
});
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
const {
|
|
|
|
|
dialogVisible,
|
|
|
|
|
roleInfo,
|
|
|
|
|
} = toRefs(state)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
watch(
|
|
|
|
|
() => props.visible,
|
|
|
|
|
(newValue) => {
|
|
|
|
|
state.dialogVisible = newValue;
|
|
|
|
|
state.roleInfo = props.role
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2022-10-29 20:08:15 +08:00
|
|
|
/**
|
|
|
|
|
* 获取所有菜单树的叶子节点
|
|
|
|
|
* @param {Object} trees 菜单树列表
|
|
|
|
|
*/
|
|
|
|
|
// const getAllLeafIds = (trees: any) => {
|
|
|
|
|
// let leafIds: any = [];
|
|
|
|
|
// for (let tree of trees) {
|
|
|
|
|
// setLeafIds(tree, leafIds);
|
|
|
|
|
// }
|
|
|
|
|
// return leafIds;
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// const setLeafIds = (tree: any, ids: any) => {
|
|
|
|
|
// if (tree.children !== null) {
|
|
|
|
|
// for (let t of tree.children) {
|
|
|
|
|
// setLeafIds(t, ids);
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// ids.push(tree.id);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
const btnOk = async () => {
|
|
|
|
|
let menuIds = menuTree.value.getCheckedKeys();
|
|
|
|
|
let halfMenuIds = menuTree.value.getHalfCheckedKeys();
|
|
|
|
|
let resources = [].concat(menuIds, halfMenuIds).join(',');
|
|
|
|
|
await roleApi.saveResources.request({
|
|
|
|
|
id: props.role!.id,
|
|
|
|
|
resourceIds: resources,
|
|
|
|
|
});
|
|
|
|
|
ElMessage.success('保存成功!');
|
|
|
|
|
emit('cancel');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const cancel = () => {
|
|
|
|
|
// 更新父组件visible prop对应的值为false
|
|
|
|
|
emit('update:visible', false);
|
|
|
|
|
emit('cancel');
|
|
|
|
|
};
|
2021-06-07 17:22:07 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style>
|
2022-10-29 20:08:15 +08:00
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
</style>
|