AHMemberManager/src/utils/handleRedirect.ts
2025-05-09 15:08:59 +08:00

105 lines
4.0 KiB
TypeScript

import { history } from 'umi';
/**
* 重定向的方法
* @description 如果是登录页, url上有redirect参数则跳转到redirect参数, 同时要考虑redirect的有效性,
* 没有就跳第一个有效路由; 非登录页, 看该页面的路由是否有效, 有效就跳转,
* 无效就看是否有子路由, 有就跳它的第一个子路由, 也就是看这个路由是不是有子路由, 是在Menu中可展开的形式,
* 没有也跳, 此时就交由umi处理404的情况
* @param isLoginPage 是否是登录页
* @param indexAllMenuItemByPath IndexAllMenuItemByKey<'path'>
* @param indexValidMenuItemByPath IndexValidMenuItemByPath
* @returns Promise<boolean>
*/
const handleRedirect = (
isLoginPage: boolean,
indexAllMenuItemByPath: IndexAllMenuItemByKey<'path'>,
indexValidMenuItemByPath: IndexValidMenuItemByPath,
): Promise<boolean> => (
new Promise((resolve) => {
let routePath = ''
// Object.keys(indexValidMenuItemByPath)[0];
if (isLoginPage) {
const queryString = window.location.search;
if (queryString) {
const matchedRes = queryString.match(/redirect=(.*)/);
console.log('isLoginPage', isLoginPage);
console.log('indexAllMenuItemByPath', indexAllMenuItemByPath);
console.log('indexValidMenuItemByPath', indexValidMenuItemByPath);
console.log('queryString', queryString);
console.log('matchedRes', matchedRes);
if (matchedRes) {
//还要考虑redirect参数是否有效
let decodeRedirect = decodeURIComponent(matchedRes[1]);
if (decodeRedirect.indexOf('/AHMemberManager') !== -1) {
decodeRedirect = decodeRedirect.split('/AHMemberManager')[1]
}
// 处理可能存在的路径问题,确保路径格式正确
// 移除可能的重复前缀
if (decodeRedirect.startsWith('/') && decodeRedirect.indexOf('/', 1) !== -1) {
const firstSlashAfterRoot = decodeRedirect.indexOf('/', 1);
const possiblePrefix = decodeRedirect.substring(0, firstSlashAfterRoot);
// 检查是否有重复的路径前缀
if (decodeRedirect.indexOf(possiblePrefix, firstSlashAfterRoot) === firstSlashAfterRoot) {
decodeRedirect = decodeRedirect.substring(firstSlashAfterRoot);
}
}
// 避免重定向到登录页面本身
if (decodeRedirect === '/user/login' || decodeRedirect.startsWith('/user/login?')) {
// 如果重定向目标是登录页面,则使用默认路由
routePath = Object.keys(indexValidMenuItemByPath)[0];
}
//有效: 跳转
else if (indexValidMenuItemByPath[decodeRedirect]) {
routePath = decodeRedirect;
} else if (indexAllMenuItemByPath[decodeRedirect]) {
//无效
//有子路由: 跳子路由
routePath = indexAllMenuItemByPath[decodeRedirect].redirect;
} else {
//无子路由: 还是要跳, 此时就是交由umi处理404的情况了
if (decodeRedirect === '/AHMemberManager/') {
routePath = ''
} else {
routePath = decodeRedirect;
}
}
}
}
} else {
let {
location: { search, pathname },
} = window;
if (pathname.indexOf('/AHMemberManager') !== -1) {
pathname = pathname.split('/AHMemberManager')[1]
}
//考虑url上有查询字符串参数的情况
//有效
if (indexValidMenuItemByPath[pathname]) {
routePath = `${pathname}${search}`;
} else if (indexAllMenuItemByPath[pathname]) {
//无效
//有子路由: 跳子路由
routePath = `${indexAllMenuItemByPath[pathname].redirect}${search}`;
} else {
//无子路由: 也跳, 此时交由umi处理404的情况
routePath = `${pathname}${search}`;
}
}
history.push(routePath);
return resolve(true);
})
);
export default handleRedirect;