permission.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // 入库单
  95. 'warehousing',
  96. 'warehousingAdd',
  97. 'warehousingEdit',
  98. 'warehousingDetail',
  99. // 出库单
  100. 'retrieval',
  101. 'retrievalAdd',
  102. 'retrievalEdit',
  103. 'retrievalDetail',
  104. // 调拨单
  105. 'requisition',
  106. 'requisitionAdd',
  107. 'requisitionEdit',
  108. 'requisitionDetail',
  109. // 换货单
  110. 'swap',
  111. 'swapAdd',
  112. 'swapEdit',
  113. 'swapDetail',
  114. // 组装单
  115. 'assembly',
  116. 'assemblyAdd',
  117. 'assemblyEdit',
  118. 'assemblyDetail',
  119. // 拆卸单
  120. 'disassembly',
  121. 'disassemblyAdd',
  122. 'disassemblyEdit',
  123. 'disassemblyDetail',
  124. // 报损单
  125. 'breakage',
  126. 'breakageAdd',
  127. 'breakageEdit',
  128. 'breakageDetail',
  129. // 盘点单
  130. 'stockCount',
  131. 'stockCountAdd',
  132. 'stockCountEdit',
  133. 'stockCountDetail',
  134. // 客户管理
  135. 'customer',
  136. 'distributor',
  137. 'business',
  138. 'vip',
  139. 'guest',
  140. //报表管理
  141. 'report',
  142. 'stockReport',
  143. 'dictManage',
  144. 'dictList',
  145. 'dictDataList',
  146. 'sysManage',
  147. 'admin',
  148. 'dept',
  149. 'role',
  150. ]
  151. router.beforeEach((to, from, next) => {
  152. NProgress.start() // start progress bar
  153. // store.dispatch('SetToken', 'y8evar5b5yecmr6hjrhyokxw5tiqizw9');
  154. // setToken('y8evar5b5yecmr6hjrhyokxw5tiqizw9');
  155. const path = to.path;
  156. const token = to.query.xToken;
  157. if (path.indexOf('auth') != -1 && token) {
  158. store.dispatch('SetToken', token);
  159. setToken(token);
  160. }
  161. if (getToken()) { // determine if there has token
  162. /* has token*/
  163. if (to.path === '/login') {
  164. next({ path: '/' })
  165. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  166. } else {
  167. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  168. store.dispatch('GetUserInfo').then(res => {
  169. store.dispatch('GetUserMenus').then(res => { // 拉取user_info
  170. // const roles = res.data.data // note: roles must be a array! such as: ['editor','develop']
  171. const roles = myRoles;
  172. store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
  173. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  174. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  175. next({ path: '/' });
  176. })
  177. })
  178. }).catch(() => {
  179. store.dispatch('FedLogOut').then(() => {
  180. Message.error('Verification failed, please login again')
  181. const prodHref = 'http://dgt.dgtis.com/oneportal/login';//正式地址
  182. // const prodHref = 'http://dgtcloud.dgtis.com/oneportal/login';//阿里云地址
  183. const devHref = 'http://192.168.100.87:8080/oneportal/login';//测试地址
  184. location.href = process.env.NODE_ENV === 'production' ? prodHref : devHref;
  185. // next({ path: '/login' })
  186. })
  187. })
  188. } else {
  189. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  190. if (hasPermission(store.getters.roles, to.meta.roles)) {
  191. next()//
  192. } else {
  193. next({ path: '/401', replace: true, query: { noGoBack: true }})
  194. }
  195. // 可删 ↑
  196. }
  197. }
  198. } else {
  199. /* has no token*/
  200. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  201. next()
  202. } else {
  203. const prodHref = 'http://dgt.dgtis.com/oneportal/login';//正式地址
  204. // const prodHref = 'http://dgtcloud.dgtis.com/oneportal/login';//阿里云地址
  205. const devHref = 'http://192.168.100.87:8080/oneportal/login';//测试地址
  206. location.href = process.env.NODE_ENV === 'production' ? prodHref : devHref;
  207. // next('/login') // 否则全部重定向到登录页
  208. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  209. }
  210. }
  211. })
  212. router.afterEach(() => {
  213. NProgress.done() // finish progress bar
  214. })