• 81.(前端)分配权限实现——用路由来接收参数实现展开行中选数据功能


    1.概述

    在上文中,我们已经实现了数据的回显,并且已经做好了后端的接口。我们本次需要实现前端的权限分配的问题——当按下弹窗中的确认按钮,传递数据给后端接口即可。

    2.流程概述

    针对于后端的接口是需要两个参数,一个是mid,一个是rid。需要想办法获取
    对于rid,我们在showMenuDialog()函数中使用slot-scope获取当前列的id字段,作为data数据返回,再在我们使用接口函数中使用。对于mid,我们使用展开列的属性,getCheckedKeys(),getHalfCheckedKeys(),获取到半展开和全展开的id,再拼接起来转成字符串既可。
    由于后端的接口用的路由接口的post请求,这里传递方式也要注意!!!

    3.主要代码展示

    async editMenu(){
                // console.log(this.rid)
                const mids = [
                    ...this.$refs.treeRef.getHalfCheckedKeys(),
                    ...this.$refs.treeRef.getCheckedKeys()
                ]
                const midsStr = mids.join(',')
                const { data: resp } = await this.$axios.post(
                    `/api/set_menu/${this.rid}`,this.$qs.stringify({mids: midsStr})
                )
                if (resp.status !== 200) return this.$msg.error(resp.msg)
                this.$msg.success(resp.msg)
                this.getRoleList()
                this.dialogClose()
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.效果展示

    在这里插入图片描述
    在这里插入图片描述

    5.完整代码展示

    
    <template>
        <div>
            
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/home' }">首页el-breadcrumb-item>
                <el-breadcrumb-item>权限管理el-breadcrumb-item>
                <el-breadcrumb-item>角色列表el-breadcrumb-item>
            el-breadcrumb>
            <el-card>
                
                <el-row :gutter="20">
                    <el-col :span="2">
                        <el-button type="primary" icon="el-icon-circle-plus-outline">新增角色el-button>
                    el-col>
                el-row>
                
                <el-row>
                    <el-col>
                        <el-table :data="roleList" border style="width:100%">
                            
                            <el-table-column type="expand">
                                <template slot-scope="scope">
                                    
                                    <el-row :class="['bottom', i==0?'top':'', 'rcenter']" v-for=" (m,i) in scope.row.menu" :key="m.id">
                                        <el-col :span="8">
                                            
                                            <el-tag closable @click="removeMenu(scope.row,m.id)">{{m.name}}el-tag>
                                            <span class="el-icon-caret-right">span>
                                        el-col>
                                        <el-col :span="14">
                                            <el-tag closable @click="removeMenu(scope.row,sm.id)" type="success" v-for="sm in m.children" :key="sm.id">
                                                {{sm.name}}
                                            el-tag>
                                        el-col>
                                    el-row>
                                template>
                            el-table-column>
                            <el-table-column type="index">el-table-column>
                            <el-table-column prop="id" label="ID">el-table-column>
                            <el-table-column prop="name" label="角色名称">el-table-column>
                            <el-table-column prop="desc" label="角色详情">el-table-column>
                            <el-table-column prop="level" label="操作" width="300px">
                                <template slot-scope="scope">
                                    <el-button size="mini" type="success" icon="el-icon-edit">修改el-button>
                                    <el-button size="mini" type="danger" icon="el-icon-edit">删除el-button>
                                    <el-button size="mini" type="warning" icon="el-icon-edit" @click="showMenuDialog(scope.row)">分配权限el-button>
                                template>
                            el-table-column>
                        el-table>
                    el-col>
                el-row>
            el-card>
            <el-dialog title="分配权限" :visible.sync="menuDialogVisible" width="40%" :before-close="dialogClose">
                
                
                <el-tree 
                :default-checked-keys="keyList"  
                node-key = "id"
                default-expand-all
                :data="menuList" 
                :props="menuProps" 
                ref="treeRef"
                show-checkbox>el-tree>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogClose()">取 消el-button>
                    <el-button type="primary" @click="editMenu()">确 定el-button>
                span>
            el-dialog>
        div>
    template>
    
    <script>
    
    export default {
        data () {
            return {
                roleList: [],
                menuDialogVisible:false,
                menuProps:{
                    label: 'name',
                    children: 'children'
                },
                menuList:[],
                keyList:[],
                rid: 0
            }
        },
        created(){
            this.getRoleList()
            // this.getMenuList()
        },
        methods: {
            async getRoleList() {
                const { data: res } = await this.$axios.get('/api/role')
                if (res.status !== 200) return this.$msg.error(res.msg)
                console.log(res.data);
                this.roleList = res.data
            },
            removeMenu(row,mid) {
                this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const { data: resp } = await this.$axios.get(`/api/del_menu/${row.id}/${mid}`)
                    if (resp.status !== 200) return this.$msg(resp.msg)
                    this.$msg({
                        type: 'success',
                        message: '删除成功!'
                    });
                    // this.getRoleList()
                    // console.log(row.id);
                    // console.log(mid);
                    row.menu = resp.data
                }).catch(() => {
                    this.$msg({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            showMenuDialog(row){
                this.rid = row.id
                this.getMenuList()
                this.menuDialogVisible = true
                // 显示菜单时调用,用于数据回显
                this.getKeys(row.menu)
            },
            async getMenuList(){
                const{data :resp} = await this.$axios.get('/api/menu')
                if (resp.status !== 200) return this.$msg.error(resp.msg)
                this.menuList = resp.data
            },
            // 获取menu中的元素
            getKeys(menu) {
                menu.forEach(item => {
                    item.children.forEach(i => {    
                        this.keyList.push(i.id)
                    })
                })
            },
            dialogClose(){
                this.keyList = []
                this.menuDialogVisible = false
            },
            async editMenu(){
                // console.log(this.rid)
                const mids = [
                    ...this.$refs.treeRef.getHalfCheckedKeys(),
                    ...this.$refs.treeRef.getCheckedKeys()
                ]
                const midsStr = mids.join(',')
                const { data: resp } = await this.$axios.post(
                    `/api/set_menu/${this.rid}`,this.$qs.stringify({mids: midsStr})
                )
                if (resp.status !== 200) return this.$msg.error(resp.msg)
                this.$msg.success(resp.msg)
                this.getRoleList()
                this.dialogClose()
            }
        }
       
    }
    
    script>
    
    <style lang="less" scoped>
    
        .top{
            border-top: 1px solid #eee
        }
        .bottom{
            border-top: 1px solid #eee
        }
        .el-tag{
            margin: 10px;
        }
        .rcenter{
            display:flex;
            align-items: center;
        }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
  • 相关阅读:
    vue 图片转base64格式的方法
    SQL查询优化---单表使用索引及常见索引失效优化
    底层概念的重要意义
    数学建模与MatheMatica
    关于netty的一些你需要知道的内容(4)
    【知识网络分析】引文网络(citation)
    [uni-app] uni.showToast 一闪而过问题/设定时间无效/1秒即逝
    Spring Cloud 实战 | 解密负载均衡Ribbon底层原理,包含实战源码
    相机光学(三十七)——自动对焦原理
    YOLO目标检测——红白细胞血小板数据集【含对应voc、coco和yolo三种格式标签】
  • 原文地址:https://blog.csdn.net/m0_63953077/article/details/127550643