permission.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. console.log(getToken())
  18. if (getToken()) {
  19. if (store.getters.isLock && to.path !== lockPage) { //如果系统激活锁屏,全部跳转到锁屏页
  20. next({path: lockPage})
  21. } else if (to.path === '/login') { //如果登录成功访问登录页跳转到主页
  22. next({path: '/'})
  23. } else {
  24. console.log(store.getters)
  25. //如果用户信息为空则获取用户信息,获取用户信息失败,跳转到登录页
  26. if (store.getters.token.length === 0) {
  27. store.dispatch('FedLogOut').then(() => {
  28. if(window.__POWERED_BY_QIANKUN__){
  29. Vue.prototype.$parentRouter.push('/login')
  30. }else{
  31. next({path: '/login'})
  32. }
  33. })
  34. } else {
  35. const value = to.query.src || to.fullPath;
  36. const label = to.query.name || to.name;
  37. const meta = to.meta || router.$avueRouter.meta || {};
  38. const i18n = to.query.i18n;
  39. if (to.query.target) {
  40. window.open(value)
  41. } else if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
  42. store.commit('ADD_TAG', {
  43. label: label,
  44. value: value,
  45. params: to.params,
  46. query: to.query,
  47. meta: (() => {
  48. if (!i18n) {
  49. return meta
  50. }
  51. return {
  52. i18n: i18n
  53. }
  54. })(),
  55. group: router.$avueRouter.group || []
  56. });
  57. }
  58. next()
  59. }
  60. }
  61. } else {
  62. //判断是否需要认证,没有登录访问去登录页
  63. if (meta.isAuth === false) {
  64. next()
  65. } else {
  66. next('/login')
  67. }
  68. }
  69. })
  70. router.afterEach(() => {
  71. NProgress.done();
  72. let title = store.getters.tag.label;
  73. let i18n = store.getters.tag.meta.i18n;
  74. title = router.$avueRouter.generateTitle(title, i18n);
  75. //判断登录页的情况
  76. if (router.history.current.fullPath === "/login") {
  77. title = "登录";
  78. }
  79. //根据当前的标签也获取label的值动态设置浏览器标题
  80. router.$avueRouter.setTitle(title);
  81. });