permission.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css'// progress bar style
  6. import { getToken } from '@/utils/auth' // getToken from cookie
  7. NProgress.configure({ showSpinner: false })// NProgress Configuration
  8. // permissiom judge function
  9. function hasPermission(roles, permissionRoles) {
  10. if (roles.indexOf('admin') >= 0) return true // admin permission passed directly
  11. if (!permissionRoles) return true
  12. return roles.some(role => permissionRoles.indexOf(role) >= 0)
  13. }
  14. const whiteList = ['/login', '/authredirect']// no redirect whitelist
  15. //自定义路由
  16. const myRoles = [
  17. 'goodsManage',
  18. 'goodsExchangeRules',
  19. 'goodsList',
  20. 'giftManage',
  21. 'giftExchangeRules',
  22. 'giftList',
  23. 'welfareManage',
  24. 'welfareList',
  25. 'exchangeManage',
  26. 'exchangeList',
  27. 'approvalList',
  28. 'pasList',
  29. 'voidList',
  30. 'pointManage',
  31. 'pointInstructions',
  32. 'pointRulesList',
  33. 'pointList',
  34. 'pointsDetailList',
  35. 'activityManage',
  36. 'activityList',
  37. 'noticeManage',
  38. 'noticeList',
  39. 'ranking',
  40. 'rankingList',
  41. // 'medalManage',
  42. // 'medalList',
  43. 'commendManage',
  44. 'commendList',
  45. 'festivalManage',
  46. 'festivalList',
  47. 'certManage',
  48. 'certRules',
  49. 'certList',
  50. 'dictManage',
  51. 'dictList',
  52. 'dictDataList',
  53. ]
  54. router.beforeEach((to, from, next) => {
  55. NProgress.start() // start progress bar
  56. if (getToken()) { // determine if there has token
  57. /* has token*/
  58. if (to.path === '/login') {
  59. next({ path: '/' })
  60. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  61. } else {
  62. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  63. store.dispatch('GetUserInfo').then(res => {
  64. store.dispatch('GetUserMenus').then(res => { // 拉取user_info
  65. const roles = res.data.data // note: roles must be a array! such as: ['editor','develop']
  66. // const roles = myRoles;
  67. store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
  68. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  69. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  70. })
  71. })
  72. }).catch(() => {
  73. store.dispatch('FedLogOut').then(() => {
  74. Message.error('Verification failed, please login again')
  75. next({ path: '/login' })
  76. })
  77. })
  78. } else {
  79. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  80. if (hasPermission(store.getters.roles, to.meta.roles)) {
  81. next()//
  82. } else {
  83. next({ path: '/401', replace: true, query: { noGoBack: true }})
  84. }
  85. // 可删 ↑
  86. }
  87. }
  88. } else {
  89. /* has no token*/
  90. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  91. next()
  92. } else {
  93. next('/login') // 否则全部重定向到登录页
  94. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  95. }
  96. }
  97. })
  98. router.afterEach(() => {
  99. NProgress.done() // finish progress bar
  100. })