实现权限列表
1. 权限列表
添加权限列表路由
创建权限管理组件(Rights.vue),并在router.js添加对应的路由规则
import Rights from '@/views/rightsManagement/Rights.vue'
......
path: '/home', component: Home, redirect: '/welcome', children: [
{ path: "/welcome", component: Welcome },
{ path: "/users", component: Users },
{ path: "/rights", component: Rights }
]
......
添加面包屑导航
在Rights.vue中添加面包屑组件展示导航路径
权限操作相关API
创建 api/role.js
import http from '../utils/http.js'
export function getRoles() {
return http.get('roles', null)
}
export function addRoles(params) {
return http.post('roles', params)
}
export function getRolesById(id) {
return http.get(`roles/${id}`, null)
}
export function updateRoles(id, params) {
return http.put(`roles/${id}`, params)
}
export function deleteRoles(id) {
return http.delete(`roles/${id}`, null)
}
创建 api/right.js
import http from '../utils/http.js'
export function getRights() {
return http.get('rights/list', null)
}
export function delRight(roleId, rightId) {
return http.delete(`roles/${roleId}/rights/${rightId}`, null)
}
export function getRightTree() {
return http.get('rights/tree', null)
}
export function setRight(roleId, rights) {
return http.post(`roles/${roleId}/rights`, rights)
}
显示数据
在data中添加一个rightsList数据,在methods中提供一个getRightsList方法发送请求获取权限列表数据,在mounted中调用这个方法获取数据
<template>
<div class="main">
<el-card>
<el-table :data="rightsList" border stripe class="table">
<el-table-column type="index"></el-table-column>
<el-table-column label="权限名称" prop="authName"></el-table-column>
<el-table-column label="路径" prop="path"></el-table-column>
<el-table-column label="权限等级" prop="label">
<template slot-scope="scope">
<el-tag v-if="scope.row.level === '0'">一级权限</el-tag>
<el-tag v-if="scope.row.level === '1'" type="success">二级权限</el-tag>
<el-tag v-if="scope.row.level === '2'" type="warning">三级权限</el-tag>
</template>
</el-table-column>
</el-table>
</el-card>
</div>
</template>
<script>
import {
getRights
} from '@/api/rights.js'
export default {
name: 'rightsList',
data() {
return {
rightsList: []
}
},
mounted() {
this.getRightsList()
},
methods: {
getRightsList() {
getRights().then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error(data.meta.mgs)
}
this.rightsList = data.data
}).catch(err => {
console.log(err)
})
}
}
}
</script>
<style lang="scss" scoped>
</style>
2.角色列表
添加角色列表路由
添加角色列表子组件(rightsManagement/Roles.vue),并添加对应的规则
{
path: '/home',
name: 'home',
component: Home,
children: [
{ path: '', redirect: 'welcome' },
{ path: 'welcome', component: Welcome },
{ path: 'users', component: Users },
{ path: 'rights', component: Rights },
{ path: 'roles', component: Roles }
]
}
添加面包屑导航
在Roles.vue中添加面包屑组件展示导航路径
显示数据
在data中添加一个roleList数据,在methods中提供一个getRoleList方法发送请求获取权限列表数据,在created)中调用这个方法获取数据
<template>
<div class="main">
<el-card>
<el-table row-key="id" :data="roleList" border :tree-props="{children: 'no'}">
<el-table-column type="expand"></el-table-column>
<el-table-column type="index"></el-table-column>
<el-table-column label="角色名称" prop="roleName"></el-table-column>
<el-table-column label="角色描述" prop="roleDesc"></el-table-column>
<el-table-column label="操作" width="300px">
<template>
<el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
<el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
<el-button size="mini" type="warning" icon="el-icon-setting">分配权限</el-button>
</template>
</el-table-column>
</el-table>
</el-card>
</div>
</template>
<script>
import {
getRoles
} from '../api/role.js'
export default {
data() {
return {
roleList: []
}
},
created() {
this.getRoleList()
},
methods: {
getRoleList() {
getRoles().then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error(data.meta.mgs)
}
this.roleList = data.data
}).catch(err => {
console.log(err)
})
}
}
}
</script>
<style lang="scss" scoped>
</style>
生成权限下拉列表
使用三重嵌套for循环生成权限下拉列表
<el-table-column type="expand">
<template v-slot="scope">
<el-row :class="['bdbottom','v-center',i1===0?'bdtop':'']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
<el-col :span="5">
<el-tag>
{{item1.authName}}
</el-tag>
<i class="el-icon-caret-right"></i>
</el-col>
<el-col :span="19">
<el-row :class="['v-center',i2===0?'':'bdtop' ]" v-for="(item2,i2) in item1.children" :key="item2.id">
<el-col :span="6">
<el-tag type="success">{{item2.authName}}</el-tag>
<i class="el-icon-caret-right"></i>
</el-col>
<el-col :span="18" class="col-height">
<el-tag type="warning" class="margin-left" v-for="(item3) in item2.children" :key="item3.id">
{{item3.authName}}
</el-tag>
</el-col>
</el-row>
</el-col>
</el-row>
</template>
</el-table-column>
美化样式
通过给一级权限el-row添加display:flex,align-items:center的方式解决一级权限垂直居中的问题,二级权限也类似添加,因为需要给多个内容添加,可以将这个样式设置为一个.vcenter{display:flex;align-items:center}
.bdtop {
border-top: 1px solid #eee;
}
.bdbottom {
border-bottom: 2px solid #eee;
}
.v-center {
display: flex;
align-items: center;
}
.col-height{
line-height: 40px;
}
.margin-left{
margin-left: 5px;
}
添加角色
<el-dialog :visible.sync="addDialogVisible" width="50%" title="添加角色" @close="addDialogClosed()">
<el-form :model="addForm" ref="addFormRef" label-width="70px">
<el-form-item label="角色名" prop="roleName">
<el-input v-model="addForm.roleName"></el-input>
</el-form-item>
<el-form-item label="角色描述" prop="roleDesc">
<el-input v-model="addForm.roleDesc"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="addDialogClosed()">取 消</el-button>
<el-button type="primary" @click="saveRole()">确 定</el-button>
</span>
</el-dialog>
<script>
import { getRoles, addRoles } from '../api/role.js'
export default {
data() {
return {
addForm: {
roleName: '',
roleDesc: ''
},
addDialogVisible: false,
addFormRules: {
rolename: [{
required: true,
message: '请输入角色名称',
trigger: 'blur'
}]
}
}
},
methods: {
addDialogClosed() {
this.$refs.addFormRef.resetFields()
this.addDialogVisible = false
},
saveRole() {
addRoles(this.addForm).then(res => {
const data = res.data
if (data.meta.status !== 201) {
this.$message.error(res.data.meta.msg)
} else {
this.$message.success(res.data.meta.msg)
}
this.addDialogVisible = false
this.getRoleList()
})
}
}
}
</script>
删除角色
<el-button size="mini" type="danger" icon="el-icon-delete" @click="removeRolesByID(scope.row.id)">删除</el-button>
import {
getRoles,
addRoles,
deleteRoles
} from '../api/role.js'
async removeRolesByID(id) {
const confirmResult = await this.$confirm('请问是否要永久删除该角色', '删除提示', {
confirmButtonText: '确认删除',
cancelButtonText: '取消',
type: 'warning'
}).catch(err => err)
if (confirmResult !== 'confirm') {
return this.$message.info('已经取消删除')
}
deleteRoles(id).then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error(data.meta.msg)
} else {
this.$message.success(data.meta.msg)
}
this.editDialogVisible = false
this.getRoleList()
})
}
编辑角色
获取角色信息
<el-dialog :visible.sync="editDialogVisible" width="50%" title="修改角色" @close="editDialogClosed()">
<el-form :model="editForm" ref="editFormRef" label-width="70px">
<el-form-item label="角色名" prop="roleName">
<el-input v-model="editForm.roleName"></el-input>
</el-form-item>
<el-form-item label="角色描述" prop="roleDesc">
<el-input v-model="editForm.roleDesc"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="editDialogClosed()">取 消</el-button>
<el-button type="primary" @click="editRoles()">确 定</el-button>
</span>
</el-dialog>
editDialogVisible: false,
editForm: {
roleName: '',
roleDesc: ''
},
editFormRules: {
rolename: [{
required: true,
message: '请输入角色名称',
trigger: 'blur'
}]
}
import {
getRoles,
addRoles,
deleteRoles,
getRolesById,
updateRoles
} from '../api/role.js'
showEditDialog(id) {
getRolesById(id).then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error('获取角色信息失败')
}
this.editForm = data.data
this.editDialogVisible = true
})
},
editDialogClosed() {
this.$refs.editFormRef.resetFields()
this.editDialogVisible = false
},
editRoles() {
updateRoles(this.editForm.roleId, this.editForm).then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error(data.meta.msg)
} else {
this.$message.success(data.meta.msg)
}
this.editDialogVisible = false
this.getRoleList()
})
添加权限删除功能
-
给每一个权限的el-tag添加closable属性,是的权限右侧出现“X”图标 -
再给el-tag添加绑定close事件处理函数 removeRightById(scope.row,item1.id) removeRightById(scope.row,item2.id) removeRightById(scope.row,item3.id)
import {
delRight
} from '../api/rights.js'
async removeRightById(role, id) {
const confirmResult = await this.$confirm('请问是否要删除该权限', '删除提示', {
confirmButtonText: '确认删除',
cancelButtonText: '取消',
type: 'warning'
}).catch(err => err)
if (confirmResult !== 'confirm') {
return this.$message.info('已经取消删除')
}
delRight(role.id, id).then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error(data.meta.msg)
} else {
this.$message.success(data.meta.msg)
role.children = data.data
}
})
}
完成权限分配功能
先给分配权限按钮添加事件 <el-button size=“mini” type=“warning” icon=“el-icon-setting” @click=“showSetRightDialog”>分配权限 在showSetRightDialog函数中请求权限树数据并显示对话框
import {
delRight,
getRightTree
} from '../api/rights.js'
showSetRightDialog() {
this.setRightDialogVisible = true
getRightTree().then(res => {
if (res.meta.status !== 200) {
return this.$message.error('获取权限树失败')
}
this.rightsList = res.data
})
}
添加分配权限对话框,并添加绑定数据setRightDialogVisible
<el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%">
<span>这是一段信息</span>
<span slot="footer" class="dialog-footer">
<el-button @click="setRightDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="setRightDialogVisible = false">确 定</el-button>
</span>
</el-dialog>
完成树形结构弹窗
在element.js中引入Tree,注册Tree
<el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%">
<el-tree :data="rightsList" :props="treeProps" show-checkbox node-key="id" default-expand-all
:default-checked-keys="defKeys" ref="treeRef"></el-tree>
<span slot="footer" class="dialog-footer">
<el-button @click="setRightDialogClose">取 消</el-button>
<el-button type="primary" @click="allotRights">确 定</el-button>
</span>
</el-dialog>
在data中增加如下数据:
rightsList: [],
treeProps: {
label: 'authName',
children: 'children'
},
defKeys: [],
roleId: ''
在methods中添加分配权限相关的方法
import {
delRight,
getRightTree,
setRight
} from '../api/rights.js'
showSetRightDialog(role) {
this.roleId = role.id
this.setRightDialogVisible = true
getRightTree().then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error('获取权限树失败')
}
this.rightsList = data.data
})
this.getLeafKeys(role, this.defKeys)
},
getLeafKeys(node, arr) {
if (!node.children) {
return arr.push(node.id)
}
node.children.forEach(item => this.getLeafKeys(item, arr))
},
setRightDialogClose() {
this.defKeys = []
this.setRightDialogVisible = false
},
allotRights() {
const keys = [
...this.$refs.treeRef.getCheckedKeys(),
...this.$refs.treeRef.getHalfCheckedKeys()
]
const idStr = keys.join(',')
setRight(this.roleId, { rids: idStr }).then(res => {
if (res.data.meta.status !== 200) {
return this.$message.error('分配权限失败')
}
this.$message.success('分配权限成功')
this.getRoleList()
this.setRightDialogVisible = false
})
}
分配角色
打开Users.vue,完成分配角色的功能 添加分配角色对话框
<el-dialog title="分配角色" :visible.sync="setRoleDialogVisible" width="50%">
<el-form :model="rolesList" label-width="100px">
<el-form-item label="当前用户名称">
<el-input v-model="userInfo.username" disabled></el-input>
</el-form-item>
<el-form-item label="当前角色名称">
<el-input v-model="userInfo.role_name" disabled></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="setRoleDialogClosed">取 消</el-button>
<el-button type="primary" @click="saveRoleInfo">确 定</el-button>
</span>
</el-dialog>
给分配角色按钮添加点击事件,点击之后弹出一个对话框进行角色分配
<el-tooltip class="item" effect="dark" content="分配角色" placement="top" :enterable="false">
<el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRole(scope.row)"></el-button>
</el-tooltip>
<script>
import {
getRoles
} from '@/api/role.js'
data(){
......
setRoleDialogVisible:false,
userInfo:{},
rolesList:[],
selectedRoleId:''
},
methods:{
......
showSetRole(userInfo) {
this.userInfo = userInfo
getRoles().then(res => {
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error(data.meta.msg)
}
this.setRoleDialogVisible = true
this.rolesList = data.data
})
}
}
}
</script>
在element.js中引入Select,Option,注册Select,Option
<el-form-item label="分配新角色">
<el-select v-model="selectedRoleId" placeholder="请选择角色">
<el-option v-for="item in rolesList" :key="item.id" :label="item.roleName" :value="item.id">
</el-option>
</el-select>
</el-form-item>
当用户点击对话框中的确定之后,完成分配角色的操作
<span slot="footer" class="dialog-footer">
<el-button @click="setRoleDialogClosed">取 消</el-button>
<el-button type="primary" @click="saveRoleInfo">确 定</el-button>
</span>
<script>
methods:{
.......
saveRoleInfo() {
setRoleByUser(this.userInfo.id, this.selectedRoleId).then(res => {
if (!this.selectedRoleId) {
return this.$message.error('请选择需要分配的角色')
}
const data = res.data
if (data.meta.status !== 200) {
return this.$message.error(data.meta.msg)
} else {
this.$message.success(data.meta.msg)
this.setRoleDialogVisible = false
this.getUserList()
}
})
},
setRoleDialogClosed() {
this.setRoleDialogVisible = false
this.userInfo = ''
}
}
</script>
|