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 API from "@/api/index.js"; import Cookies from "js-cookie"; import axios from "axios"; NProgress.configure({ showSpinner: false }); const whiteList = ["/login", "/register"]; router.beforeEach(async (to, from, next) => { if (to.name) NProgress.start(); console.log("进入", to); let userName = JSON.parse(localStorage.getItem("userDto")); // 如果是空的跳到login不让进 if (JSON.parse(localStorage.getItem("DongtrouterList_router")) == []) { location.href = `${location.origin}/login`; } if (to.path.split("/")[1] == "monitor") { // 因为先走路由拦截,再走路由渲染,可能导致停留在已经删除的路由页面中,导致白屏,所以每次刷新要在main.js中更新路由数据 localStorage.setItem("Login_index", "1"); if (userName.username == "admin") { next(); } else { let DongtrouterList = JSON.parse( localStorage.getItem("DongtrouterList_router") )?.find((res) => { return res.menuUrl == "monitor"; }); // 过滤出已经不在路由表中的数据 let showTrue = DongtrouterList.childMenuList.find( (item) => item.menuUrl === to.fullPath ); // 如果不存在,则跳到新路由表中的第0项--解决刷新白屏 if (showTrue) { next(); } else { next(DongtrouterList.childMenuList[0].menuUrl); } } } else if (to.path.split("/")[1] == "convenient") { localStorage.setItem("Login_index", "2"); if (userName.username == "admin") { next(); } else { // 便民服务 let DongtrouterList = JSON.parse( localStorage.getItem("DongtrouterList_router") ).find((res) => { return res.menuUrl == "convenient"; }); if (DongtrouterList.length == []) { location.href = `${location.origin}/login`; } let showTrue = DongtrouterList.childMenuList.find( (item) => item.menuUrl === to.fullPath ); if (showTrue) { next(); } else { next(DongtrouterList.childMenuList[0].menuUrl); } } } if (to.path.split("/")[1] == "dw") { next(); } if (to.path.split("/")[1] == "no-top") { next(); } if (to.path.split("/")[1] == "login") { next(); } }); router.afterEach(() => { NProgress.done(); });