permission.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. router.beforeEach((to, from, next) => {
  12. NProgress.start()
  13. const meta = to.meta || {}
  14. if (store.getters.access_token) {
  15. if (store.getters.isLock && to.path !== '/lock') {
  16. next({ path: '/lock' })
  17. } else if (to.path === '/login') {
  18. next({ path: '/' })
  19. } else {
  20. const value = to.query.src || to.fullPath
  21. const label = to.query.name || to.name
  22. if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
  23. store.commit('ADD_TAG', {
  24. label: label,
  25. value: value,
  26. params: to.params,
  27. query: to.query,
  28. group: router.$avueRouter.group || []
  29. })
  30. }
  31. next()
  32. }
  33. } else {
  34. const specialPath = store.getters.specialPath
  35. const pathQuery = { path: '/login' }
  36. if (specialPath !== 'default') pathQuery.query = { s: specialPath }
  37. if (meta.isAuth === false) {
  38. next()
  39. } else {
  40. next(pathQuery)
  41. }
  42. }
  43. })
  44. router.afterEach(() => {
  45. NProgress.done()
  46. const title = store.getters.tag.label
  47. router.$avueRouter.setTitle(title)
  48. })