bug修复
Some checks failed
Node CI / build (14.x, macOS-latest) (push) Has been cancelled
Node CI / build (14.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (14.x, windows-latest) (push) Has been cancelled
Node CI / build (16.x, macOS-latest) (push) Has been cancelled
Node CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (16.x, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
coverage CI / build (push) Has been cancelled
Node pnpm CI / build (16.x, macOS-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, windows-latest) (push) Has been cancelled
Some checks failed
Node CI / build (14.x, macOS-latest) (push) Has been cancelled
Node CI / build (14.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (14.x, windows-latest) (push) Has been cancelled
Node CI / build (16.x, macOS-latest) (push) Has been cancelled
Node CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (16.x, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
coverage CI / build (push) Has been cancelled
Node pnpm CI / build (16.x, macOS-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, windows-latest) (push) Has been cancelled
This commit is contained in:
86
src/app.tsx
86
src/app.tsx
@@ -6,6 +6,10 @@ import { history } from '@umijs/max';
|
||||
import defaultSettings from '../config/defaultSettings';
|
||||
import { errorConfig } from './requestErrorConfig';
|
||||
import { clearSessionToken, getAccessToken, getRefreshToken, getTokenExpireTime } from './access';
|
||||
import {
|
||||
exchangeThirdPartyCredential,
|
||||
isThirdPartyTransitionPage,
|
||||
} from './utils/thirdPartyLogin';
|
||||
import {
|
||||
getRemoteMenu,
|
||||
getRoutersInfo,
|
||||
@@ -69,7 +73,17 @@ export async function getInitialState(): Promise<{
|
||||
};
|
||||
// 如果不是登录页面,执行
|
||||
const { location } = history;
|
||||
if (location.pathname !== PageEnum.LOGIN) {
|
||||
// 排除登录页面、过渡页面和求职者页面(考虑基础路径 /shihezi/)
|
||||
const isTransitionPage = location.pathname === '/' ||
|
||||
location.pathname === '/login-tow' ||
|
||||
location.pathname === '/shihezi/' ||
|
||||
location.pathname === '/shihezi/login-tow';
|
||||
|
||||
// 排除求职者相关页面
|
||||
const isJobPortalPage = location.pathname.startsWith('/job-portal') ||
|
||||
location.pathname.startsWith('/shihezi/job-portal');
|
||||
|
||||
if (location.pathname !== PageEnum.LOGIN && !isTransitionPage && !isJobPortalPage) {
|
||||
const currentUser = await fetchUserInfo();
|
||||
return {
|
||||
fetchUserInfo,
|
||||
@@ -91,9 +105,9 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
|
||||
avatarProps: {
|
||||
src: initialState?.currentUser?.avatar,
|
||||
title: <AvatarName />,
|
||||
render: (_, avatarChildren) => {
|
||||
return <AvatarDropdown menu="True">{avatarChildren}</AvatarDropdown>;
|
||||
},
|
||||
render: (_, avatarChildren) => {
|
||||
return <AvatarDropdown menu>{avatarChildren}</AvatarDropdown>;
|
||||
},
|
||||
},
|
||||
waterMarkProps: {
|
||||
// content: initialState?.currentUser?.nickName,
|
||||
@@ -115,8 +129,17 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
|
||||
footerRender: () => <Footer />,
|
||||
onPageChange: () => {
|
||||
const { location } = history;
|
||||
// 如果没有登录,重定向到 login
|
||||
if (!initialState?.currentUser && location.pathname !== PageEnum.LOGIN) {
|
||||
// 如果没有登录,重定向到 login,但排除过渡页面和求职者页面(考虑基础路径 /shihezi/)
|
||||
const isTransitionPage = location.pathname === '/' ||
|
||||
location.pathname === '/login-tow' ||
|
||||
location.pathname === '/shihezi/' ||
|
||||
location.pathname === '/shihezi/login-tow';
|
||||
|
||||
// 排除求职者相关页面
|
||||
const isJobPortalPage = location.pathname.startsWith('/job-portal') ||
|
||||
location.pathname.startsWith('/shihezi/job-portal');
|
||||
|
||||
if (!initialState?.currentUser && location.pathname !== PageEnum.LOGIN && !isTransitionPage && !isJobPortalPage) {
|
||||
history.push(PageEnum.LOGIN);
|
||||
}
|
||||
},
|
||||
@@ -176,12 +199,23 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
|
||||
};
|
||||
};
|
||||
|
||||
export async function onRouteChange({ clientRoutes, location }) {
|
||||
export async function onRouteChange({ clientRoutes, location }: { clientRoutes: any; location: any }) {
|
||||
const menus = getRemoteMenu();
|
||||
// console.log('onRouteChange', clientRoutes, location, menus);
|
||||
if (menus === null && location.pathname !== PageEnum.LOGIN) {
|
||||
console.log('refresh');
|
||||
// history.go(0);
|
||||
|
||||
// 如果路由信息未加载,尝试重新获取路由信息
|
||||
const token = getAccessToken();
|
||||
if (menus === null && location.pathname !== PageEnum.LOGIN && token) {
|
||||
console.log('检测到路由信息未加载,正在重新获取路由信息...');
|
||||
|
||||
// 重新获取路由信息,而不是刷新页面
|
||||
try {
|
||||
const routers = await getRoutersInfo();
|
||||
setRemoteMenu(routers);
|
||||
console.log('路由信息重新获取成功');
|
||||
} catch (error) {
|
||||
console.error('重新获取路由信息失败:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,24 +223,40 @@ export async function onRouteChange({ clientRoutes, location }) {
|
||||
// console.log('patchRoutes', routes, routeComponents);
|
||||
// }
|
||||
|
||||
export async function patchClientRoutes({ routes }) {
|
||||
export async function patchClientRoutes({ routes }: { routes: any }) {
|
||||
// console.log('patchClientRoutes', routes);
|
||||
patchRouteWithRemoteMenus(routes);
|
||||
}
|
||||
|
||||
export async function render(oldRender: () => void) {
|
||||
console.log('render get routers', oldRender);
|
||||
const { pathname, search } = window.location;
|
||||
const params = new URLSearchParams(search);
|
||||
const code = params.get('code');
|
||||
const tokenParam = params.get('token');
|
||||
|
||||
// 过渡页带 code/token 时,先完成 SSO 换票再拉路由,避免旧 token 导致 getRouters 401
|
||||
if (isThirdPartyTransitionPage(pathname) && (code || tokenParam)) {
|
||||
clearSessionToken();
|
||||
const ok = await exchangeThirdPartyCredential({ code, token: tokenParam });
|
||||
if (!ok) {
|
||||
oldRender();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const token = getAccessToken();
|
||||
if (!token || token?.length === 0) {
|
||||
if (!token || token.length === 0) {
|
||||
oldRender();
|
||||
return;
|
||||
}
|
||||
await getRoutersInfo().then((res) => {
|
||||
console.log('render get routers', 123);
|
||||
|
||||
setRemoteMenu(res);
|
||||
oldRender();
|
||||
});
|
||||
try {
|
||||
const routers = await getRoutersInfo();
|
||||
setRemoteMenu(routers);
|
||||
} catch (error) {
|
||||
console.error('获取路由失败:', error);
|
||||
}
|
||||
oldRender();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user