permission.js 5.0 KB

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