permission.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import Cookies from 'js-cookie'
  2. import router from './router'
  3. import store from './store'
  4. import Vue from 'vue'
  5. import {
  6. Message
  7. } from 'element-ui'
  8. import {
  9. cookGetToken,
  10. localSetToken,
  11. getToken,
  12. removeToken
  13. } from '@/utils/auth'
  14. import {
  15. wxAuth
  16. } from '@/utils/wx-auth-two.js';
  17. function hasPermission(roles, permissionRoles) {
  18. if (roles.indexOf('admin') >= 0) return true
  19. if (!permissionRoles) return true
  20. return roles.some(role => permissionRoles.indexOf(role) >= 0)
  21. }
  22. // 免登名单
  23. const whiteList = ['/invite_new_company', '/create_company', '/login', '/loginTransfer', '/noAccess', '/loginbytoken',
  24. '/reg', '/forgetPwd', '/resetPwd', '/android', '/demo', '/swiperShow', '/autoLogin'
  25. ] // no redirect whitelist
  26. const stopPath = ['/login', '/noAccess']
  27. router.beforeEach((to, from, next) => {
  28. if (Vue.$httpRequestList.length > 0) { //强行中断时才向下执行
  29. Vue.$httpRequestList.forEach(item => {
  30. item('interrupt'); //给个标志,中断请求
  31. })
  32. }
  33. if (stopPath.indexOf(to.path) !== -1) {
  34. next()
  35. } else {
  36. if (cookGetToken()) {
  37. if (getToken() !== cookGetToken()) {
  38. store.commit('SET_DELETE_USER', {})
  39. store.commit('SET_TOKEN', cookGetToken())
  40. localStorage.setItem('Admin-Token', cookGetToken())
  41. init(to, next)
  42. } else {
  43. init(to, next)
  44. }
  45. } else if (getToken()) {
  46. if (!store.state.user.token) store.commit("SET_TOKEN", getToken());
  47. init(to, next)
  48. } else {
  49. if (whiteList.indexOf(to.path) !== -1 || window.location.href.indexOf('loginbytoken/') !== -1) { // 在免登录白名单,直接进入
  50. next()
  51. } else {
  52. window.location.href = process.env.BASE_API //直接重新授权
  53. }
  54. }
  55. }
  56. })
  57. function init(to, next) {
  58. if (to.path === '/login') {
  59. next({ path: '/'})
  60. } else {
  61. if (!store.getters.user_info.id) { // 判断当前用户是否已拉取完user_info信息
  62. store.dispatch('GetUserInfo').then(res => { // 拉取user_info
  63. // 设置title
  64. document.title = ''
  65. const mo_list = []
  66. res.data['mo_list'] = []
  67. for (const i in mo_list) {
  68. res.data['mo_list'].push(mo_list[i].code)
  69. }
  70. const roles = ['admin']
  71. store.dispatch('GenerateRoutes', { roles}).then(() => {
  72. // 增加动态权限列表,在这里发送一个请求,得到路由
  73. const addRouters = []
  74. for (const item in store.getters.addRouters) {
  75. const obj = []
  76. for (const ritem in store.getters.addRouters[item].children) {
  77. if (res.data.mo_list.indexOf(store.getters.addRouters[item].children[ritem].name) >= 0 || store
  78. .getters.addRouters[item].children[ritem].hidden === true || store.getters.addRouters[item]
  79. .children[ritem].common === true) {
  80. obj.push(store.getters.addRouters[item].children[ritem])
  81. }
  82. obj.push(store.getters.addRouters[item].children[ritem])
  83. }
  84. if (obj.length > 0) {
  85. const _R = store.getters.addRouters[item]
  86. _R['children'] = obj
  87. addRouters.push(_R)
  88. }
  89. }
  90. router.addRoutes(addRouters) // 动态添加可访问路由表
  91. next({
  92. ...to,
  93. replace: true
  94. }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  95. })
  96. }).catch((err) => {
  97. if (getToken()) {
  98. next('/login')
  99. }
  100. })
  101. } else {
  102. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  103. if (hasPermission(store.getters.roles, to.meta.roles)) {
  104. wxAuth('', next);
  105. } else {
  106. next({
  107. path: '/401',
  108. replace: true,
  109. query: {
  110. noGoBack: true
  111. }
  112. })
  113. }
  114. }
  115. }
  116. }
  117. router.afterEach((to, from) => {
  118. console.log("当前页面"+to.name)
  119. })