permission.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. 'upLoadFileRules',
  62. 'trainList',
  63. 'operateTrainList',
  64. 'ceoTrainList',
  65. 'festivalManage',
  66. 'festivalList',
  67. 'certManage',
  68. 'certSetList',
  69. 'certRules',
  70. 'certList',
  71. 'dictManage',
  72. 'dictList',
  73. 'dictDataList',
  74. 'postManage',
  75. 'postList',
  76. 'postApprovalList',
  77. 'rankingManage',
  78. 'answerRanking',
  79. 'gameRanking'
  80. ]
  81. router.beforeEach((to, from, next) => {
  82. NProgress.start() // start progress bar
  83. // store.dispatch('SetToken', 'y8evar5b5yecmr6hjrhyokxw5tiqizw9');
  84. // setToken('y8evar5b5yecmr6hjrhyokxw5tiqizw9');
  85. const path = to.path;
  86. const token = to.query.xToken;
  87. if (path.indexOf('auth') != -1 && token) {
  88. store.dispatch('SetToken', token);
  89. setToken(token);
  90. }
  91. if (getToken()) { // determine if there has token
  92. /* has token*/
  93. if (to.path === '/login') {
  94. next({ path: '/' })
  95. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  96. } else {
  97. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  98. store.dispatch('GetUserInfo').then(res => {
  99. store.dispatch('GetUserMenus').then(res => { // 拉取user_info
  100. const roles = res.data.data // note: roles must be a array! such as: ['editor','develop']
  101. // const roles = myRoles;
  102. store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
  103. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  104. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  105. next({ path: '/' });
  106. })
  107. })
  108. }).catch(() => {
  109. store.dispatch('FedLogOut').then(() => {
  110. Message.error('Verification failed, please login again')
  111. const prodHref = 'http://dgt.dgtis.com/oneportal/login';//正式地址
  112. // const prodHref = 'http://dgtcloud.dgtis.com/oneportal/login';//阿里云地址
  113. const devHref = 'http://192.168.100.208:8080/oneportal/login';//测试地址
  114. location.href = process.env.NODE_ENV === 'production' ? prodHref : devHref;
  115. // next({ path: '/login' })
  116. })
  117. })
  118. } else {
  119. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  120. if (hasPermission(store.getters.roles, to.meta.roles)) {
  121. next()//
  122. } else {
  123. next({ path: '/401', replace: true, query: { noGoBack: true }})
  124. }
  125. // 可删 ↑
  126. }
  127. }
  128. } else {
  129. /* has no token*/
  130. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  131. next()
  132. } else {
  133. const prodHref = 'http://dgt.dgtis.com/oneportal/login';//正式地址
  134. // const prodHref = 'http://dgtcloud.dgtis.com/oneportal/login';//阿里云地址
  135. const devHref = 'http://192.168.100.208:8080/oneportal/login';//测试地址
  136. location.href = process.env.NODE_ENV === 'production' ? prodHref : devHref;
  137. // next('/login') // 否则全部重定向到登录页
  138. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  139. }
  140. }
  141. })
  142. router.afterEach(() => {
  143. NProgress.done() // finish progress bar
  144. })