[web教学] (vue权限管理)前端路由表脚色权限管理,通过登录不同脚色侧边栏体现对应

[复制链接]
查看856 | 回复0 | 2023-8-23 11:41:48 | 显示全部楼层 |阅读模式 来自 中国北京
前端路由表脚色权限管理,通过登录不同脚色侧边栏体现对应页面

demo根据vue-admin-template为基础修改,起首展示实现的效果


1. 起首在src/router/index.js中添加路由表,其中constantRoutes 设置的为全部脚色可见的路由,asyncRouterMap为对应权限人员可见路由,demo路由表代码如下:

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. Vue.use(Router)
  4. //避免导航守卫报错
  5. const originalPush = Router.prototype.push
  6. Router.prototype.push = function push(location, onResolve, onReject) {
  7.   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  8.   return originalPush.call(this, location).catch(err => err)
  9. }
  10. /* Layout */
  11. import Layout from '@/layout'
  12. //所有人可见
  13. export const constantRoutes = [
  14.   {
  15.     path: '/login',
  16.     component: () => import('@/views/login/index'),
  17.     hidden: true
  18.   },
  19.   {
  20.     path: '/404',
  21.     component: () => import('@/views/404'),
  22.     hidden: true
  23.   },
  24.   {
  25.     path: '/',
  26.     component: Layout,
  27.     redirect: '/dashboard',
  28.     children: [
  29.       {
  30.         path: 'dashboard',
  31.         name: 'Dashboard',
  32.         component: () => import('@/views/dashboard/index'),
  33.         meta: {
  34.           title: '首页',
  35.           icon: 'dashboard'
  36.         }
  37.       }
  38.     ]
  39.   },
  40.   {
  41.     path: '/example',
  42.     component: Layout,
  43.     children: [
  44.       {
  45.         path: 'index',
  46.         name: 'Table',
  47.         component: () => import('@/views/table/index'),
  48.         meta: {
  49.           title: '所有人可见',
  50.           icon: 'table'
  51.         }
  52.       }
  53.     ]
  54.   },
  55.   // 404 page must be placed at the end !!!
  56.   { path: '*', redirect: '/404', hidden: true }
  57. ]
  58. //相应权限人员可见
  59. export const asyncRouterMap = [
  60.   {
  61.     path: '/form',
  62.     component: Layout,
  63.     children: [
  64.       {
  65.         path: 'index',
  66.         name: 'Form',
  67.         component: () => import('@/views/form/index'),
  68.         meta: {
  69.           title: '所有人可见',
  70.           icon: 'form',
  71.           role: ['admin']
  72.         }
  73.       }
  74.     ]
  75.   },
  76.   {
  77.     path: '/system',
  78.     component: Layout,
  79.     redirect: 'system/test',
  80.     name: 'System',
  81.     alwaysShow: true,
  82.     meta:{title:'系统管理', icon: 'nested', role: ['admin','editor']},
  83.     children: [
  84.       {
  85.         path: '权限管理',
  86.         name: 'test',
  87.         name: 'Test',
  88.         component: () => import('@/views/system/index'),
  89.         meta: {
  90.           title: '权限修改',
  91.           icon: 'table',
  92.           role: ['admin']
  93.         }
  94.       }
  95.     ]
  96.   }
  97. ]
  98. const createRouter = () =>
  99.   new Router({
  100.     // mode: 'history', // require service support
  101.     scrollBehavior: () => ({ y: 0 }),
  102.     routes: constantRoutes
  103.   })
  104. const router = createRouter()
  105. // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
  106. export function resetRouter() {
  107.   const newRouter = createRouter()
  108.   router.matcher = newRouter.matcher // reset router
  109. }
  110. export default router
复制代码
2.在src/api/user.js中创建用户登录,获取用户信息,以及登出的接口


3.在store/modules/user.js文件,添加获取脚色权限role的信息



4.在src/store/modules/目次下创建permission.js,来存储不同权限动态添加的路由表,文件代码如下:

  1. import { asyncRouterMap, constantRoutes } from '@/router'
  2. /**
  3. * Use meta.role to determine if the current user has permission
  4. * @param role
  5. * @param route
  6. */
  7. function hasPermission(role, route) {
  8.     if (route.meta && route.meta.role) {
  9.         // return roleArr.some(role => route.meta.role.includes(role))  //当给的角色权限为数组形式可采取该方式判断返回值
  10.         return route.meta.role.includes(role)?true:false  //当角色权限为字符串时,采用该方式判断
  11.     } else {
  12.         return true
  13.     }
  14. }
  15. /**
  16. * 将符合相应权限的路由表筛选出来
  17. * @param routes asyncRouterMap
  18. * @param role
  19. */
  20. export function filterasyncRouterMap(routes, role) {
  21.     const res = []
  22.     routes.forEach(route => {
  23.         const tmp = { ...route }
  24.         if (hasPermission(role, tmp)) {
  25.             console.log(111);
  26.             if (tmp.children) {
  27.                 tmp.children = filterasyncRouterMap(tmp.children, role)
  28.             }
  29.             res.push(tmp)
  30.         }
  31.     })
  32.     return res
  33. }
  34. const permission = {
  35.     state: {
  36.         routes: [],
  37.         addRoutes: []
  38.     },
  39.     mutations: {
  40.         SET_ROUTES: (state, routes) => {
  41.             state.addRoutes = routes
  42.             state.routes = constantRoutes.concat(routes)
  43.         }
  44.     },
  45.     actions: {
  46.         generateRoutes({ commit }, role) {
  47.             return new Promise(resolve => {
  48.                 let accessedRoutes
  49.                 //如果角色是admin
  50.                 if (role.includes('admin')) {
  51.                 //将route.js中的admin权限人员可见的路由表加入,此处我们只有admin和editor两个角色
  52.                     accessedRoutes = asyncRouterMap || []
  53.                 } else {
  54.                     accessedRoutes = filterasyncRouterMap(asyncRouterMap, role) || []
  55.                 }
  56.                 commit('SET_ROUTES', accessedRoutes)
  57.                 resolve(accessedRoutes)
  58.             })
  59.         }
  60.     }
  61. }
  62. export default permission
复制代码
5.在src/store/getters.js中,代码如下(注意:state.permission.routes别写成了state.user.routes):


6.在src/store/index.js中,代码如下


7.最后在src/permission.js的路由导航守卫中添加动态路由,此处用到了vue-router的addRoute函数,修改处代码如下:


8.在src/layout/components/Sidebar/index中,添加新的路由表,代码如下:


终极可以实现文章首部动图效果,简单的纪录下前端路由表权限管理功能实现,若有禁绝确处,批评处可交换讨论,文末会贴源码,安装依赖后可直接运行。
文末demo码云链接:权限管理demo

来源:https://blog.csdn.net/qq_36660135/article/details/129064461
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则