permission.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * 全站权限配置
  3. *
  4. */
  5. import router from './router/router'
  6. import store from '@/store'
  7. import {validatenull} from '@/util/validate'
  8. import NProgress from 'nprogress' // progress bar
  9. import 'nprogress/nprogress.css' // progress bar style
  10. NProgress.configure({showSpinner: false})
  11. /**
  12. * 导航守卫,相关内容可以参考:
  13. * https://router.vuejs.org/zh/guide/advanced/navigation-guards.html
  14. */
  15. router.beforeEach((to, from, next) => {
  16. // 缓冲设置
  17. if (to.meta.keepAlive === true && store.state.tags.tagList.some(ele => {
  18. return ele.value === to.fullPath
  19. })) {
  20. to.meta.$keepAlive = true
  21. } else {
  22. NProgress.start()
  23. if (to.meta.keepAlive === true && validatenull(to.meta.$keepAlive)) {
  24. to.meta.$keepAlive = true
  25. } else {
  26. to.meta.$keepAlive = false
  27. }
  28. }
  29. const meta = to.meta || {}
  30. if (store.getters.access_token) {
  31. if (to.path === '/login') {
  32. next({path: '/'})
  33. } else {
  34. // NOTE: 当用户角色不存在时,会存在无限请求用户信息接口的问题
  35. if (store.getters.roles.length === 0) {
  36. // store.dispatch('GetUserInfo').then(() => {
  37. next()
  38. // }).catch(() => {
  39. // store.dispatch('FedLogOut').then(() => {
  40. // next({path: '/login'})
  41. // })
  42. // })
  43. } else {
  44. const value = to.query.src || to.fullPath
  45. const label = to.query.name || to.name
  46. if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
  47. store.commit('ADD_TAG', {
  48. label: label,
  49. value: value,
  50. params: to.params,
  51. query: to.query,
  52. group: router.$avueRouter.group || []
  53. })
  54. }
  55. next()
  56. }
  57. }
  58. // if(to.fullPath=="/idp/data/forecastpowerdata/index"&&from.fullPath=='/homePage'){
  59. // var href = location.href.split("#")[0]
  60. // if (location.href.indexOf("#reloaded") == -1) {
  61. // location.href = href+"#/idp/data/forecastpowerdata/index"+"#reloaded";
  62. // location.reload();
  63. // }
  64. // }
  65. } else {
  66. if (meta.isAuth === false) {
  67. next()
  68. } else {
  69. next('/login')
  70. }
  71. }
  72. })
  73. router.afterEach(() => {
  74. NProgress.done()
  75. const title = store.getters.tag.label
  76. router.$avueRouter.setTitle(title)
  77. })