Commit dd6cf76f authored by 左玲玲's avatar 左玲玲 😬

token免登录

parent 884176e4
Pipeline #8436 failed with stages
in 35 minutes and 37 seconds
......@@ -121,6 +121,7 @@ export default function AccountPopover() {
if (path === "/user/login") {
doFetch({ url: "/system/logout", params: {} }).then((res) => {
if (res?.code === "0000") {
localStorage.clear();
path && navigate(path);
}
});
......
......@@ -7,6 +7,7 @@ import Snackbar from "@mui/material/Snackbar";
import * as Sentry from "@sentry/react";
import { Outlet, useModel, useNavigate, useLocation } from "@umijs/max";
import { ConfigProvider } from "antd";
import { doFetch } from "@/utils/doFetch";
import locale from "antd/locale/zh_CN";
import "dayjs/locale/zh-cn";
import ThemeProvider from "./theme";
......@@ -75,36 +76,39 @@ function App() {
// ---------- 初始化时检查 token ----------
useEffect(() => {
// 获取 URL 参数
const url = new URL(window.location.href);
const token =
url.searchParams.get('token') ||
new URLSearchParams(window.location.hash.split('?')[1] || '').get('token');
if (!localStorage.getItem("TOKENES")) {
// 获取 URL 参数
const url = new URL(window.location.href);
const token = url.searchParams.get('token') || new URLSearchParams(window.location.hash.split('?')[1] || '').get('token');
if (token) {
localStorage.setItem("TOKENES", token);
// ✅ 清理 URL 中的 token 参数
const newUrl = window.location.origin + '/#' + window.location.hash.split('#')[1];
window.history.replaceState(null, '', newUrl);
(async () => {
try {
let user = await fetchUserInfo();
if (user) {
navigate("/work", { replace: true });
} else {
if (token) {
localStorage.setItem("TOKENES", token);
// ✅ 清理 URL 中的 token 参数
const newUrl = window.location.origin + '/#' + window.location.hash.split('#')[1];
window.history.replaceState(null, '', newUrl);
(async () => {
try {
let user = await doFetch({
url: "/system/me",
});
if (user?.code==="0000") {
localStorage.setItem("ID", user?.data?.data?.id);
navigate("/work", { replace: true });
} else {
localStorage.clear();
navigate("/user/login", { replace: true });
}
} catch (err) {
localStorage.clear();
navigate("/user/login", { replace: true });
}
} catch (err) {
})();
} else {
// 没有 token,若不在登录页则跳转
if (!location.pathname.startsWith("/user")) {
localStorage.clear();
navigate("/user/login", { replace: true });
}
})();
} else {
// 没有 token,若不在登录页则跳转
if (!location.pathname.startsWith("/user")) {
localStorage.clear();
navigate("/user/login", { replace: true });
}
}
}, []);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment