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