From ecbbb27f3fea6048a26a65659a5faf21d12acc69 Mon Sep 17 00:00:00 2001 From: make Date: Wed, 7 May 2025 14:57:42 +0800 Subject: [PATCH] debug --- code/public/public/js/common.js | 44 ++++++++++++++----- code/public/public/video/common.js | 29 ++++++++---- code/public/template/default/js/common.js | 29 ++++++++---- .../public/template/haozi/public/js/common.js | 29 ++++++++---- .../template/kuangyu/public/js/common.js | 29 ++++++++---- 5 files changed, 112 insertions(+), 48 deletions(-) diff --git a/code/public/public/js/common.js b/code/public/public/js/common.js index e2a1c2a..c4bf627 100644 --- a/code/public/public/js/common.js +++ b/code/public/public/js/common.js @@ -74,22 +74,42 @@ document.addEventListener("DOMContentLoaded", function () { }); function handleRedirect() { - // 获取当前路径和主机名 - const strCurrentPath = window.location.pathname || '/index.html'; // 如果路径为空,默认设置为 /index.html - const strCurrentHost = window.location.origin; + const { pathname, origin, search } = window.location; + // 规范化路径名,将多个斜杠替换为单个斜杠 + const normalizedPath = pathname.replace(/\/+/g, '/'); + 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()) { - // 移动端逻辑 - if (strCurrentPath.startsWith(strPcPath)) { - // 如果路径包含 "/pc/",去除 "/pc/" - const newPath = strCurrentPath.replace(strPcPath, '/'); // 去除 "/pc/" - window.location.href = `${strCurrentHost}${newPath}`; + // 如果第一个目录 在pc 模板里面 则删除替换成空 + if (isInPcPathArray) { + const newPath = strCurrentPath.replace(firstSegment, ''); + window.location.href = buildUrl(newPath); } + } else { - // PC 端逻辑 - if (!strCurrentPath.startsWith(strPcPath)) { - // 如果路径不包含 "/pc/",添加 "/pc/" - window.location.href = `${strCurrentHost}${strPcPath}${strCurrentPath}`; + + 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); + } } } } diff --git a/code/public/public/video/common.js b/code/public/public/video/common.js index d906b7f..f8b171a 100644 --- a/code/public/public/video/common.js +++ b/code/public/public/video/common.js @@ -100,27 +100,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); + } } } } diff --git a/code/public/template/default/js/common.js b/code/public/template/default/js/common.js index 4f9b116..428ba20 100644 --- a/code/public/template/default/js/common.js +++ b/code/public/template/default/js/common.js @@ -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); + } } } } diff --git a/code/public/template/haozi/public/js/common.js b/code/public/template/haozi/public/js/common.js index 91928de..e497419 100644 --- a/code/public/template/haozi/public/js/common.js +++ b/code/public/template/haozi/public/js/common.js @@ -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); + } } } } diff --git a/code/public/template/kuangyu/public/js/common.js b/code/public/template/kuangyu/public/js/common.js index 385d769..8088f54 100644 --- a/code/public/template/kuangyu/public/js/common.js +++ b/code/public/template/kuangyu/public/js/common.js @@ -97,27 +97,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); + } } } }