permission.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // 'pointManage',
  18. // 'pointInstructions',
  19. // 'pointRulesList',
  20. // 'pointList',
  21. 'goodsManage',
  22. 'goodsExchangeRules',
  23. 'goodsList',
  24. 'giftManage',
  25. 'giftExchangeRules',
  26. 'giftList',
  27. 'welfareManage',
  28. 'welfareList',
  29. 'exchangeManage',
  30. 'exchangeList',
  31. // 'activityManage',
  32. // 'activityList',
  33. // 'noticeManage',
  34. // 'noticeList',
  35. // 'ranking',
  36. // 'rankingList',
  37. ]
  38. router.beforeEach((to, from, next) => {
  39. NProgress.start() // start progress bar
  40. if (getToken()) { // determine if there has token
  41. /* has token*/
  42. if (to.path === '/login') {
  43. next({ path: '/' })
  44. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  45. } else {
  46. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  47. store.dispatch('GetUserInfo').then(res => {
  48. store.dispatch('GetUserMenus').then(res => { // 拉取user_info
  49. // const roles = res.data.data // note: roles must be a array! such as: ['editor','develop']
  50. const roles = myRoles;
  51. store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
  52. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  53. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  54. })
  55. })
  56. }).catch(() => {
  57. store.dispatch('FedLogOut').then(() => {
  58. Message.error('Verification failed, please login again')
  59. next({ path: '/login' })
  60. })
  61. })
  62. } else {
  63. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  64. if (hasPermission(store.getters.roles, to.meta.roles)) {
  65. next()//
  66. } else {
  67. next({ path: '/401', replace: true, query: { noGoBack: true }})
  68. }
  69. // 可删 ↑
  70. }
  71. }
  72. } else {
  73. /* has no token*/
  74. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  75. next()
  76. } else {
  77. next('/login') // 否则全部重定向到登录页
  78. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  79. }
  80. }
  81. })
  82. router.afterEach(() => {
  83. NProgress.done() // finish progress bar
  84. })