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.
207 lines
6.2 KiB
207 lines
6.2 KiB
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 { isPathMatch } from "@/utils/validate";
|
|
import { isRelogin } from "@/utils/request";
|
|
import { extractPrefix } from "@/utils/common";
|
|
|
|
// 政务登录接口
|
|
import { governmentGetInfo } from "@/api/login/index";
|
|
|
|
NProgress.configure({ showSpinner: false });
|
|
|
|
const whiteList = ["/login", "/entLogin", "/register"];
|
|
|
|
const isWhiteList = (path) => {
|
|
return whiteList.some((pattern) => isPathMatch(pattern, path));
|
|
};
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
if (!extractPrefix(to.path)) {
|
|
store.commit("SET_CRUMBS", false);
|
|
}
|
|
NProgress.start();
|
|
|
|
const search =
|
|
window.location.search || window.location.hash.split("?")[1] || "";
|
|
const params = new URLSearchParams(search);
|
|
const userToken = params.get("userToken");
|
|
const signature = params.get("signature");
|
|
const timespan = params.get("timespan");
|
|
|
|
// =============================
|
|
// 🔑 新增:处理 clienttoken 登录企业单点登录逻辑
|
|
// =============================
|
|
if (window.location.href.includes("clienttoken=")) {
|
|
const reg = /[?&]clienttoken=([^&#]+)/;
|
|
const match = window.location.href.match(reg);
|
|
const clienttoken = match && match[1];
|
|
|
|
if (clienttoken) {
|
|
// 清除 clienttoken 避免死循环
|
|
let modifiedUrl = window.location.href.replace(
|
|
/[?&]clienttoken=[^&#]+/,
|
|
""
|
|
);
|
|
if (modifiedUrl.endsWith("?") || modifiedUrl.endsWith("&")) {
|
|
modifiedUrl = modifiedUrl.slice(0, -1);
|
|
}
|
|
|
|
// 如果 roles 还未加载,则拉取用户信息
|
|
if (store.getters.roles.length === 0) {
|
|
isRelogin.show = true;
|
|
|
|
store
|
|
.dispatch("SingleSignOnGetInfo", { clientToken: clienttoken })
|
|
.then(() => {
|
|
isRelogin.show = false;
|
|
|
|
// 清除 URL 中的 clienttoken 并刷新页面
|
|
window.history.replaceState({}, "", modifiedUrl);
|
|
|
|
// 获取用户权限并生成路由
|
|
return store.dispatch("GenerateRoutes");
|
|
})
|
|
.then((accessRoutes) => {
|
|
router.addRoutes(accessRoutes);
|
|
next({ ...to, replace: true });
|
|
})
|
|
.catch((err) => {
|
|
// console.error("单点登录失败:", err);
|
|
store.dispatch("LogOut").then(() => {
|
|
// Message.error("单点登录失败,请重试");
|
|
window.location.href =
|
|
"https://qytt.sipac.gov.cn/ecobrainportal/index.html";
|
|
});
|
|
});
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// =============================
|
|
// 📦 原有政务参数处理逻辑
|
|
// =============================
|
|
if (from.path === to.path && store.getters.token) {
|
|
next();
|
|
return;
|
|
}
|
|
|
|
if (userToken && signature && timespan && !isPathMatch("/login", to.path)) {
|
|
next({
|
|
path: "/login",
|
|
query: { userToken, signature, timespan },
|
|
replace: true,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (to.path === "/login" && userToken && signature && timespan) {
|
|
governmentGetInfo({ userToken, signature, timespan })
|
|
.then((res) => {
|
|
const token = res.data.token;
|
|
if (!token) {
|
|
const cleanPath =
|
|
window.location.pathname + window.location.hash.split("?")[0];
|
|
window.history.replaceState({}, "", cleanPath);
|
|
throw new Error("未获取到有效 token");
|
|
}
|
|
|
|
setToken(token);
|
|
localStorage.setItem("otherToken", userToken);
|
|
|
|
const cleanPath =
|
|
window.location.pathname + (window.location.hash || "");
|
|
window.history.replaceState({}, "", cleanPath);
|
|
|
|
if (store.getters.roles.length > 0) {
|
|
next("/");
|
|
return;
|
|
}
|
|
|
|
isRelogin.show = true;
|
|
store
|
|
.dispatch("GetInfo")
|
|
.then(() => {
|
|
isRelogin.show = false;
|
|
return store.dispatch("GenerateRoutes");
|
|
})
|
|
.then((accessRoutes) => {
|
|
router.addRoutes(accessRoutes);
|
|
next(to.query.redirect || "/");
|
|
})
|
|
.catch((err) => {
|
|
console.error("获取用户信息失败:", err);
|
|
return store.dispatch("LogOut").then(() => {
|
|
Message.error(err.message || "获取用户信息失败");
|
|
next("/login");
|
|
});
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
console.error("政务系统登录失败:", err);
|
|
const cleanPath =
|
|
window.location.pathname + (window.location.hash || "");
|
|
window.history.replaceState({}, "", cleanPath);
|
|
Message.error(err.message || "政务系统登录失败");
|
|
next("/login");
|
|
});
|
|
return;
|
|
}
|
|
|
|
// =============================
|
|
// 🔐 原始本地登录逻辑
|
|
// =============================
|
|
if (getToken()) {
|
|
to.meta.title && store.dispatch("settings/setTitle", to.meta.title);
|
|
/* has token*/
|
|
if (to.path === "/entLogin") {
|
|
next({ path: "/" });
|
|
NProgress.done();
|
|
} else if (whiteList.indexOf(to.path) !== -1) {
|
|
next({ path: "/" });
|
|
NProgress.done();
|
|
// next();
|
|
} 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({ ...to, replace: true });
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
store.dispatch("LogOut").then(() => {
|
|
Message.error(err);
|
|
next({ path: "/" });
|
|
});
|
|
});
|
|
} else {
|
|
next();
|
|
}
|
|
}
|
|
} else {
|
|
// 没有token
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
// 在免登录白名单,直接进入
|
|
next();
|
|
} else {
|
|
next(`/entLogin?redirect=${encodeURIComponent(to.fullPath)}`);
|
|
NProgress.done();
|
|
}
|
|
}
|
|
});
|
|
router.afterEach(() => {
|
|
NProgress.done();
|
|
});
|