permission.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 (to.query.target) {
  34. window.open(value)
  35. } else if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
  36. store.commit('ADD_TAG', {
  37. label: label,
  38. value: value,
  39. params: to.params,
  40. query: to.query,
  41. meta: (() => {
  42. if (!i18n) {
  43. return meta
  44. }
  45. return {
  46. i18n: i18n
  47. }
  48. })(),
  49. group: router.$avueRouter.group || []
  50. });
  51. }
  52. next()
  53. }
  54. }
  55. } else {
  56. //判断是否需要认证,没有登录访问去登录页
  57. if (meta.isAuth === false) {
  58. next()
  59. } else {
  60. next('/login')
  61. }
  62. }
  63. })
  64. router.afterEach(() => {
  65. NProgress.done();
  66. let title = store.getters.tag.label;
  67. let i18n = store.getters.tag.meta.i18n;
  68. title = router.$avueRouter.generateTitle(title, i18n);
  69. //判断登录页的情况
  70. if (router.history.current.fullPath === "/login") {
  71. title = "登录";
  72. }
  73. //根据当前的标签也获取label的值动态设置浏览器标题
  74. router.$avueRouter.setTitle(title);
  75. });