PrivilegePage.vue 10.4 KB
<template>
    <a-card title="权限管理">
        <a-button type="default" @click="createModule()">
            <template #icon>
                <font-awesome-icon :icon="['far', 'plus-square']" class="mr-5"></font-awesome-icon>
            </template>
            新建模块
        </a-button>
        <a-button type="default" @click="savePrivileges()">
            <template #icon>
                <font-awesome-icon :icon="['far', 'plus-square']" class="mr-5"></font-awesome-icon>
            </template>
            保存
        </a-button>
        <a-button type="default" @click="download()">
            <template #icon>
                <font-awesome-icon :icon="['far', 'plus-square']" class="mr-5"></font-awesome-icon>
            </template>
            导出
        </a-button>
        <a-table :columns="columns" :data-source="privileges" :row-key="record => record.code" bordered size="small" :pagination="false" style="padding-top: 8px">
            <template #bodyCell="{column,record}">
                <template v-if="column.key === 'type'">
                    <span v-if="record.type==='MODULE'">模块</span>
                    <span v-else-if="record.type==='MENU'">菜单</span>
                    <span v-else-if="record.type==='BUTTON'">按钮</span>
                    <span v-else-if="record.type==='METHOD'">方法</span>
                </template>
                <template v-if="column.key === 'enabled'">
                    <a-switch v-model:checked="record.enabled" checked-children="已启用" un-checked-children="已停用"/>
                </template>
                <template v-if="column.key === 'action'">
                    <a @click="createPrivilege(record)">
                        <a-tooltip title="创建下级">
                            <plus-circle-outlined/>
                        </a-tooltip>
                    </a>
                    <a-divider type="vertical"/>
                    <a @click="editPrivilege(record)">
                        <a-tooltip title="编辑">
                            <EditOutlined/>
                        </a-tooltip>
                    </a>
                    <a-divider type="vertical"/>
                    <a-popconfirm placement="topRight" title="确认删除吗?" ok-text="确认" cancel-text="取消"
                                  @confirm="removePrivilege(record)">
                        <a href="#" class="text-danger">
                            <a-tooltip title="删除">
                                <DeleteOutlined/>
                            </a-tooltip>
                        </a>
                    </a-popconfirm>
                    <a-divider type="vertical"/>
                    <a @click="upward(record)">
                        <a-tooltip title="上移">
                            <arrow-up-outlined/>
                        </a-tooltip>
                    </a>
                    <a-divider type="vertical"/>
                    <a @click="downward(record)">
                        <a-tooltip title="下移">
                            <arrow-down-outlined/>
                        </a-tooltip>
                    </a>
                </template>
            </template>
        </a-table>
    </a-card>
</template>

<script lang="ts">
import {message, TableColumnsType} from 'ant-design-vue';
import {
    ArrowDownOutlined,
    CheckOutlined,
    DeleteOutlined,
    EditOutlined,
    FormOutlined,
    HomeOutlined,
    PlusCircleOutlined,
    TableOutlined,
} from '@ant-design/icons-vue';
import {ArrowUpOutlined} from '@ant-design/icons-vue/lib';
import EditPrivilege from './EditPrivilege.vue';
import CreatePrivilege from './CreatePrivilege.vue';
import {Component, Vue} from "vue-facing-decorator";
import {Privilege} from "lib/src/core/security/Privilege";
import {ModalService} from "@/components/ModalService";

@Component({
    name: "MPrivilegePage",
    components: {
        HomeOutlined, FormOutlined, ArrowUpOutlined, DeleteOutlined, TableOutlined, ArrowDownOutlined, PlusCircleOutlined, CheckOutlined, EditOutlined,
    }
})
export default class PrivilegePage extends Vue {

    columns: TableColumnsType = [
        {title: '名称', dataIndex: 'name', sorter: false, key: 'name', minWidth: 200},
        {title: '编码', dataIndex: 'code', sorter: false, key: 'code', width: 160},
        {title: '类型', dataIndex: 'type', sorter: false, key: 'type', width: 80, align: 'center'},
        {title: '图标', dataIndex: 'icon', sorter: false, key: 'icon', width: 120},
        {title: '节点样式', dataIndex: 'css', sorter: false, key: 'css', width: 120},
        {title: '子节点样式', dataIndex: 'cssChild', sorter: false, key: 'cssChild', width: 120},
        {title: '权限值', dataIndex: 'value', sorter: false, key: 'value', minWidth: 200},
        {title: '状态', dataIndex: 'enabled', sorter: false, key: 'enabled', width: 100, align: 'center'},
        {title: '操作', key: 'action', width: 155, align: 'center', fixed: 'right'},
    ];

    privileges: Privilege[] = [];

