在上文中,我们已经实现了数据的回显,并且已经做好了后端的接口。我们本次需要实现前端的权限分配的问题——当按下弹窗中的确认按钮,传递数据给后端接口即可。
针对于后端的接口是需要两个参数,一个是mid,一个是rid。需要想办法获取
对于rid,我们在showMenuDialog()函数中使用slot-scope获取当前列的id字段,作为data数据返回,再在我们使用接口函数中使用。对于mid,我们使用展开列的属性,getCheckedKeys(),getHalfCheckedKeys(),获取到半展开和全展开的id,再拼接起来转成字符串既可。
由于后端的接口用的路由接口的post请求,这里传递方式也要注意!!!
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()
}


<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>