parent
d2a44e5fb3
commit
6a43cde58a
@ -1,121 +1,155 @@
|
|||||||
import router from './router'
|
import router from "./router";
|
||||||
import store from './store'
|
import store from "./store";
|
||||||
import { Message } from 'element-ui'
|
import { Message } from "element-ui";
|
||||||
import NProgress from 'nprogress'
|
import NProgress from "nprogress";
|
||||||
import 'nprogress/nprogress.css'
|
import "nprogress/nprogress.css";
|
||||||
import { getToken, setToken } from '@/utils/auth'
|
import { getToken, setToken } from "@/utils/auth";
|
||||||
import { isPathMatch } from '@/utils/validate'
|
import { isPathMatch } from "@/utils/validate";
|
||||||
import { isRelogin } from '@/utils/request'
|
import { isRelogin } from "@/utils/request";
|
||||||
import { extractPrefix } from '@/utils/common'
|
import { extractPrefix } from "@/utils/common";
|
||||||
|
|
||||||
// 政务登录接口
|
// 政务登录接口
|
||||||
import { governmentGetInfo } from '@/api/login/index'
|
import { governmentGetInfo } from "@/api/login/index";
|
||||||
|
|
||||||
NProgress.configure({ showSpinner: false })
|
NProgress.configure({ showSpinner: false });
|
||||||
|
|
||||||
const whiteList = ['/login', '/register']
|
const whiteList = ["/login", "/register"];
|
||||||
|
|
||||||
const isWhiteList = (path) => {
|
const isWhiteList = (path) => {
|
||||||
return whiteList.some(pattern => isPathMatch(pattern, path))
|
return whiteList.some((pattern) => isPathMatch(pattern, path));
|
||||||
}
|
};
|
||||||
|
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
if(!extractPrefix(to.path)){
|
if(!extractPrefix(to.path)){
|
||||||
store.commit("SET_CRUMBS",false);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
NProgress.start()
|
// 如果存在政务参数,并且不在登录页,则强制跳转过去
|
||||||
|
if (userToken && signature && timespan && to.path !== "/login") {
|
||||||
|
next({
|
||||||
|
path: "/login",
|
||||||
|
query: { userToken, signature, timespan },
|
||||||
|
replace: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const { userToken, signature, timespan } = to.query
|
if (to.path === "/login" && userToken && signature && timespan) {
|
||||||
console.log(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);
|
||||||
|
|
||||||
if (userToken && signature && timespan) {
|
// 清除 URL 参数
|
||||||
// 政务登录
|
const cleanPath =
|
||||||
governmentGetInfo({
|
window.location.pathname + (window.location.hash || "");
|
||||||
userToken,
|
window.history.replaceState({}, "", cleanPath);
|
||||||
signature,
|
|
||||||
timespan
|
|
||||||
}).then(res => {
|
|
||||||
const token = res.token
|
|
||||||
if (token) {
|
|
||||||
setToken(token)
|
|
||||||
localStorage.setItem('otherToken', userToken)
|
|
||||||
|
|
||||||
const newQuery = { ...to.query }
|
// 如果已有角色信息,直接跳转
|
||||||
delete newQuery.userToken
|
if (store.getters.roles.length > 0) {
|
||||||
delete newQuery.signature
|
next("/"); // 或者 next(to.query.redirect || "/")
|
||||||
delete newQuery.timespan
|
return;
|
||||||
const newPath = to.path + '?' + new URLSearchParams(newQuery).toString()
|
}
|
||||||
window.history.replaceState({}, '', newPath)
|
|
||||||
|
|
||||||
if (store.getters.roles.length === 0) {
|
// 否则获取用户信息并加载路由
|
||||||
isRelogin.show = true
|
isRelogin.show = true;
|
||||||
store.dispatch('GetInfo').then(() => {
|
store
|
||||||
isRelogin.show = false
|
.dispatch("GetInfo")
|
||||||
store.dispatch('GenerateRoutes').then(accessRoutes => {
|
.then(() => {
|
||||||
router.addRoutes(accessRoutes)
|
isRelogin.show = false;
|
||||||
next({ ...to, replace: true })
|
return store.dispatch("GenerateRoutes");
|
||||||
})
|
})
|
||||||
}).catch(err => {
|
.then((accessRoutes) => {
|
||||||
store.dispatch('LogOut').then(() => {
|
router.addRoutes(accessRoutes);
|
||||||
Message.error(err)
|
next(to.query.redirect || "/"); // 跳转到目标页或首页
|
||||||
next({ path: '/' })
|
|
||||||
})
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error("获取用户信息失败:", err);
|
||||||
|
return store.dispatch("LogOut").then(() => {
|
||||||
|
Message.error(err.message || "获取用户信息失败");
|
||||||
|
next("/login");
|
||||||
|
});
|
||||||
|
});
|
||||||
})
|
})
|
||||||
} else {
|
.catch((err) => {
|
||||||
next()
|
console.error("政务系统登录失败:", err);
|
||||||
}
|
// 清除 URL 参数,避免循环
|
||||||
}
|
const cleanPath =
|
||||||
}).catch(err => {
|
window.location.pathname + (window.location.hash || "");
|
||||||
console.error('政务系统登录失败:', err)
|
window.history.replaceState({}, "", cleanPath);
|
||||||
Message.error('政务系统登录失败,请重新登录')
|
Message.error(err.message || "政务系统登录失败");
|
||||||
})
|
next("/login");
|
||||||
|
});
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 原始本地登录逻辑
|
||||||
|
const token = getToken();
|
||||||
//原来的登录逻辑
|
|
||||||
const token = getToken()
|
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
|
to.meta.title && store.dispatch("settings/setTitle", to.meta.title);
|
||||||
if (to.path === '/login') {
|
if (to.path === "/login") {
|
||||||
next({ path: '/' })
|
next({ path: "/" });
|
||||||
NProgress.done()
|
NProgress.done();
|
||||||
} else if (isWhiteList(to.path)) {
|
} else if (isWhiteList(to.path)) {
|
||||||
next()
|
next();
|
||||||
} else {
|
} else {
|
||||||
if (store.getters.roles.length === 0) {
|
if (store.getters.roles.length === 0) {
|
||||||
isRelogin.show = true
|
isRelogin.show = true;
|
||||||
store.dispatch('GetInfo').then(() => {
|
store
|
||||||
isRelogin.show = false
|
.dispatch("GetInfo")
|
||||||
store.dispatch('GenerateRoutes').then(accessRoutes => {
|
.then(() => {
|
||||||
router.addRoutes(accessRoutes)
|
isRelogin.show = false;
|
||||||
next({ ...to, replace: true })
|
store.dispatch("GenerateRoutes").then((accessRoutes) => {
|
||||||
})
|
router.addRoutes(accessRoutes);
|
||||||
}).catch(err => {
|
next({ ...to, replace: true });
|
||||||
store.dispatch('LogOut').then(() => {
|
});
|
||||||
Message.error(err)
|
|
||||||
next({ path: '/' })
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
store.dispatch("LogOut").then(() => {
|
||||||
|
Message.error(err);
|
||||||
|
next("/");
|
||||||
|
});
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
next()
|
next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (isWhiteList(to.path)) {
|
if (isWhiteList(to.path)) {
|
||||||
next()
|
next();
|
||||||
} else {
|
} else {
|
||||||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
|
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||||||
NProgress.done()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
router.afterEach(() => {
|
router.afterEach(() => {
|
||||||
NProgress.done()
|
NProgress.done();
|
||||||
})
|
});
|
||||||
|
Loading…
Reference in new issue