You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LiaoNingDangAn/src/permission.js

88 lines
2.5 KiB

2 years ago
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"];
2 years ago
2 years ago
router.beforeEach(async (to, from, next) => {
if (to.name) NProgress.start();
2 years ago
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");
2 years ago
2 years ago
if (userName.username == "admin") {
next();
} else {
let DongtrouterList = JSON.parse(
localStorage.getItem("DongtrouterList_router")
)?.find((res) => {
return res.menuUrl == "monitor";
});
2 years ago
2 years ago
// 过滤出已经不在路由表中的数据
let showTrue = DongtrouterList.childMenuList.find(
(item) => item.menuUrl === to.fullPath
);
// 如果不存在则跳到新路由表中的第0项--解决刷新白屏
if (showTrue) {
next();
} else {
next(DongtrouterList.childMenuList[0].menuUrl);
}
2 years ago
}
2 years ago
} else if (to.path.split("/")[1] == "convenient") {
localStorage.setItem("Login_index", "2");
2 years ago
2 years ago
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);
}
2 years ago
}
}
if (to.path.split("/")[1] == "dw") {
next();
}
if (to.path.split("/")[1] == "no-top") {
next();
}
2 years ago
if (to.path.split("/")[1] == "login") {
next();
}
2 years ago
});
router.afterEach(() => {
NProgress.done();
});