推拿小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.8 KiB

  1. // H5 环境修复
  2. export function fixH5Environment() {
  3. // #ifdef H5
  4. try {
  5. // 修复路由 meta 属性访问问题
  6. const originalGetCurrentPages = getCurrentPages;
  7. window.getCurrentPages = function() {
  8. try {
  9. const pages = originalGetCurrentPages();
  10. // 确保页面对象有基本的路由信息
  11. if (pages && pages.length > 0) {
  12. pages.forEach(page => {
  13. if (page && !page.route) {
  14. page.route = page.$page?.fullPath || '/';
  15. }
  16. if (page && page.route && !page.$page?.meta) {
  17. page.$page = page.$page || {};
  18. page.$page.meta = page.$page.meta || {};
  19. }
  20. });
  21. }
  22. return pages;
  23. } catch(e) {
  24. console.warn('getCurrentPages 修复失败:', e);
  25. return [];
  26. }
  27. };
  28. // 修复 History API
  29. const originalReplaceState = window.history.replaceState;
  30. window.history.replaceState = function(state, title, url) {
  31. try {
  32. // 确保 URL 格式正确
  33. if (url && typeof url === 'string') {
  34. // 修复格式不正确的 URL
  35. if (url.startsWith('https:/#/')) {
  36. url = url.replace('https:/#/', window.location.origin + '/#/');
  37. }
  38. // 如果 URL 不包含完整的域名,添加基础路径
  39. if (url.startsWith('#/')) {
  40. url = window.location.origin + window.location.pathname + url;
  41. }
  42. }
  43. return originalReplaceState.call(this, state, title, url);
  44. } catch(e) {
  45. console.warn('History.replaceState 修复失败:', e);
  46. return null;
  47. }
  48. };
  49. // 修复路由跳转相关问题
  50. const originalNavigateTo = uni.navigateTo;
  51. uni.navigateTo = function(options) {
  52. try {
  53. return originalNavigateTo(options);
  54. } catch(e) {
  55. console.warn('uni.navigateTo 失败:', e);
  56. // 降级处理
  57. window.location.href = window.location.origin + window.location.pathname + '#' + options.url;
  58. }
  59. };
  60. const originalReLaunch = uni.reLaunch;
  61. uni.reLaunch = function(options) {
  62. try {
  63. return originalReLaunch(options);
  64. } catch(e) {
  65. console.warn('uni.reLaunch 失败:', e);
  66. // 降级处理
  67. window.location.href = window.location.origin + window.location.pathname + '#' + options.url;
  68. }
  69. };
  70. console.log('H5 环境修复完成');
  71. } catch(e) {
  72. console.warn('H5 环境修复失败:', e);
  73. }
  74. // #endif
  75. }
  76. // 修复 URL 参数解析
  77. export function fixUrlParams() {
  78. // #ifdef H5
  79. try {
  80. function GetQueryString(name) {
  81. const url = window.location.href;
  82. try {
  83. const cs = url.split('?')[1];
  84. if (cs) {
  85. const cs_arr = cs.split('&');
  86. for (let i = 0; i < cs_arr.length; i++) {
  87. if (cs_arr[i].split('=')[0] === name) {
  88. sessionStorage.setItem('vid', cs_arr[i].split('=')[1]);
  89. }
  90. }
  91. }
  92. } catch(e) {
  93. console.warn('URL参数解析失败:', e);
  94. }
  95. }
  96. GetQueryString('vid');
  97. } catch(e) {
  98. console.warn('URL参数修复失败:', e);
  99. }
  100. // #endif
  101. }