parent
d2a44e5fb3
commit
6a43cde58a
@ -1,121 +1,155 @@
|
||||
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 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'
|
||||
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) => {
|
||||
return whiteList.some(pattern => isPathMatch(pattern, path))
|
||||
}
|
||||
return whiteList.some((pattern) => isPathMatch(pattern, path));
|
||||
};
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (!extractPrefix(to.path)) {
|
||||
store.commit("SET_CRUMBS", false);
|
||||
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;
|
||||
}
|
||||
|
||||
NProgress.start()
|
||||
|
||||
const { userToken, signature, timespan } = to.query
|
||||
console.log(userToken, signature, timespan, '参数')
|
||||
|
||||
|
||||
if (userToken && signature && timespan) {
|
||||
// 政务登录
|
||||
governmentGetInfo({
|
||||
userToken,
|
||||
signature,
|
||||
timespan
|
||||
}).then(res => {
|
||||
const token = res.token
|
||||
if (token) {
|
||||
setToken(token)
|
||||
localStorage.setItem('otherToken', userToken)
|
||||
|
||||
const newQuery = { ...to.query }
|
||||
delete newQuery.userToken
|
||||
delete newQuery.signature
|
||||
delete newQuery.timespan
|
||||
const newPath = to.path + '?' + new URLSearchParams(newQuery).toString()
|
||||
window.history.replaceState({}, '', newPath)
|
||||
// 如果存在政务参数,并且不在登录页,则强制跳转过去
|
||||
if (userToken && signature && timespan && to.path !== "/login") {
|
||||
next({
|
||||
path: "/login",
|
||||
query: { userToken, signature, timespan },
|
||||
replace: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
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 })
|
||||
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");
|
||||
})
|
||||
}).catch(err => {
|
||||
store.dispatch('LogOut').then(() => {
|
||||
Message.error(err)
|
||||
next({ path: '/' })
|
||||
.then((accessRoutes) => {
|
||||
router.addRoutes(accessRoutes);
|
||||
next(to.query.redirect || "/"); // 跳转到目标页或首页
|
||||
})
|
||||
})
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('政务系统登录失败:', err)
|
||||
Message.error('政务系统登录失败,请重新登录')
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
.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()
|
||||
// 原始本地登录逻辑
|
||||
const token = getToken();
|
||||
|
||||
if (token) {
|
||||
to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
|
||||
if (to.path === '/login') {
|
||||
next({ path: '/' })
|
||||
NProgress.done()
|
||||
to.meta.title && store.dispatch("settings/setTitle", to.meta.title);
|
||||
if (to.path === "/login") {
|
||||
next({ path: "/" });
|
||||
NProgress.done();
|
||||
} else if (isWhiteList(to.path)) {
|
||||
next()
|
||||
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({ path: '/' })
|
||||
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()
|
||||
next();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (isWhiteList(to.path)) {
|
||||
next()
|
||||
next();
|
||||
} else {
|
||||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
|
||||
NProgress.done()
|
||||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
router.afterEach(() => {
|
||||
NProgress.done()
|
||||
})
|
||||
NProgress.done();
|
||||
});
|
||||
|
Loading…
Reference in new issue