InspectionCleaning/utils/routeInterceptor.js

89 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 路由拦截器
* 用于在页面跳转时执行特定逻辑
*/
// 通过getCurrentPages获取当前页面并触发刷新
const checkCurrentPageAndRefresh = () => {
setTimeout(() => {
const pages = getCurrentPages();
if (pages.length > 0) {
const currentPage = pages[pages.length - 1];
const pagePath = currentPage.route;
console.log("当前页面路径:", pagePath);
// 如果当前页面是首页,触发刷新事件
if (pagePath === "pages/index/index") {
console.log("检测到首页,触发刷新事件");
uni.$emit("indexPageShow");
}
// 这里可以添加其他页面的刷新逻辑
// if (pagePath === 'pages/other/page') {
// uni.$emit('otherPageShow');
// }
} else {
console.log("getCurrentPages返回空数组无法获取当前页面");
}
}, 150); // 增加延迟,确保页面完全加载
};
// 拦截器配置
const interceptors = {
// 普通页面跳转
navigateTo: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 页面返回
navigateBack: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 切换 tabbar 页面
switchTab: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 重定向页面
redirectTo: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 重启应用并打开页面
reLaunch: {
success: () => {
checkCurrentPageAndRefresh();
},
},
};
// 初始化函数:注册所有拦截器
const initInterceptors = () => {
console.log("初始化路由拦截器");
// 注册各种导航方法的拦截器
uni.addInterceptor("navigateTo", interceptors.navigateTo);
uni.addInterceptor("navigateBack", interceptors.navigateBack);
uni.addInterceptor("switchTab", interceptors.switchTab);
uni.addInterceptor("redirectTo", interceptors.redirectTo);
uni.addInterceptor("reLaunch", interceptors.reLaunch);
console.log("路由拦截器注册完成");
};
// 导出一个对象,包含初始化方法和拦截器配置
export default {
init: initInterceptors,
interceptors,
};