|
|
- // H5 路由和环境修复
- // #ifdef H5
-
- // 存储原始方法
- let originalGetCurrentPages;
- let originalHistoryReplaceState;
- let isFixed = false;
-
- // 修复H5环境路由问题的主函数
- export function fixH5Router() {
- if (isFixed) return;
-
- try {
- // 1. 修复 getCurrentPages 方法
- if (typeof getCurrentPages === 'function') {
- originalGetCurrentPages = getCurrentPages;
- window.getCurrentPages = function() {
- try {
- const pages = originalGetCurrentPages();
- if (pages && Array.isArray(pages)) {
- // 确保每个页面对象有必要的属性
- pages.forEach((page, index) => {
- if (!page.route) {
- page.route = page.$page?.fullPath || `page_${index}`;
- }
- if (!page.options) {
- page.options = {};
- }
- // 确保页面有 meta 属性
- if (page.$page && !page.$page.meta) {
- page.$page.meta = {};
- }
- });
- }
- return pages || [];
- } catch(e) {
- console.warn('getCurrentPages 执行失败:', e);
- return [{
- route: '/',
- options: {},
- $page: { meta: {} }
- }];
- }
- };
- }
-
- // 2. 修复 History API
- if (window.history && window.history.replaceState) {
- originalHistoryReplaceState = window.history.replaceState;
- window.history.replaceState = function(state, title, url) {
- try {
- // 修复 URL 格式问题
- if (url && typeof url === 'string') {
- // 处理错误的 URL 格式
- if (url.includes('https:/#/')) {
- url = url.replace(/https:\/+#\//, window.location.origin + '/#/');
- }
- // 确保相对路径的正确性
- if (url.startsWith('#/')) {
- url = window.location.origin + window.location.pathname + url;
- }
- // 处理双斜杠问题
- url = url.replace(/([^:]\/)\/+/g, '$1');
- }
- return originalHistoryReplaceState.call(this, state, title, url);
- } catch(e) {
- console.warn('History.replaceState 修复执行失败:', e);
- // 忽略错误,不执行原方法
- return null;
- }
- };
- }
-
- // 3. 添加全局错误处理
- const originalOnError = window.onerror;
- window.onerror = function(message, source, lineno, colno, error) {
- // 过滤已知的H5兼容性错误
- if (typeof message === 'string') {
- if (message.includes("Cannot read property 'meta'") ||
- message.includes('replaceState') ||
- message.includes('showTabBar') ||
- message.includes('History')) {
- console.warn('H5兼容性错误已忽略:', message);
- return true; // 阻止默认错误处理
- }
- }
-
- // 其他错误交给原处理器
- if (originalOnError) {
- return originalOnError(message, source, lineno, colno, error);
- }
- return false;
- };
-
- // 4. 修复 URL 哈希处理
- if (window.location.hash === '' || window.location.hash === '#') {
- window.location.hash = '#/';
- }
-
- // 5. 监听 hashchange 事件,确保路由正常
- window.addEventListener('hashchange', function(e) {
- try {
- const hash = window.location.hash;
- if (!hash || hash === '#') {
- window.location.hash = '#/';
- }
- } catch(e) {
- console.warn('hashchange 处理失败:', e);
- }
- });
-
- isFixed = true;
- console.log('H5 路由修复完成');
-
- } catch(e) {
- console.warn('H5 路由修复失败:', e);
- }
- }
-
- // 恢复原始方法(用于调试)
- export function restoreH5Router() {
- if (!isFixed) return;
-
- try {
- if (originalGetCurrentPages) {
- window.getCurrentPages = originalGetCurrentPages;
- }
- if (originalHistoryReplaceState) {
- window.history.replaceState = originalHistoryReplaceState;
- }
-
- isFixed = false;
- console.log('H5 路由修复已恢复');
- } catch(e) {
- console.warn('H5 路由恢复失败:', e);
- }
- }
-
- // 页面加载完成后自动执行修复
- if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', fixH5Router);
- } else {
- fixH5Router();
- }
-
- // #endif
|