Commit 884176e4 authored by 左玲玲's avatar 左玲玲 😬

如果路径带token那就直接进入系统,否则跳转到登录页

parent 48c3bc17
Pipeline #8428 failed with stages
in 33 minutes and 53 seconds
......@@ -17,6 +17,7 @@ const loginPath = "/user/login";
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
export async function getInitialState() {
let token = localStorage.getItem('TOKENES');
const fetchUserInfo = async () => {
try {
const msg = await doFetch({
......@@ -31,7 +32,7 @@ export async function getInitialState() {
};
// 如果不是登录页面,执行
const { location } = history;
if (!location.pathname.includes("user")) {
if (!location.pathname.includes("user") && token) {
const currentUser = await fetchUserInfo();
localStorage.setItem("ID", currentUser?.id);
return {
......
import React, { useEffect } from "react";
import { AlertTitle } from "@mui/material";
import MuiAlert from "@mui/material/Alert";
import CssBaseline from "@mui/material/CssBaseline";
import Slide from "@mui/material/Slide";
import Snackbar from "@mui/material/Snackbar";
import * as Sentry from "@sentry/react";
import { Outlet, useModel } from "@umijs/max";
import { Outlet, useModel, useNavigate, useLocation } from "@umijs/max";
import { ConfigProvider } from "antd";
import locale from "antd/locale/zh_CN";
import "dayjs/locale/zh-cn";
import React from "react";
import ThemeProvider from "./theme";
// 自定义主题
......@@ -60,6 +60,9 @@ const Alert = React.forwardRef(function Alert(props, ref) {
});
function App() {
const { initialState: { message, fetchUserInfo }, setInitialState } = useModel("@@initialState");
const navigate = useNavigate();
const location = useLocation();
const handleClose = (event) => {
setInitialState((s) => ({
...s,
......@@ -70,11 +73,41 @@ function App() {
}));
};
const {
initialState: { message },
setInitialState,
} = useModel("@@initialState");
// ---------- 初始化时检查 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 (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 {
localStorage.clear();
navigate("/user/login", { replace: true });
}
} catch (err) {
localStorage.clear();
navigate("/user/login", { replace: true });
}
})();
} else {
// 没有 token,若不在登录页则跳转
if (!location.pathname.startsWith("/user")) {
localStorage.clear();
navigate("/user/login", { replace: true });
}
}
}, []);
return (
<ConfigProvider locale={locale}>
<ThemeProvider>
......
......@@ -112,9 +112,6 @@ export default function Welcome() {
}
});
}, [data, currentUser?.type, menuNum]);
console.log(navConfigs, currentUser);
return (
<Box p={2}>
<Typography component={"h1"} align="center" fontSize={24}>
......
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