guards.js 2.57 KB
Newer Older
1 2 3 4
import { hasAuthority } from '@/utils/authorityUtil';
import { loginIgnore } from '@/router/index';
import { checkAuthorization } from '@/utils/requestUtil';
import NProgress from 'nprogress';
wb-ct393452's avatar
wb-ct393452 committed
5

6
NProgress.configure({ showSpinner: false });
wb-ct393452's avatar
wb-ct393452 committed
7 8 9 10 11 12 13 14

/**
 * 进度条开始
 * @param to
 * @param form
 * @param next
 */
const progressStart = (to, from, next) => {
15 16 17 18 19 20
    // start progress bar
    if (!NProgress.isStarted()) {
        NProgress.start();
    }
    next();
};
wb-ct393452's avatar
wb-ct393452 committed
21 22 23 24 25 26 27 28 29

/**
 * 登录守卫
 * @param to
 * @param form
 * @param next
 * @param options
 */
const loginGuard = (to, from, next, options) => {
30 31 32 33 34 35 36 37
    const { message } = options;
    if (!loginIgnore.includes(to) && !checkAuthorization()) {
        message.warning('登录已失效,请重新登录');
        next({ path: '/login' });
    } else {
        next();
    }
};
wb-ct393452's avatar
wb-ct393452 committed
38 39 40 41 42 43 44 45 46

/**
 * 权限守卫
 * @param to
 * @param form
 * @param next
 * @param options
 */
const authorityGuard = (to, from, next, options) => {
47 48 49 50 51 52 53 54 55 56 57
    const { store, message } = options;
    const permissions = store.getters['accountModule/permissions'];
    const roles = store.getters['accountModule/roles'];
    if (!hasAuthority(to, permissions, roles)) {
        message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`);
        next({ path: '/403' });
        // NProgress.done()
    } else {
        next();
    }
};
wb-ct393452's avatar
wb-ct393452 committed
58 59 60 61 62 63 64 65 66 67

/**
 * 混合导航模式下一级菜单跳转重定向
 * @param to
 * @param from
 * @param next
 * @param options
 * @returns {*}
 */
const redirectGuard = (to, from, next, options) => {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    const { store } = options;
    const getFirstChild = routes => {
        const route = routes[0];
        if (!route.children || route.children.length === 0) {
            return route;
        }
        return getFirstChild(route.children);
    };
    if (store.state.settingModule.layout === 'mix') {
        const firstMenu = store.getters['settingModule/firstMenu'];
        if (firstMenu.find(item => item.fullPath === to.fullPath)) {
            store.commit('settingModule/setActivatedFirst', to.fullPath);
            const subMenu = store.getters['settingModule/subMenu'];
            if (subMenu.length > 0) {
                const redirect = getFirstChild(subMenu);
                return next({ path: redirect.fullPath });
            }
        }
wb-ct393452's avatar
wb-ct393452 committed
86
    }
87 88
    next();
};
wb-ct393452's avatar
wb-ct393452 committed
89 90 91 92 93 94 95 96

/**
 * 进度条结束
 * @param to
 * @param form
 * @param options
 */
const progressDone = () => {
97 98 99
    // finish progress bar
    NProgress.done();
};
wb-ct393452's avatar
wb-ct393452 committed
100 101

export default {
102 103 104
    beforeEach: [progressStart, loginGuard, authorityGuard, redirectGuard],
    afterEach: [progressDone],
};