From bd332b9a03aef5d1304267a6be50561d8a6f6943 Mon Sep 17 00:00:00 2001 From: suerprisePlus <15810472099@163.com> Date: 星期五, 29 十一月 2024 09:40:22 +0800 Subject: [PATCH] 前端跳转地址优化 --- se-ui/src/permission.js | 97 +++++++++++++++++++++++++----------------------- 1 files changed, 50 insertions(+), 47 deletions(-) diff --git a/se-ui/src/permission.js b/se-ui/src/permission.js index bec8366..f183ebf 100644 --- a/se-ui/src/permission.js +++ b/se-ui/src/permission.js @@ -1,73 +1,76 @@ -import router from './router' -import store from './store' -import { Message } from 'element-ui' -import NProgress from 'nprogress' -import 'nprogress/nprogress.css' -import { getToken } from '@/utils/auth' -import { isRelogin } from '@/utils/request' +import router from "./router"; +import store from "./store"; +import { Message } from "element-ui"; +import NProgress from "nprogress"; +import "nprogress/nprogress.css"; +import { getToken } from "@/utils/auth"; +import { isRelogin } from "@/utils/request"; -NProgress.configure({ showSpinner: false }) +NProgress.configure({ showSpinner: false }); -const whiteList = ['/login', '/register'] +const whiteList = ["/login", "/register"]; router.beforeEach((to, from, next) => { - NProgress.start() + NProgress.start(); if (getToken()) { - to.meta.title && store.dispatch('settings/setTitle', to.meta.title) + to.meta.title && store.dispatch("settings/setTitle", to.meta.title); /* has token*/ - if (to.path === '/login') { - if (process.env.NODE_ENV == 'development') { - window.location.href = 'http://localhost:8080/login' - } else if (process.env.NODE_ENV == 'production') { - window.location.href = window.location.origin + '/sso/login' + if (to.path === "/login") { + if (process.env.NODE_ENV == "development") { + window.location.href = window.location.origin + "/login"; + } else if (process.env.NODE_ENV == "production") { + window.location.href = window.location.origin + "/sso/login"; } // next({ path: '/' }) - NProgress.done() + NProgress.done(); } else if (whiteList.indexOf(to.path) !== -1) { - next() + next(); } else { if (store.getters.roles.length === 0) { - isRelogin.show = true + isRelogin.show = true; // 鍒ゆ柇褰撳墠鐢ㄦ埛鏄惁宸叉媺鍙栧畬user_info淇℃伅 - store.dispatch('GetInfo').then(() => { - isRelogin.show = false - store.dispatch('GenerateRoutes').then(accessRoutes => { - // 鏍规嵁roles鏉冮檺鐢熸垚鍙闂殑璺敱琛� - router.addRoutes(accessRoutes) // 鍔ㄦ�佹坊鍔犲彲璁块棶璺敱琛� - next({ ...to, replace: true }) // hack鏂规硶 纭繚addRoutes宸插畬鎴� + store + .dispatch("GetInfo") + .then(() => { + isRelogin.show = false; + store.dispatch("GenerateRoutes").then((accessRoutes) => { + // 鏍规嵁roles鏉冮檺鐢熸垚鍙闂殑璺敱琛� + router.addRoutes(accessRoutes); // 鍔ㄦ�佹坊鍔犲彲璁块棶璺敱琛� + next({ ...to, replace: true }); // hack鏂规硶 纭繚addRoutes宸插畬鎴� + }); }) - }).catch(err => { - store.dispatch('LogOut').then(() => { - Message.error(err) - console.log(process.env.NODE_ENV); - if (process.env.NODE_ENV == 'development') { - window.location.href = 'http://localhost:8080/login' - } else if (process.env.NODE_ENV == 'production') { - window.location.href = window.location.origin + '/sso/login' - } - // next({ path: '/' }) - }) - }) + .catch((err) => { + store.dispatch("LogOut").then(() => { + Message.error(err); + console.log(process.env.NODE_ENV); + if (process.env.NODE_ENV == "development") { + window.location.href = window.location.origin + "/login"; + } else if (process.env.NODE_ENV == "production") { + window.location.href = window.location.origin + "/sso/login"; + } + // next({ path: '/' }) + }); + }); } else { - next() + next(); } } } else { // 娌℃湁token if (whiteList.indexOf(to.path) !== -1) { // 鍦ㄥ厤鐧诲綍鐧藉悕鍗曪紝鐩存帴杩涘叆 - next() + next(); } else { - if (process.env.NODE_ENV == 'development') { - window.location.href = 'http://localhost:8080/login' - } else if (process.env.NODE_ENV == 'production') { - window.location.href = window.location.origin + '/sso/login' + if (process.env.NODE_ENV == "development") { + window.location.href = window.location.origin + "/login"; + } else if (process.env.NODE_ENV == "production") { + window.location.href = window.location.origin + "/sso/login"; } - NProgress.done() + NProgress.done(); } } -}) +}); router.afterEach(() => { - NProgress.done() -}) + NProgress.done(); +}); -- Gitblit v1.9.3