    selectedPrivilege: Privilege | null = null;
    /**
     * 新建弹窗
     */
    createVisible = false;

    /**
     * 修改弹窗
     */
    editVisible = false;

    /**
     * 详情弹窗
     */
    viewVisible = false;

    showCreate(): void {
        this.createVisible = true;
    }

    created(): void {
        // @ts-ignore
        this.axios.get('security/getAllPrivileges').then(response => {
            this.privileges = response.data;
        });
    }

    createModule(): void {
        this.selectedPrivilege = null;
        this.createVisible = true;
    }

    createPrivilege(privilege: Privilege): void {
        this.selectedPrivilege = privilege;
        const modal = ModalService.confirm({
            title: '新建权限资源',
            closable: true,
            appContext: this.$.appContext,
            component: CreatePrivilege,
            props: {
                privilege: this.selectedPrivilege,
                privileges: this.privileges
            },
            onOk: (component): void => {
                component.submitForm();
            },
            emits: {
                handleOk: (e) => {
                    modal.destroy();
                    if (e) {
                        this.handleCreated(e);
                    }
                }
            },
        });
    }

    editPrivilege(privilege: Privilege): void {
        this.selectedPrivilege = privilege;
        const modal = ModalService.confirm({
            title: '编辑权限资源',
            closable: true,
            appContext: this.$.appContext,
            component: EditPrivilege,
            props: {
                privilege: this.selectedPrivilege,
                privileges: this.privileges
            },
            onOk: (component): void => {
                component.submitForm();
            },
            emits: {
                handleOk: (e) => {
                    modal.destroy();
                    if (e) {
                        this.handleEdited(e);
                    }
                }
            },
        });
    }

    handleCreated(privilege: Privilege): void {
        if (this.selectedPrivilege) {
            if (!this.selectedPrivilege.children) {
                // @ts-ignore
                this.selectedPrivilege.children = [];
            }
            this.selectedPrivilege?.children.push(privilege);
        } else {
            this.privileges.push(privilege);
        }
    }

    handleEdited(privilege: Privilege): void {
        if (this.selectedPrivilege) {
            this.selectedPrivilege.code = privilege.code;
            this.selectedPrivilege.name = privilege.name;
            this.selectedPrivilege.type = privilege.type;
            this.selectedPrivilege.value = privilege.value;
            this.selectedPrivilege.icon = privilege.icon;
            this.selectedPrivilege.css = privilege.css;
            this.selectedPrivilege.cssChild = privilege.cssChild;
        }
    }

    /**
     * 上移
     * @param privilege
     */
    upward(privilege: Privilege): void {
        const plgs = this.findBrotherPrivileges(privilege.code, this.privileges);
        if (plgs) {
            const index = plgs.indexOf(privilege);
            if (index !== 0) {
                plgs.splice(index - 1, 1, ...plgs.splice(index, 1, plgs[index - 1]))
            }
        }
    }

    /**
     * 下移
     * @param privilege
     */
    downward(privilege: Privilege): void {
        const plgs = this.findBrotherPrivileges(privilege.code, this.privileges);
        if (plgs) {
            const index = plgs.indexOf(privilege);
            if (index < (plgs.length - 1)) {
                if (plgs) {
                    plgs.splice(index, 1, ...plgs.splice(index + 1, 1, plgs[index]));
                }
            }
        }
    }

    removePrivilege(privilege: Privilege): void {
        const plgs = this.findBrotherPrivileges(privilege.code, this.privileges);
        if (plgs) {
            const index = plgs.indexOf(privilege);
            plgs.splice(index, 1);
        }
    }

    savePrivileges(): void {
        // @ts-ignore
        this.axios.post('security/updatePrivileges', this.privileges).then(response => {
            if (response.data) {
                message.error('权限编码' + response.data + '重复,保存失败');
            } else {
                message.success('保存成功');
            }
        });
    }

    findBrotherPrivileges(code: string, privileges: Privilege[]): Privilege[] | null {
        for (const privilege of privileges) {
            if (privilege.code === code) {
                return privileges;
            } else if (privilege.children) {
                const plgs = this.findBrotherPrivileges(code, privilege.children);
                if (plgs) {
                    return plgs;
                }
            }
        }
        return null;
    }

    download(): void {
        // @ts-ignore
        this.axios.get('security/downloadPrivileges', {//静态资源文件夹public
            responseType: 'blob', //重要
        }).then(response => {
            const url = window.URL.createObjectURL(new Blob([response.data]));
            const link = document.createElement('a');
            let fname = 'privileges.json';
            link.href = url;
            link.setAttribute('download', fname);
            document.body.appendChild(link);
            link.click();
        }).catch(error => {
            console.log('error:' + JSON.stringify(error))
        });
    }
}
</script>

<style scoped>

</style>