由于公司的项目使用的是el-admin项目的开源框架,最近在学习el-template-admin项目时,发现两者的路由权限有很大的不同,总结一下,方便日后使用:
1、el-admin开源项目使用的是动态路由,权限和动态路由都是在后端配置的,前端没有使用控制权限,缺点就是,前端人员要想画页面,就要通知后端人员配置路由和权限,没有做到完全的进行前后端分离。
2、el-element-admin是前端配置的路由,可以在前端配置路由权限,给路由分配权限。
1、首先在router/router.js中进行路由的配置,注意这里手动配置不需要权限的路由,那些需要权限的路由是通过请求后端接口实现的。附上源码:
1、api/system/menu.js 前端请求后端的接口
import request from '@/utils/request'
export function buildMenus() {
return request({
url: 'yj/iscUserLocext/menus',
method: 'get'
})
}
2、router/index.js
.......
import { buildMenus } from '@/api/system/menu' // 请求路由的api
import { filterAsyncRouter } from '@/store/modules/permission' // 遍历后台传来的路由字符串,转换为组件对象
.......
const whiteList = ['/login']// no redirect whitelist
router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = to.meta.title + ' - ' + Config.title
}
NProgress.start()
if (getToken()) {
// 已登录且要跳转的页面是登录页
if (to.path === '/login') {
next({ path: '/' })
NProgress.done()
} else {
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
store.dispatch('GetInfo').then(res => { // 拉取user_info
// 动态路由,拉取菜单
loadMenus(next, to)
}).catch((err) => {
console.log(err)
store.dispatch('LogOut').then(() => {
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
// 登录时未拉取 菜单,在此处拉取
} else if (store.getters.loadMenus) {
// 修改成false,防止死循环
store.dispatch('updateLoadMenus').then(res => {})
loadMenus(next, to)
} else {
next()
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
next()
} else {
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
NProgress.done()
}
}
})
// 动态获取路由
export const loadMenus = (next, to) => {
buildMenus().then(res => {
const asyncRouter = filterAsyncRouter(res)
asyncRouter.push({ path: '*', redirect: '/404', hidden: true })
store.dispatch('GenerateRoutes', asyncRouter).then(() => { // 存储路由
router.addRoutes(asyncRouter) // 动态添加可访问路由表
next({ ...to, replace: true })
})
})
}
router.afterEach(() => {
NProgress.done() // finish progress bar(完成进度条)
})
3、store/module/permission.js
import { constantRouterMap } from '@/router/routers'
import Layout from '@/layout/index'
const permission = {
state: {
routers: constantRouterMap, // 默认是静态路由,这部分路由是公共的,从数据库中查询的路由会添加到其后面
addRouters: []
},
mutations: {
SET_ROUTERS: (state, routers) => {
state.addRouters = routers
state.routers = constantRouterMap.concat(routers)
}
},
actions: {
GenerateRoutes({ commit }, asyncRouter) {
commit('SET_ROUTERS', asyncRouter)
}
}
}
export const filterAsyncRouter = (routers) => { // 遍历后台传来的路由字符串,转换为组件对象
return routers.filter(router => {
if (router.component) {
if (router.component === 'Layout') { // Layout组件特殊处理
router.component = Layout
} else {
const component = router.component
router.component = loadView(component)
}
}
if (router.children && router.children.length) {
router.children = filterAsyncRouter(router.children)
}
return true
})
}
export const loadView = (view) => {
return (resolve) => require([`@/views/${view}`], resolve)
}
export default permission
4、store/getters.js
const getters = {
.......
loadMenus: state => state.user.loadMenus,
permission_routers: state => state.permission.routers,
addRouters: state => state.permission.addRouters,
.......
}
export default getters
5、Sidebar/index.vue
<template>
<div :class="{'has-logo':showLogo}">
<el-scrollbar wrap-class="scrollbar-wrapper">
<el-menu
.....
>
<sidebar-item v-for="route in permission_routers" :key="route.path" :item="route" :base-path="route.path" />
</el-menu>
</el-scrollbar>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
.....
computed: {
...mapGetters([
'permission_routers',
'sidebar'
]),
......
}
}
</script>
el-admin项目的动态路由大概就是这么实现的。
1、el-element-admin项目的路由和权限是在前端配置的,最大的区别是permission.js不同:
import { asyncRoutes, constantRoutes } from '@/router'
/**
* Use meta.role to determine if the current user has permission(使用元。角色确定当前用户是否具有权限)
* @param roles
* @param route
*/
function hasPermission(roles, route) {
if (route.meta && route.meta.roles) {
return roles.some(role => route.meta.roles.includes(role))
} else {
return true
}
}
/**
* Filter asynchronous routing tables by recursion(通过递归过滤异步路由表)
* @param routes asyncRoutes
* @param roles
*/
export function filterAsyncRoutes(routes, roles) {
const res = []
routes.forEach(route => {
const tmp = { ...route }
if (hasPermission(roles, tmp)) {
if (tmp.children) {
tmp.children = filterAsyncRoutes(tmp.children, roles)
}
res.push(tmp)
}
})
return res
}
const state = {
routes: [], // 默认为空
addRoutes: []
}
const mutations = {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
}
}
const actions = {
generateRoutes({ commit }, roles) {
return new Promise(resolve => {
let accessedRoutes
if (roles.includes('admin')) {
accessedRoutes = asyncRoutes || []
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
2、使用mock模拟数据: mock/index.js, 分配权限
const Mock = require('mockjs')
const { deepClone } = require('../utils')
const { asyncRoutes, constantRoutes } = require('./routes.js')
const routes = deepClone([...constantRoutes, ...asyncRoutes])
const roles = [
{
key: 'admin',
name: 'admin',
description: '超级管理员。可以查看所有页面',
routes: routes
},
{
key: 'editor',
name: 'editor',
description: '正常的编辑器。可以看到所有页面除了权限页',
routes: routes.filter(i => i.path !== '/permission')// just a mock
},
{
key: 'visitor',
name: 'visitor',
description: '只是一个游客,只能看到主页和文档页',
routes: [{
path: '',
redirect: 'dashboard',
children: [
{
path: 'dashboard',
name: 'Dashboard',
meta: { title: 'dashboard', icon: 'dashboard' }
}
]
}]
}
]
module.exports = [
// mock get all routes form server
{
url: '/vue-element-admin/routes',
type: 'get',
response: _ => {
return {
code: 20000,
data: routes
}
}
},
// mock get all roles form server
{
url: '/vue-element-admin/roles',
type: 'get',
response: _ => {
return {
code: 20000,
data: roles
}
}
},
// add role
{
url: '/vue-element-admin/role',
type: 'post',
response: {
code: 20000,
data: {
key: Mock.mock('@integer(300, 5000)')
}
}
},
// update role
{
url: '/vue-element-admin/role/[A-Za-z0-9]',
type: 'put',
response: {
code: 20000,
data: {
status: 'success'
}
}
},
// delete role
{
url: '/vue-element-admin/role/[A-Za-z0-9]',
type: 'delete',
response: {
code: 20000,
data: {
status: 'success'
}
}
}
]
3、mock/role/routes.js, 在该文件中,所有的路由都已经配置好了。
// Just a mock data
const constantRoutes = [
{
path: '/redirect',
component: 'layout/Layout',
hidden: true,
children: [
{
path: '/redirect/:path*',
component: 'views/redirect/index'
}
]
},
{
path: '/login',
component: 'views/login/index',
hidden: true
},
{
path: '/auth-redirect',
component: 'views/login/auth-redirect',
hidden: true
},
{
path: '/404',
component: 'views/error-page/404',
hidden: true
},
{
path: '/401',
component: 'views/error-page/401',
hidden: true
},
{
path: '',
component: 'layout/Layout',
redirect: 'dashboard',
children: [
{
path: 'dashboard',
component: 'views/dashboard/index',
name: 'Dashboard',
meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
}
]
},
]
const asyncRoutes = [
{
path: '/permission',
component: 'layout/Layout',
redirect: '/permission/index',
alwaysShow: true,
meta: {
title: 'Permission',
icon: 'lock',
roles: ['admin', 'editor']
},
children: [
{
path: 'page',
component: 'views/permission/page',
name: 'PagePermission',
meta: {
title: 'Page Permission',
roles: ['admin']
}
},
{
path: 'directive',
component: 'views/permission/directive',
name: 'DirectivePermission',
meta: {
title: 'Directive Permission'
}
},
{
path: 'role',
component: 'views/permission/role',
name: 'RolePermission',
meta: {
title: 'Role Permission',
roles: ['admin']
}
}
]
},
},
.......
{
path: 'external-link',
component: 'layout/Layout',
children: [
{
path: 'https://github.com/PanJiaChen/vue-element-admin',
meta: { title: 'External Link', icon: 'link' }
}
]
},
{ path: '*', redirect: '/404', hidden: true }
]
module.exports = {
constantRoutes,
asyncRoutes
}