Ÿ¥是否是关键词页面(keywords/xxx 或 keywords/xxx.html) let isKeywordPage = false; let keywordValue = ''; let originalHost = host; // 保存原始 host 用于后续 URL 解析 let keywordBasePath = ''; // keywords 页面的基础路径(包含关键词) (function() { let currentPath = window.location.pathname.replace(/^\/+|\/+$/g, ''); // 匹配 keywords/xxx 或 keywords/xxx.html 或 keywords/xxx-参数 let keywordMatch = currentPath.match(/^keywords\/([^\/]+)/i); if(keywordMatch) { isKeywordPage = true; let fullKeywordPath = keywordMatch[1]; // 完整的路径部分,可能是 baojie 或 baojie-LW-ST-MH 或 baojie.html // 分离关键词和筛选参数 // 如果包含 .html,先移除 let keywordWithParams = fullKeywordPath.replace(/\.html$/i, ''); // 查找第一个筛选参数的位置 // 筛选参数的特征:- 后面跟着大写字母(如 -MH, -ST, -LW, -C医院, -Y中石油) // 或者小写字母(城市代码,如 -beijing) let paramIndex = -1; for(let i = 0; i < keywordWithParams.length; i++) { if(keywordWithParams[i] === '-') { // 检查 - 后面的字符 if(i + 1 < keywordWithParams.length) { let nextChar = keywordWithParams[i + 1]; // 如果后面是大写字母或小写字母,说明是筛选参数 if((nextChar >= 'A' && nextChar <= 'Z') || (nextChar >= 'a' && nextChar <= 'z')) { paramIndex = i; break; } } } } if(paramIndex > 0) { // 有筛选参数,分离关键词和参数 keywordValue = keywordWithParams.substring(0, paramIndex); keywordBasePath = keywordValue; // 基础路径只包含关键词 } else { // 没有筛选参数,整个就是关键词 keywordValue = keywordWithParams; keywordBasePath = keywordValue; } host = "http://www.chitu5.com/keywords/" + keywordBasePath; } })(); let time = window.location.search; // /**非搜索页面链接时*/ // //console.log(url);return false; if(url.indexOf('Search') < 0 && !isKeywordPage){ if(href.indexOf('-S') > -1){ href = 'china'+href; }else{ href = href.replace('~',''); } window.location.href = host+href; return false; } /**携带参数部分url字符*/ // 对于 keywords 页面和 Search 页面,需要从完整 URL 中提取参数部分 let param_str = ''; if(isKeywordPage) { // 从 URL 中提取筛选参数部分 let pathname = window.location.pathname.replace(/^\/+|\/+$/g, ''); console.log('4.1 pathname (提取参数前):', pathname); // 移除 keywords/ 前缀 let pathWithoutPrefix = pathname.replace(/^keywords\//i, ''); console.log('4.2 pathWithoutPrefix (移除keywords/后):', pathWithoutPrefix); // 移除 .html 后缀(如果有) pathWithoutPrefix = pathWithoutPrefix.replace(/\.html$/i, ''); console.log('4.3 pathWithoutPrefix (移除.html后):', pathWithoutPrefix); // 现在需要分离关键词和参数 // 关键词通常是拼音或中文,不包含筛选参数标识 // 筛选参数以 - 开头,后面跟着: // - 大写字母(如 -MH, -ST, -LW, -C医院, -Y中石油) // - 小写字母(城市代码,如 -beijing) // 查找第一个筛选参数的位置 // 筛选参数的特征:- 后面跟着大写字母或小写字母(城市代码) let paramStartIndex = -1; for(let i = 0; i < pathWithoutPrefix.length; i++) { if(pathWithoutPrefix[i] === '-') { // 检查 - 后面的字符 if(i + 1 < pathWithoutPrefix.length) { let nextChar = pathWithoutPrefix[i + 1]; // 如果后面是大写字母或小写字母,说明是筛选参数 if((nextChar >= 'A' && nextChar <= 'Z') || (nextChar >= 'a' && nextChar <= 'z')) { paramStartIndex = i; break; } } } } console.log('4.4 paramStartIndex (参数开始位置):', paramStartIndex); if(paramStartIndex > 0) { // 找到了参数部分 param_str = pathWithoutPrefix.substring(paramStartIndex); console.log('4.5 提取的 param_str:', param_str); } else { // 没有找到参数部分 param_str = ''; console.log('4.6 未找到参数部分'); } } else { // Search 页面:从 URL 中移除 host 部分,得到参数部分 // 使用原始方法,但需要处理各种 URL 格式 let pathname = window.location.pathname.replace(/^\/+|\/+$/g, ''); // 移除 Search/ 前缀,得到参数部分(如 china-LW-ST-MH) param_str = pathname.replace(/^Search\//i, ''); // 如果 param_str 为空,说明可能是 Search/ 后面没有参数 if(!param_str) { param_str = ''; } // 移除查询参数(如果 pathname 中没有,可能 URL 中有) if(param_str.indexOf('?') >= 0) { param_str = param_str.substring(0, param_str.indexOf('?')); } } param_str = param_str.replace(time,''); // 调试日志:参数提取后 console.log('4. 提取的 param_str (移除time后):', param_str); console.log('5. isKeywordPage:', isKeywordPage); console.log('6. host:', host); /**取出参数标识*/ let identifier = href.substr(0,1); console.log('7. identifier (href第一个字符):', identifier); switch (identifier) { /**附加条件*/ case '-': /*获取条件标识 L->最近时间 S->招标类型 M->搜索模式 C->场景 Y->业主*/ let condition_ = href.substr(0,2); console.log('8. case "-": condition_ =', condition_); /*查询url是否携带该类参数有则替换否则天添加*/ var index = param_str.indexOf(condition_); console.log('9. 查找 condition_ 在 param_str 中的位置:', index); if(index >= 0){ // 找到同类型的参数,需要替换它 // 找到下一个 - 的位置,或者到字符串末尾 let index_ = param_str.indexOf('-',index+1); let endstr = index_ > 0 ? param_str.substr(index_) : ''; console.log('10. 找到同类型参数,替换前 param_str:', param_str); console.log('11. index_ (下一个-的位置):', index_); console.log('12. endstr (后续参数):', endstr); param_str = param_str.substr(0,index) + href + endstr; console.log('13. 替换后 param_str:', param_str); }else{ // 没有找到同类型的参数,追加到末尾 console.log('14. 未找到同类型参数,追加前 param_str:', param_str); param_str += href; console.log('15. 追加后 param_str:', param_str); } break; /**城市筛选*/ case '~': var key = href.substr(1); if(isKeywordPage) { // keywords 页面:地区筛选应该以 - 开头(如 -beijing) // 需要移除现有的地区筛选(如果存在),然后添加新的 // 地区代码通常是小写字母,且不是以 L/S/M/C/Y 开头 // 查找第一个 - 后面的部分,如果第一个参数是城市代码,需要替换 let firstParamMatch = param_str.match(/^-([a-z]+)/); if(firstParamMatch) { // 如果第一个参数是城市代码,替换它 param_str = '-' + key + param_str.substring(firstParamMatch[0].length); } else { // 如果没有城市代码,在开头添加 param_str = '-' + key + param_str; } } else { // Search 页面:保持原有逻辑 var index = param_str.indexOf('-'); if(index >= 0){ param_str = key + param_str.substr(index); }else{ param_str = key; } } break; /**省市筛选*/ default: console.log('16. case "default":'); if(isKeywordPage) { // keywords 页面:处理业主筛选(Y开头)和场景筛选(C开头)等 // 需要检查 href 是否以 Y 或 C 开头(业主或场景筛选) let firstChar = href.substr(0, 1); console.log('17. firstChar (href第一个字符):', firstChar); if(firstChar == 'Y' || firstChar == 'C') { // 业主或场景筛选:需要查找并替换同类型的参数 // Y 开头的参数(业主):查找 -Y 开头的参数 // C 开头的参数(场景):查找 -C 开头的参数 let pattern = new RegExp('-' + firstChar + '[^-]*'); console.log('18. 业主/场景筛选,pattern:', pattern); let matchResult = param_str.match(pattern); console.log('19. 匹配结果:', matchResult); if(matchResult) { // 如果存在同类型的参数,替换它 console.log('20. 找到同类型参数,替换前 param_str:', param_str); param_str = param_str.replace(pattern, '-' + href); console.log('21. 替换后 param_str:', param_str); } else { // 如果不存在,追加到末尾 console.log('22. 未找到同类型参数,追加前 param_str:', param_str); param_str += '-' + href; console.log('23. 追加后 param_str:', param_str); } } else { // 其他情况(可能是城市代码):需要移除现有的地区筛选(如果存在),然后添加新的 console.log('24. 其他情况(可能是城市代码)'); let firstParamMatch = param_str.match(/^-([a-z]+)/); console.log('25. firstParamMatch (城市代码匹配):', firstParamMatch); if(firstParamMatch) { // 如果第一个参数是城市代码,替换它 console.log('26. 找到城市代码,替换前 param_str:', param_str); param_str = '-' + href + param_str.substring(firstParamMatch[0].length); console.log('27. 替换后 param_str:', param_str); } else { // 如果没有城市代码,在开头添加 console.log('28. 未找到城市代码,添加前 param_str:', param_str); param_str = '-' + href + param_str; console.log('29. 添加后 param_str:', param_str); } } } else { // Search 页面:保持原有逻辑 var index = param_str.indexOf('-'); if(index > 0){ param_str = href + param_str.substr(index); }else{ param_str = href; } } } //param_str = param_str.replace(/(-LC|-SC)/,''); // 对于 keywords 页面,确保 param_str 格式正确 // keywords 页面的筛选参数应该以 - 开头(如 -LW-ST-MH) if(isKeywordPage && param_str && !param_str.match(/^-/)) { // 如果 param_str 不是以 - 开头,说明可能是城市代码或其他格式 // 对于 keywords 页面,这种情况不应该出现,但为了兼容性,加上 - 前缀 param_str = '-' + param_str; } param_str += time; // 调试日志:最终结果 console.log('30. 最终 param_str (添加time后):', param_str); console.log('31. 最终 host:', host); let finalUrl = isKeywordPage ? (host + param_str) : (host + param_str); console.log('32. 最终 URL:', finalUrl); console.log('=== 筛选参数处理结束 ==='); // 对于 keywords 页面,URL 格式应该是 keywords/关键词-参数 // 对于 Search 页面,URL 格式应该是 Search/城市-参数 if(isKeywordPage) { // keywords 页面:host 已经是 keywords/关键词,param_str 是筛选参数(如 -LW-ST-MH?page=1) window.location.href = finalUrl; } else { // Search 页面:host 是 Search/,param_str 是城市-参数(如 china-LW-ST-MH?page=1) window.location.href = finalUrl; } });