permission.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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,setToken } 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. 'couponsManage',
  18. 'couponsList',
  19. 'answerGame',
  20. 'answerList',
  21. 'raffleManage',
  22. 'raffleList',
  23. 'raffleDataList',
  24. 'raffleLogsList',
  25. 'goodsManage',
  26. 'goodsExchangeRules',
  27. 'goodsList',
  28. 'giftManage',
  29. 'giftExchangeRules',
  30. 'giftList',
  31. 'welfareManage',
  32. 'welfareList',
  33. 'exchangeManage',
  34. 'exchangeList',
  35. 'cancelledList',
  36. 'approvalList',
  37. 'pasList',
  38. 'voidList',
  39. 'pointManage',
  40. 'pointInstructions',
  41. 'pointRulesList',
  42. 'pointList',
  43. 'pointsDetailList',
  44. 'pointsPlusOrMinus',
  45. 'activityManage',
  46. 'activityList',
  47. 'noticeManage',
  48.   'noticeList',
  49. 'ranking',
  50. 'rankingList',
  51. 'medalManage',
  52. 'medalList',
  53. 'medalDataList',
  54. 'commendManage',
  55. 'commendList',
  56. 'commendDataList',
  57. 'citeList',
  58. 'operateCiteList',
  59. 'ceoCiteList',
  60. 'trainManage',
  61. 'trainList',
  62. 'operateTrainList',
  63. 'ceoTrainList',
  64. 'festivalManage',
  65. 'festivalList',
  66. 'certManage',
  67. 'certRules',
  68. 'certList',
  69. 'dictManage',
  70. 'dictList',
  71. 'dictDataList',
  72. ]
  73. router.beforeEach((to, from, next) => {
  74. debugger
  75. NProgress.start() // start progress bar
  76. // store.dispatch('SetToken', 'y8evar5b5yecmr6hjrhyokxw5tiqizw9');
  77. // setToken('y8evar5b5yecmr6hjrhyokxw5tiqizw9');
  78. const path = to.path;
  79. const token = to.query.xToken;
  80. if (path.indexOf('auth') != -1 && token) {
  81. store.dispatch('SetToken', token);
  82. setToken(token);
  83. }
  84. if (getToken()) { // determine if there has token
  85. /* has token*/
  86. if (to.path === '/login') {
  87. next({ path: '/' })
  88. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  89. } else {
  90. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  91. store.dispatch('GetUserInfo').then(res => {
  92. store.dispatch('GetUserMenus').then(res => { // 拉取user_info
  93. // const roles = res.data.data // note: roles must be a array! such as: ['editor','develop']
  94. const roles = myRoles;
  95. store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
  96. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  97. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  98. next({ path: '/' });
  99. })
  100. })
  101. }).catch(() => {
  102. store.dispatch('FedLogOut').then(() => {
  103. Message.error('Verification failed, please login again')
  104. // const prodHref = 'http://dgt.dgtis.com/oneportal/login';//正式地址
  105. const prodHref = 'http://dgtcloud.dgtis.com/oneportal/login';//阿里云地址
  106. const devHref = 'http://192.168.100.208:8080/oneportal/login';//测试地址
  107. location.href = process.env.NODE_ENV === 'production' ? prodHref : devHref;
  108. // next({ path: '/login' })
  109. })
  110. })
  111. } else {
  112. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  113. if (hasPermission(store.getters.roles, to.meta.roles)) {
  114. next()//
  115. } else {
  116. next({ path: '/401', replace: true, query: { noGoBack: true }})
  117. }
  118. // 可删 ↑
  119. }
  120. }
  121. } else {
  122. /* has no token*/
  123. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  124. next()
  125. } else {
  126. // const prodHref = 'http://dgt.dgtis.com/oneportal/login';//正式地址
  127. const prodHref = 'http://dgtcloud.dgtis.com/oneportal/login';//阿里云地址
  128. const devHref = 'http://192.168.100.208:8080/oneportal/login';//测试地址
  129. location.href = process.env.NODE_ENV === 'production' ? prodHref : devHref;
  130. // next('/login') // 否则全部重定向到登录页
  131. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  132. }
  133. }
  134. })
  135. router.afterEach(() => {
  136. NProgress.done() // finish progress bar
  137. })