zouyu
2023-11-17 e5ed44879d7722c160b9af63ba51b333bc7f4d1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * 全站权限配置
 *
 */
import router from './router/router';
import store from '@/store';
import { validatenull } from '@/util/validate';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style
NProgress.configure({ showSpinner: false });
 
router.beforeEach((to, from, next) => {
  NProgress.start();
  const meta = to.meta || {};
  if (store.getters.access_token) {
    if (store.getters.isLock && to.path !== '/lock') {
      next({ path: '/lock' });
    } else if (to.path === '/login') {
      next({ path: '/' });
    } else {
      const value = to.query.src || to.fullPath;
      const label = to.query.name || to.name;
      if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
        store.commit('ADD_TAG', {
          label: label,
          value: value,
          params: to.params,
          query: to.query,
          group: router.$avueRouter.group || []
        });
      }
      next();
    }
  } else {
    // 页面多开:载入localStorage的auth参数 By Luxn
    let auth = JSON.parse(localStorage.getItem("authorization") || "{}");
    if ( meta.isAuth === false) {
      next();
    }else{
      if (auth.date_time) {
        let key = ["access_token", "refresh_token", "expires_in", "user_info", "language"];
        key.forEach((item, i) => {
          store.commit("SET_" + item.toUpperCase(), auth[item]);
          store.commit("SET_PERMISSIONS", auth.user_info.authorities || []);
        });
        // 获取左侧导航,打开Path页面 By Luxn
        store.dispatch("GetMenu", { language: auth.language, type: true }).then((data) => {
          let path = window.location.hash.substr(1);
          if (data.length > 0 && path.length > 1) {
            router.$avueRouter.formatRoutes(data, true);
            next({ path: path });
          }
          else {
            next();
          }
        });
      }else{
        next('/login');
      }
 
    }
  }
})
 
router.afterEach((to,from) => {
  NProgress.done();
  const title = store.getters.tag.label;
  router.$avueRouter.setTitle(title);
  // 打开新页面标题不对,根据router配置的title进行替换
  if(to.meta.title){
    router.$avueRouter.setTitle( to.meta.title)
  }
})