Commit b605c21b546389597f6470b08e2cc33863fef8a5
1 parent
34cd941c
feat: 重构多语言重定向逻辑以提高可维护性
- 将硬编码的语言映射提取为独立的 `getLanguageFolder` 函数 - 将 URL 构建逻辑封装到 `getRedirectUrl` 函数中,支持更灵活的路径处理 - 使用 `window.location.replace` 避免浏览器历史记录问题 - 添加 API 请求失败时的降级处理,默认重定向至英文页面 - 改进路径替换逻辑,避免重复斜杠并处理更多边缘情况
Showing
1 changed file
with
55 additions
and
22 deletions
app/pages/index.html
| ... | ... | @@ -7,30 +7,63 @@ |
| 7 | 7 | <script src="../../../node_modules/jquery/dist/jquery.js"></script> |
| 8 | 8 | </head> |
| 9 | 9 | <body> |
| 10 | - | |
| 10 | + | |
| 11 | 11 | </body> |
| 12 | 12 | <script> |
| 13 | - let urlStr = window.location.href | |
| 14 | - console.log('地址',urlStr) | |
| 15 | - $.get('http://www.ccdcdf.com:6080/api/index/index',function(res){ | |
| 16 | - console.log('返回国家代码',res) | |
| 17 | - if(res.data.code == 'en'){ | |
| 18 | - let str = urlStr.replace('pages/','pages/English/') | |
| 19 | - window.location.href = str | |
| 20 | - console.log('更新后地址',str) | |
| 21 | - }else if(res.data.code == 'ru'){ | |
| 22 | - let str = urlStr.replace('pages/','pages/Russian/') | |
| 23 | - window.location.href = str | |
| 24 | - }else if(res.data.code == 'es'){ | |
| 25 | - let str = urlStr.replace('pages/','pages/Spain/') | |
| 26 | - window.location.href = str | |
| 27 | - }else if(res.data.code == 'cn'){ | |
| 28 | - let str = urlStr.replace('pages/','pages/Chinese/') | |
| 29 | - window.location.href = str | |
| 30 | - }else{ | |
| 31 | - let str = urlStr.replace('pages/','pages/English/') | |
| 32 | - window.location.href = str | |
| 13 | + function getLanguageFolder(code) { | |
| 14 | + if (code === 'ru') { | |
| 15 | + return 'Russian'; | |
| 33 | 16 | } |
| 34 | - }) | |
| 17 | + if (code === 'es') { | |
| 18 | + return 'Spain'; | |
| 19 | + } | |
| 20 | + if (code === 'cn') { | |
| 21 | + return 'Chinese'; | |
| 22 | + } | |
| 23 | + return 'English'; | |
| 24 | + } | |
| 25 | + | |
| 26 | + function getRedirectUrl(languageFolder) { | |
| 27 | + var currentUrl = new URL(window.location.href); | |
| 28 | + var pathname = currentUrl.pathname || '/'; | |
| 29 | + var pagesMarker = '/app/pages/'; | |
| 30 | + var targetPath = ''; | |
| 31 | + | |
| 32 | + if (pathname.indexOf(pagesMarker) > -1) { | |
| 33 | + targetPath = pathname.replace( | |
| 34 | + /\/app\/pages(?:\/index\.html|\/)?$/i, | |
| 35 | + '/app/pages/' + languageFolder + '/index.html' | |
| 36 | + ); | |
| 37 | + | |
| 38 | + if (targetPath === pathname) { | |
| 39 | + targetPath = pathname.split(pagesMarker)[0] + pagesMarker + languageFolder + '/index.html'; | |
| 40 | + } | |
| 41 | + } else { | |
| 42 | + var basePath = pathname.replace(/\/?index\.html$/i, '').replace(/\/$/, ''); | |
| 43 | + | |
| 44 | + if (!basePath || basePath === '/') { | |
| 45 | + basePath = '/pc'; | |
| 46 | + } | |
| 47 | + | |
| 48 | + targetPath = basePath + '/app/pages/' + languageFolder + '/index.html'; | |
| 49 | + } | |
| 50 | + | |
| 51 | + currentUrl.pathname = targetPath.replace(/\/{2,}/g, '/'); | |
| 52 | + return currentUrl.toString(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + function redirectToLanguage(code) { | |
| 56 | + var targetUrl = getRedirectUrl(getLanguageFolder(code)); | |
| 57 | + | |
| 58 | + if (targetUrl !== window.location.href) { | |
| 59 | + window.location.replace(targetUrl); | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + $.get('http://www.ccdcdf.com:6080/api/index/index', function(res) { | |
| 64 | + redirectToLanguage(res && res.data ? res.data.code : ''); | |
| 65 | + }).fail(function() { | |
| 66 | + redirectToLanguage(''); | |
| 67 | + }); | |
| 35 | 68 | </script> |
| 36 | 69 | </html> |
| 37 | 70 | \ No newline at end of file | ... | ... |