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", "/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(); // 提取所有 URL 参数(包括 hash 后面的部分) 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"); console.log(userToken, signature, timespan, "参数"); // 如果已经处理过一次登录逻辑,不再重复执行 if (from.path === to.path && store.getters.token) { next(); return; } // 如果存在政务参数,并且不在登录页,则强制跳转过去 if (userToken && signature && timespan && to.path !== "/login") { 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) { // 清除 URL 参数,避免循环 const cleanPath = window.location.pathname + window.location.hash.split("?")[0]; window.history.replaceState({}, "", cleanPath); throw new Error("未获取到有效 token"); } setToken(token); localStorage.setItem("otherToken", userToken); // 清除 URL 参数 const cleanPath = window.location.pathname + (window.location.hash || ""); window.history.replaceState({}, "", cleanPath); // 如果已有角色信息,直接跳转 if (store.getters.roles.length > 0) { next("/"); // 或者 next(to.query.redirect || "/") 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); // 清除 URL 参数,避免循环 const cleanPath = window.location.pathname + (window.location.hash || ""); window.history.replaceState({}, "", cleanPath); Message.error(err.message || "政务系统登录失败"); next("/login"); }); return; } // 原始本地登录逻辑 const token = getToken(); if (token) { to.meta.title && store.dispatch("settings/setTitle", to.meta.title); if (to.path === "/login") { next({ path: "/" }); NProgress.done(); } else if (isWhiteList(to.path)) { next(); } else { if (store.getters.roles.length === 0) { isRelogin.show = true; store .dispatch("GetInfo") .then(() => { isRelogin.show = false; store.dispatch("GenerateRoutes").then((accessRoutes) => { router.addRoutes(accessRoutes); next({ ...to, replace: true }); }); }) .catch((err) => { store.dispatch("LogOut").then(() => { Message.error(err); next("/"); }); }); } else { next(); } } } else { if (isWhiteList(to.path)) { next(); } else { next(`/login?redirect=${encodeURIComponent(to.fullPath)}`); } } }); router.afterEach(() => { NProgress.done(); });