redirect.html 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Redirecting...</title>
  7. <style>
  8. body {
  9. margin: 0;
  10. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  11. background: #f5f5f5;
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. min-height: 100vh;
  16. }
  17. .error-box {
  18. display: none;
  19. background: #fff;
  20. padding: 40px 48px;
  21. border-radius: 12px;
  22. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  23. text-align: center;
  24. color: #333;
  25. font-size: 16px;
  26. line-height: 1.6;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="error" class="error-box"></div>
  32. <script>
  33. (function () {
  34. var redirectMap = %VITE_REDIRECT_MAP%;
  35. // hash 为 target key,支持 /redirect.html#demo?credit_code=123 和 /redirect.html?from=wx#demo 两种形式
  36. var rawHash = location.hash;
  37. var hashBody = rawHash ? rawHash.slice(1) : ""; // "demo?credit_code=123" 或 "demo"
  38. var hashParts = hashBody.split("?");
  39. var targetKey = hashParts[0];
  40. // hash 中 ? 之后的部分作为额外 querystring
  41. var hashQuery = hashParts.length > 1 ? "?" + hashParts.slice(1).join("?") : "";
  42. if (!targetKey) {
  43. showError("\u672a\u6307\u5b9a\u8df3\u8f6c\u76ee\u6807\uff0c\u8bf7\u901a\u8fc7 hash \u6307\u5b9a target");
  44. return;
  45. }
  46. var targetUrl = redirectMap[targetKey];
  47. if (targetUrl) {
  48. // .env 中 # 为注释符,URL 中的 # 使用 %23 编码,此处解码还原
  49. targetUrl = targetUrl.replace(/%23/g, "#");
  50. } else {
  51. showError("\u672a\u627e\u5230\u8df3\u8f6c\u76ee\u6807\uff1a" + targetKey);
  52. return;
  53. }
  54. // 合并 location.search 和 hash 中的 querystring,原样透传
  55. var queryString;
  56. if (location.search && hashQuery) {
  57. queryString = location.search + "&" + hashQuery.slice(1);
  58. } else {
  59. queryString = location.search || hashQuery;
  60. }
  61. // querystring 直接追加到目标 URL 末尾
  62. var finalUrl = targetUrl + queryString;
  63. location.replace(finalUrl);
  64. function showError(msg) {
  65. var el = document.getElementById("error");
  66. el.textContent = msg;
  67. el.style.display = "block";
  68. }
  69. })();
  70. </script>
  71. </body>
  72. </html>