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