Commit 07512c16 authored by “yiyousong”'s avatar “yiyousong”

pref:修改路由守卫

parent 03f6b05c
import { hasAuthority } from '@/utils/authority-utils'
import { hasAuthority } from "@/utils/authority-utils";
// import {loginIgnore} from '@/router/index'
// import {checkAuthorization} from '@/utils/request'
import NProgress from 'nprogress'
import local from "@/utils/local"
import NProgress from "nprogress";
import local from "@/utils/local";
NProgress.configure({ showSpinner: false })
NProgress.configure({ showSpinner: false });
/**
* 进度条开始
......@@ -15,10 +15,10 @@ NProgress.configure({ showSpinner: false })
const progressStart = (to, from, next) => {
// start progress bar
if (!NProgress.isStarted()) {
NProgress.start()
NProgress.start();
}
next()
}
next();
};
/**
* 登录守卫
......@@ -28,15 +28,15 @@ const progressStart = (to, from, next) => {
* @param options
*/
const loginGuard = (to, from, next) => {
let token = local.getLocal('token') ? true : false
let token = local.getLocal("token") ? true : false;
if (token) {
next()
next();
} else {
// 再次判断防止死循环
if (to.path === '/jump') {
next()
if (to.path === "/jump") {
next();
} else {
location.href = 'http://192.168.0.98:11072/#/'
location.href = process.env.VUE_APP_API_portal_URL + "/#/";
}
}
// const {message} = options
......@@ -46,7 +46,7 @@ const loginGuard = (to, from, next) => {
// } else {
// }
// next()
}
};
/**
* 权限守卫
......@@ -56,17 +56,17 @@ const loginGuard = (to, from, next) => {
* @param options
*/
const authorityGuard = (to, from, next, options) => {
const { store, message } = options
const permissions = store.getters['account/permissions']
const roles = store.getters['account/roles']
const { store, message } = options;
const permissions = store.getters["account/permissions"];
const roles = store.getters["account/roles"];
if (!hasAuthority(to, permissions, roles)) {
message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`)
next({ path: '/403' })
message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`);
next({ path: "/403" });
// NProgress.done()
} else {
next()
next();
}
}
};
/**
* 混合导航模式下一级菜单跳转重定向
......@@ -77,28 +77,31 @@ const authorityGuard = (to, from, next, options) => {
* @returns {*}
*/
const redirectGuard = (to, from, next, options) => {
const { store } = options
const { store } = options;
const getFirstChild = (routes) => {
const route = routes[0]
const route = routes[0];
if (!route.children || route.children.length === 0) {
return route
return route;
}
return getFirstChild(route.children)
}
return getFirstChild(route.children);
};
// 自定义 || head模式展示一级菜单
if (store.state.setting.layout === 'mix' || store.state.setting.layout === 'head') {
const firstMenu = store.getters['setting/firstMenu']
if (firstMenu.find(item => item.fullPath === to.fullPath)) {
store.commit('setting/setActivatedFirst', to.fullPath)
const subMenu = store.getters['setting/subMenu']
if (
store.state.setting.layout === "mix" ||
store.state.setting.layout === "head"
) {
const firstMenu = store.getters["setting/firstMenu"];
if (firstMenu.find((item) => item.fullPath === to.fullPath)) {
store.commit("setting/setActivatedFirst", to.fullPath);
const subMenu = store.getters["setting/subMenu"];
if (subMenu.length > 0) {
const redirect = getFirstChild(subMenu)
return next({ path: redirect.fullPath })
const redirect = getFirstChild(subMenu);
return next({ path: redirect.fullPath });
}
}
}
next()
}
next();
};
/**
* 进度条结束
......@@ -108,10 +111,10 @@ const redirectGuard = (to, from, next, options) => {
*/
const progressDone = () => {
// finish progress bar
NProgress.done()
}
NProgress.done();
};
export default {
beforeEach: [progressStart, authorityGuard, loginGuard, redirectGuard],
afterEach: [progressDone]
}
afterEach: [progressDone],
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment