EditPrivilegePage.vue 6.27 KB
<template>
    <a-card title="权限管理" :bordered="false">
        <template #extra>
            <a-button class="mr-10" type="primary" :disabled="submitted" @click="submitForm">保存</a-button>
            <a-button @click="$goBack()">返回</a-button>
        </template>
        <div class="scroll-block">
            <a-collapse v-model:activeKey="activeKey" collapsible="icon">
                <a-collapse-panel v-for="(menu, index) in privilegeTree" :key="index">
                    <template #header>
                        <a-checkbox v-model:checked="menu.checked" @change="onCheckAllChange(menu)">
                            <MenuOutlined v-if="menu.type === 'MENU'" class="mr-5"/>
                            <ToolOutlined v-else class="mr-5"/>
                            {{ menu.title }}
                        </a-checkbox>
                    </template>
                    <!--                    <template #extra>-->
                    <!--                        <a-checkbox v-model:checked="menu.unselectable" @change="onCheckAllChange(menu)">-->
                    <!--                            全选-->
                    <!--                        </a-checkbox>-->
                    <!--                    </template>-->
                    <a-collapse v-model:activeKey="activeSubKey" ghost collapsible="icon">
                        <a-collapse-panel v-for="(subMenu ) in menu.children" :key="subMenu.key">
                            <template #header>
                                <a-checkbox v-model:checked="subMenu.checked" @change="onCheckAllChange(subMenu)">
                                    <MenuOutlined v-if="subMenu.type === 'MENU'" class="mr-5"/>
                                    <ToolOutlined v-else class="mr-5"/>
                                    {{ subMenu.title }}
                                </a-checkbox>
                            </template>
                            <a-row :gutter="[4, 4]">
                                <a-col :offset="2" :span="4" class="child-col-item" v-for="colItem in subMenu?.children" :key="colItem.key">
                                    <a-checkbox v-model:checked="colItem.checked">
                                        <MenuOutlined v-if="colItem.type === 'MENU'" class="mr-5"/>
                                        <ToolOutlined v-else class="mr-5"/>
                                        {{ colItem?.title }}
                                    </a-checkbox>
                                </a-col>
                            </a-row>
                        </a-collapse-panel>
                    </a-collapse>
                </a-collapse-panel>
            </a-collapse>
        </div>
        <a-row class="mt-16">
            <a-col :offset="5">
                <a-button class="mr-10" type="primary" :disabled="submitted" @click="submitForm">保存</a-button>
                <a-button @click="$goBack()">返回</a-button>
            </a-col>
        </a-row>
    </a-card>
</template>

<script lang="ts">
import {Component, Vue} from "vue-facing-decorator";
import {Role} from "lib/src/entity/security/entity/Role";
import {TreeItem} from "lib/src/core/bean/TreeItem";
import {message} from "ant-design-vue";
import {SecurityController} from "@/api/SecurityController";
import {MenuOutlined, ToolOutlined} from "@ant-design/icons-vue";

@Component({
    components: {MenuOutlined, ToolOutlined}
})
export default class EditPrivilegePage extends Vue {

    activeKey = "";

    activeSubKey = "";

    roleId: number;

    submitted = false;

    /**
     * 权限树
     */
    privilegeTree: TreeItem[] = [];

    created(): void {
        this.roleId = Number(this.$route.query.roleId);
        // 初始化加载权限树
        SecurityController.getPrivilegeTreeByRoleId(this.axios, this.roleId).then(res => {
            this.privilegeTree = res.data;
        });
    }

    submitForm(): void {
        const role = new Role();
        role.roleId = this.roleId;
        const codes: Array<string> = [];
        this.privilegeTree.forEach(item => {
            this.buildPrivilegeCodes(item, codes);
        });
        role.privilegeCodes = codes;
        SecurityController.updateRolePrivileges(this.axios, role).then(res => {
            message.success('操作成功');
            this.$goBack();
        });
    }

    buildPrivilegeCodes(treeItem: TreeItem, codes: Array<string>): void {
        if (treeItem.checked) {
            codes.push(treeItem.key);
        }
        if (treeItem.children) {
            treeItem.children.forEach(item => {
                this.buildPrivilegeCodes(item, codes);
            });
        }
    }

    onCheckAllChange(treeItem: TreeItem) {
        const checked = treeItem.checked;
        this.toggleFor(treeItem, checked);
    }

    // 数据递归处理
    toggleFor(treeItem: TreeItem, checked: boolean) {
        treeItem.checked = checked;
        treeItem.unselectable = checked;
        if (treeItem.children) {
            treeItem.children.forEach(item => {
                this.toggleFor(item, checked);
            });
        }
    }
}
</script>

<style lang="less" scoped>
.scroll-block {
    max-height: calc(100vh - 280px);
    overflow: hidden auto;
}

.privilege-codes-tree {
    margin-top: 32px;
    border-radius: 4px;
    border: 1px solid rgba(0, 0, 0, 0.15);

    .tree-title {
        padding: 13px 20px;
        font-size: 14px;
        font-weight: 600;
        color: #000;
        background: #F5F7FA;
        border-bottom: 1px solid rgba(0, 0, 0, 0.15);
    }

    .tree-children > div {
        width: 100%;

        &:not(:last-child) {
            & > div {
                border-bottom: 1px solid rgba(0, 0, 0, 0.15);
            }
        }
    }

    .parent {
        padding: 12px 20px;
        min-width: 232px;
        border-right: 1px solid rgba(0, 0, 0, 0.15);
    }

    .child-col {
        padding: 12px 20px;
    }

    .child {
        flex: 1;
    }

    .child-col-item {
        padding-left: 16px !important;

        ::v-deep(.ant-checkbox+span) {
            color: rgba(0, 0, 0, 0.65);
        }
    }
}

::v-deep(.ant-collapse > .ant-collapse-item > .ant-collapse-content) {
    transition: none !important;
    animation: none !important;
}

//::v-deep(.ant-collapse-content-active) {
//    transition: none !important;
//    animation: none !important;
//}
</style>