董国庆
2025-03-31 12a48c637cca91d063db7a5d5242f001237814f2
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Vue from "vue";
import VueRouter from "vue-router";
import Layouts from "../layouts";
import Parent from "../layouts/components/AppContent"
import store from "../store";
 
Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
 
/**
 *  path: "/login",   ------页面地址
    component: () => import("../views/login"),  ------组件地址
    meta: {
      title: "登录",    ------页面标题
      icon: "el-icon-user-solid",  ------菜单图标
      oneself: true,  ------是否在单独页面打开
      hide: true,  ------是否隐藏改菜单
    }
 */
 
const routes = [
    {
        path: "",
        redirect: "login",
        component: Layouts,
        children: [
            {
                path: "/login",
                meta: {
                    title: "登录",
                    oneself: true,
                    hide: true,
                    privilege: 'login'
                },
                component: () => import("../views/login"),
            },
        ],
    },{
        path: "",
        redirect: "dispatching",
        component: Layouts,
        children: [
            {
                path: "/dispatching",
                meta: {
                    title: "实验调度管理",
                    oneself: true,
                    hide: true,
                    privilege: 'dispatching'
                },
                component: () => import("../views/dispatching/list.vue"),
            },
        ],
    },
];
 
const router = new VueRouter({
    mode: "hash",
    base: process.env.BASE_URL,
    routes,
});
 
// 前置路由拦截器
router.beforeEach((to, from, next) => {
    // 设置当前页签名称
    document.title = to.meta.title || '职评网管理系统';
    // 没有登录并且要去的页面不是登录页面,在强制跳转到登录
    if (to.path === "/login") {
        localStorage.removeItem('userInfo')
        next()
    } else if (!localStorage.getItem('userInfo')) {
        next('/login')
    } else {
        // 判断是否拥有要跳转菜单权限
        let menus = store.state.menus
 
        // console.log(store.state.menus);
        // console.log(to.meta);
        // console.log(to.meta.hasOwnProperty('privilege'));
        // console.log(!menus.includes(to.meta.privilege));
        
        if (to.meta.hasOwnProperty('privilege') && !menus.includes(to.meta.privilege)) {
            return
        }
        next()
    }
});
 
export default router;