This commit is contained in:
make
2025-05-07 14:57:42 +08:00
parent 453d73cd56
commit ecbbb27f3f
5 changed files with 112 additions and 48 deletions

View File

@@ -98,27 +98,38 @@ function handleRedirect() {
// 规范化路径名,将多个斜杠替换为单个斜杠
const normalizedPath = pathname.replace(/\/+/g, '/');
const strCurrentPath = normalizedPath || '/index.html';
var strCurrentPath = normalizedPath || '/index.html';
const arrPcPathUrl = ['/pc', '/web', '/desktop', '/index', '/shouye', '/zhuomian', '/windows']; // 所有模板的 PC 目录
// 构建URL确保斜杠正确
const buildUrl = (path) => {
// 确保路径以单个斜杠开头,并移除多余斜杠
const cleanPath = `/${path.replace(/\/+/g, '/')}`.replace(/^\/+/, '/');
return `${origin}${cleanPath}${search}`;
};
// 获取当前路径的第一个目录
var firstSegment = strCurrentPath.split('/')[1] || '';
// 判断当前路径的第一个目录是否在其它模板的 PC 目录中
const isInPcPathArray = arrPcPathUrl.some(pcPath => firstSegment.startsWith(pcPath.replace('/', '')));
if (isMobile()) {
// 移动端逻辑将PC路径重定向到移动端路径
if (strCurrentPath.startsWith(strPcPath)) {
const newPath = strCurrentPath.replace(strPcPath, '') || '/';
// 如果第一个目录 在pc 模板里面 则删除替换成空
if (isInPcPathArray) {
const newPath = strCurrentPath.replace(firstSegment, '');
window.location.href = buildUrl(newPath);
}
} else {
// PC端逻辑将非PC路径重定向到PC路径
if (!strCurrentPath.startsWith(strPcPath)) {
// 确保 PC 首页以斜杠结尾
if (!isInPcPathArray) {
const newPath = strCurrentPath === '/' ? `${strPcPath}/` : `${strPcPath}${strCurrentPath}`;
window.location.href = buildUrl(newPath);
}else{
// 如果包含,则先判断是不是当前模板的 url如果不是 则替换成当前模板的 url
if (firstSegment != strPcPath.replace('/', '')) {
strCurrentPath = strCurrentPath.replace(firstSegment,strPcPath.replace('/', '')) ;
window.location.href = buildUrl(strCurrentPath);
}
}
}
}