123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import router from "./router";
- import store from "./store";
- import { Message } from "element-ui";
- import NProgress from "nprogress";
- import "nprogress/nprogress.css";
- import { getToken, setToken } from "@/utils/auth";
- import { isRelogin } from "@/utils/request";
- import { login2 } from "@/api/login";
- NProgress.configure({ showSpinner: false });
- const whiteList = ["/login", "/register", "/adminLogin"];
- router.beforeEach((to, from, next) => {
- NProgress.start();
- if (getToken()) {
- to.meta.title && store.dispatch("settings/setTitle", to.meta.title);
- /* has token*/
- if (to.path === "/login") {
- next({ path: "/" });
- NProgress.done();
- } else {
- if (store.getters.roles.length === 0) {
- isRelogin.show = true;
- // 判断当前用户是否已拉取完user_info信息
- store
- .dispatch("GetInfo")
- .then(() => {
- isRelogin.show = false;
- store.dispatch("GenerateRoutes").then((accessRoutes) => {
- // 根据roles权限生成可访问的路由表
- router.addRoutes(accessRoutes); // 动态添加可访问路由表
- next({
- path: accessRoutes.length ? accessRoutes[0].path : to.path,
- replace: true,
- });
- // next({ ...to, replace: true }); // hack方法 确保addRoutes已完成
- });
- })
- .catch((err) => {
- store.dispatch("LogOut").then(() => {
- Message.error(err);
- next({ path: "/" });
- });
- });
- } else {
- next();
- }
- //获取消息数量
- // store.dispatch("SET_WAITTASKNUM")
- }
- } else {
- // 没有token
- if (whiteList.indexOf(to.path) !== -1) {
- // 在免登录白名单,直接进入
- next();
- } else {
- let url = window.location.href;
- // if (url.endsWith("admin")) {
- // next(`/adminLogin`);
- // return;
- // }
- next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
- NProgress.done();
- // let urls = url.split("?");
- // if (urls.length > 1) {
- // if (urls[1].startsWith("ticket")) {
- // let params = urls[1].split("=");
- // let args = params[1];
- // if (args == "out") {
- // //生产 10.223.1.127:8098
- // console.log("重定向ISC--------3");
- // window.location.href =
- // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
- // } else {
- // login2(args).then((res) => {
- // if (res == 503) {
- // this.$message.warning("当前账号未开通权限!");
- // } else {
- // setToken(res.token);
- // store.commit("SET_TOKEN", res.token);
- // next(`/`)
- // }
- // });
- // }
- // } else {
- // console.log("重定向ISC--------1");
- // //生产 10.223.1.127:8098
- // window.location.href =
- // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
- // }
- // } else {
- // if (!url.endsWith("admin/")) {
- // console.log("重定向ISC--------2");
- // //生产 10.223.1.127:8098
- // window.location.href =
- // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
- // }
- // }
- // next(`/login`); // 否则全部重定向到登录页
- }
- }
- });
- router.afterEach(() => {
- NProgress.done();
- });
|