permission.js 5.4 KB

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