Files
mayfly-go/frontend/src/views/system/role/ResourceEdit.vue

115 lines
3.1 KiB
Vue
Raw Normal View History

<template>
<div>
2024-11-20 22:43:53 +08:00
<el-dialog
:title="$t('system.role.allocateMenuTitle', { roleName: roleInfo?.name })"
2025-04-26 17:37:09 +08:00
v-model="visible"
:before-close="onCancel"
2024-11-20 22:43:53 +08:00
: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"
>
<template #default="{ node, data }">
<span class="custom-tree-node">
<SvgIcon :name="getMenuIcon(data)" class="mb-0.5 mr-0.5" />
2024-11-20 22:43:53 +08:00
<span v-if="data.type == ResourceTypeEnum.Menu.value">{{ $t(node.label) }}</span>
<span v-if="data.type == ResourceTypeEnum.Permission.value" style="color: #67c23a">{{ $t(node.label) }}</span>
</span>
</template>
</el-tree>
<template #footer>
<div class="dialog-footer">
<el-button :loading="state.submiting" @click="onCancel">{{ $t('common.cancel') }}</el-button>
<el-button :loading="state.submiting" type="primary" @click="onConfirm">{{ $t('common.confirm') }}</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script lang="ts" setup>
import { toRefs, reactive, watch, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { roleApi } from '../api';
import { ResourceTypeEnum } from '../enums';
2024-11-20 22:43:53 +08:00
import { useI18n } from 'vue-i18n';
import SvgIcon from '@/components/svgIcon/index.vue';
import { getMenuIcon } from '../resource';
2024-11-20 22:43:53 +08:00
const { t } = useI18n();
const props = defineProps({
title: {
type: String,
},
role: {
type: Object,
},
// 默认勾选的节点
defaultCheckedKeys: {
type: Array,
},
// 所有资源树
resources: {
type: Array,
},
});
2025-04-26 17:37:09 +08:00
const visible = defineModel<boolean>('visible', { default: false });
//定义事件
2025-04-26 17:37:09 +08:00
const emit = defineEmits(['cancel', 'val-change']);
const defaultProps = {
children: 'children',
label: 'name',
};
const menuTree: any = ref(null);
const state = reactive({
roleInfo: null as any,
2024-04-23 11:35:45 +08:00
submiting: false,
});
2025-04-26 17:37:09 +08:00
const { roleInfo } = toRefs(state);
watch(
2025-04-26 17:37:09 +08:00
() => visible,
(newValue) => {
state.roleInfo = props.role;
}
);
const onConfirm = async () => {
let menuIds = menuTree.value.getCheckedKeys();
let halfMenuIds = menuTree.value.getHalfCheckedKeys();
let resources = [].concat(menuIds, halfMenuIds).join(',');
2024-04-23 11:35:45 +08:00
try {
state.submiting = true;
await roleApi.saveResources.request({
id: props.role!.id,
resourceIds: resources,
});
2024-11-20 22:43:53 +08:00
ElMessage.success(t('common.saveSuccess'));
2024-04-23 11:35:45 +08:00
emit('cancel');
} finally {
state.submiting = false;
}
};
const onCancel = () => {
2025-04-26 17:37:09 +08:00
visible.value = false;
emit('cancel');
};
</script>
<style></style>