permission.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * 全站权限配置
  3. *
  4. */
  5. import router from './router/router'
  6. import store from './store'
  7. import {validatenull} from '@/util/validate'
  8. import {getToken} from '@/util/auth'
  9. import NProgress from 'nprogress' // progress bar
  10. import 'nprogress/nprogress.css' // progress bar style
  11. NProgress.configure({showSpinner: false});
  12. const lockPage = store.getters.website.lockPage; //锁屏页
  13. router.beforeEach((to, from, next) => {
  14. const meta = to.meta || {};
  15. const isMenu = meta.menu === undefined ? to.query.menu : meta.menu;
  16. store.commit('SET_IS_MENU', isMenu === undefined);
  17. if (getToken()) {
  18. if (store.getters.isLock && to.path !== lockPage) { //如果系统激活锁屏,全部跳转到锁屏页
  19. next({path: lockPage})
  20. } else if (to.path === '/login') { //如果登录成功访问登录页跳转到主页
  21. next({path: '/'})
  22. } else {
  23. //如果用户信息为空则获取用户信息,获取用户信息失败,跳转到登录页
  24. if (store.getters.token.length === 0) {
  25. store.dispatch('FedLogOut').then(() => {
  26. next({path: '/login'})
  27. })
  28. } else {
  29. const value = to.query.src || to.fullPath;
  30. const label = to.query.name || to.name;
  31. const meta = to.meta || router.$avueRouter.meta || {};
  32. const i18n = to.query.i18n;
  33. if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
  34. store.commit('ADD_TAG', {
  35. label: label,
  36. value: value,
  37. params: to.params,
  38. query: to.query,
  39. meta: (() => {
  40. if (!i18n) {
  41. return meta
  42. }
  43. return {
  44. i18n: i18n
  45. }
  46. })(),
  47. group: router.$avueRouter.group || []
  48. });
  49. }
  50. next()
  51. }
  52. }
  53. } else {
  54. //判断是否需要认证,没有登录访问去登录页
  55. if (meta.isAuth === false) {
  56. next()
  57. } else {
  58. next('/login')
  59. }
  60. }
  61. })
  62. router.afterEach(() => {
  63. NProgress.done();
  64. let title = store.getters.tag.label;
  65. let i18n = store.getters.tag.meta.i18n;
  66. title = router.$avueRouter.generateTitle(title, i18n)
  67. //根据当前的标签也获取label的值动态设置浏览器标题
  68. router.$avueRouter.setTitle(title);
  69. });