推拿小程序前端代码仓库
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.
 
 
 

107 lines
2.8 KiB

// H5 环境修复
export function fixH5Environment() {
// #ifdef H5
try {
// 修复路由 meta 属性访问问题
const originalGetCurrentPages = getCurrentPages;
window.getCurrentPages = function() {
try {
const pages = originalGetCurrentPages();
// 确保页面对象有基本的路由信息
if (pages && pages.length > 0) {
pages.forEach(page => {
if (page && !page.route) {
page.route = page.$page?.fullPath || '/';
}
if (page && page.route && !page.$page?.meta) {
page.$page = page.$page || {};
page.$page.meta = page.$page.meta || {};
}
});
}
return pages;
} catch(e) {
console.warn('getCurrentPages 修复失败:', e);
return [];
}
};
// 修复 History API
const originalReplaceState = window.history.replaceState;
window.history.replaceState = function(state, title, url) {
try {
// 确保 URL 格式正确
if (url && typeof url === 'string') {
// 修复格式不正确的 URL
if (url.startsWith('https:/#/')) {
url = url.replace('https:/#/', window.location.origin + '/#/');
}
// 如果 URL 不包含完整的域名,添加基础路径
if (url.startsWith('#/')) {
url = window.location.origin + window.location.pathname + url;
}
}
return originalReplaceState.call(this, state, title, url);
} catch(e) {
console.warn('History.replaceState 修复失败:', e);
return null;
}
};
// 修复路由跳转相关问题
const originalNavigateTo = uni.navigateTo;
uni.navigateTo = function(options) {
try {
return originalNavigateTo(options);
} catch(e) {
console.warn('uni.navigateTo 失败:', e);
// 降级处理
window.location.href = window.location.origin + window.location.pathname + '#' + options.url;
}
};
const originalReLaunch = uni.reLaunch;
uni.reLaunch = function(options) {
try {
return originalReLaunch(options);
} catch(e) {
console.warn('uni.reLaunch 失败:', e);
// 降级处理
window.location.href = window.location.origin + window.location.pathname + '#' + options.url;
}
};
console.log('H5 环境修复完成');
} catch(e) {
console.warn('H5 环境修复失败:', e);
}
// #endif
}
// 修复 URL 参数解析
export function fixUrlParams() {
// #ifdef H5
try {
function GetQueryString(name) {
const url = window.location.href;
try {
const cs = url.split('?')[1];
if (cs) {
const cs_arr = cs.split('&');
for (let i = 0; i < cs_arr.length; i++) {
if (cs_arr[i].split('=')[0] === name) {
sessionStorage.setItem('vid', cs_arr[i].split('=')[1]);
}
}
}
} catch(e) {
console.warn('URL参数解析失败:', e);
}
}
GetQueryString('vid');
} catch(e) {
console.warn('URL参数修复失败:', e);
}
// #endif
}