debug
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user