声脉桥梁云监控平台

permission.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import router from './router'
  2. import store from './store'
  3. import NProgress from 'nprogress' // Progress 进度条
  4. import 'nprogress/nprogress.css'// Progress 进度条样式
  5. import { getCookiesName } from '@/utils/auth' // 验权
  6. import queryInfo from '@/utils/queryInfo'
  7. NProgress.configure({ showSpinner: false })// NProgress Configuration
  8. function hasPermission(roles, permissionRoles) {
  9. if (roles.indexOf('1') >= 0) return true
  10. if (!permissionRoles) return true
  11. return roles.some(role => permissionRoles.indexOf(role) >= 0)
  12. }
  13. const whiteList = ['/login', '/findPwd'] // 不重定向白名单
  14. router.beforeEach((to, from, next) => {
  15. NProgress.start()
  16. if (getCookiesName()) {
  17. if (to.path === '/login') {
  18. next({ path: '/' })
  19. NProgress.done()
  20. } else {
  21. queryInfo.queryAllInfo()
  22. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  23. // console.log(5555)
  24. store.dispatch('GetUserInfo').then(res => { // 拉取user_info
  25. if (res.success) {
  26. const roles = [res.data.type.toString()]
  27. store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
  28. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  29. next({ ...to, replace: true })
  30. })
  31. }
  32. })
  33. } else {
  34. // console.log(4444)
  35. if (hasPermission(store.getters.roles, to.meta.roles)) {
  36. next()
  37. } else {
  38. // console.log(777)
  39. }
  40. }
  41. }
  42. } else {
  43. if (whiteList.indexOf(to.path) !== -1) {
  44. next()
  45. } else {
  46. next('/login')
  47. NProgress.done()
  48. }
  49. }
  50. })
  51. router.afterEach(() => {
  52. NProgress.done() // 结束Progress
  53. })