123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import router from "./router";
- import store from "./store";
- import { Message } from "element-ui";
- import NProgress from "nprogress"; // progress bar
- import "nprogress/nprogress.css"; // progress bar style
- import { getToken, getMenuTree } from "@/utils/auth"; // get token from cookie
- import getPageTitle from "@/utils/get-page-title";
- import { getMenuTreeAsync } from "@/router/index";
- import { currentUserName } from "./utils/trace";
- NProgress.configure({
- showSpinner: false,
- }); // NProgress Configuration
- const whiteList = ["/login"]; // no redirect whitelist
- router.beforeEach(async (to, from, next) => {
- // start progress bar
- NProgress.start();
- console.log(to);
- // set page title
- document.title = getPageTitle(to.meta.title);
- // determine whether the user has logged in
- const hasToken = getToken();
- const hasMenu = getMenuTree();
- const asyncRouter = getMenuTreeAsync();
- let redirectRouter = "/";
- if (
- asyncRouter &&
- asyncRouter.length > 0 &&
- asyncRouter[0].children &&
- asyncRouter[0].children.length > 0
- ) {
- if (asyncRouter[0].path !== "/") {
- redirectRouter =
- asyncRouter[0].path + "/" + asyncRouter[0].children[0].path;
- } else {
- redirectRouter = asyncRouter[0].children[0].path;
- }
- }
- //auth
- if (to.path.indexOf("role-user-list") > -1) {
- sessionStorage.setItem("RoleUserListKey", to.name);
- }
- //pmc
- if (to.path.indexOf("pmc") > -1) {
- sessionStorage.setItem("RoleUserListKey", to.name);
- }
- // console.log("redirectRouter", redirectRouter)
- if (hasToken && hasMenu) {
- if (to.path === "/login") {
- // if is logged in, redirect to the home page
- next({
- path: redirectRouter,
- });
- NProgress.done();
- } else if (
- (from.path === "/equip-management/add-box" ||
- from.path === "/equip-management/edit-box" ||
- from.path === "/equip-management/add-box-template" ||
- from.path === "/equip-management/edit-box-template") &&
- sessionStorage.normalProcess !== "skip"
- ) {
- const answer = window.confirm("是否确定跳转?");
- if (answer) {
- next();
- } else {
- next(false);
- }
- } else {
- // get analytics
- await store.dispatch("user/setNums");
- setInterval(() => {
- store.dispatch("user/setNums");
- }, 30000)
-
- const hasGetUserInfo = store.getters.name;
- if (hasGetUserInfo) {
- next();
- } else {
- try {
- // get user info
- await store.dispatch("user/getInfo");
- next();
- } catch (error) {
- // remove token and go to login page to re-login
- await store.dispatch("user/resetToken");
- Message.error(error || "Has Error");
- next(`/login?redirect=${to.path}`);
- NProgress.done();
- }
- }
- }
- } else {
- /* has no token*/
- if (whiteList.indexOf(to.path) !== -1) {
- // in the free login whitelist, go directly
- next();
- } else {
- // other pages that do not have permission to access are redirected to the login page.
- next(`/login?redirect=${to.path}`);
- NProgress.done();
- }
- }
- });
- router.afterEach(() => {
- // finish progress bar
- NProgress.done();
- });
|